Skip to content

Commit

Permalink
(#120) Add IDeclarationScope to make scope for for statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Griboedoff committed May 3, 2022
1 parent bc8f4c2 commit fc6060c
Show file tree
Hide file tree
Showing 27 changed files with 54 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Contexts/FunctionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Cesium.CodeGen.Contexts;

internal record FunctionScope(TranslationUnitContext Context, MethodDefinition Method)
internal record FunctionScope(TranslationUnitContext Context, MethodDefinition Method) : IDeclarationScope
{
public AssemblyContext AssemblyContext => Context.AssemblyContext;
public ModuleDefinition Module => Context.Module;
Expand Down
16 changes: 16 additions & 0 deletions Cesium.CodeGen/Contexts/IDeclarationScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Cesium.CodeGen.Contexts.Meta;
using Mono.Cecil;
using Mono.Cecil.Cil;

namespace Cesium.CodeGen.Contexts;

internal interface IDeclarationScope
{
AssemblyContext AssemblyContext { get; }
ModuleDefinition Module { get; }
TypeSystem TypeSystem { get; }
IReadOnlyDictionary<string, FunctionInfo> Functions { get; }
TranslationUnitContext Context { get; }
MethodDefinition Method { get; }
Dictionary<string, VariableDefinition> Variables { get; }
}
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Extensions/CodeGenEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Cesium.CodeGen.Extensions;

internal static class CodeGenEx
{
public static void StLoc(this FunctionScope scope, VariableDefinition variable)
public static void StLoc(this IDeclarationScope scope, VariableDefinition variable)
{
scope.Method.Body.Instructions.Add(Instruction.Create(OpCodes.Stloc, variable));
}
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/BlockItems/CompoundStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CompoundStatement(Ast.CompoundStatement statement)

public IBlockItem Lower() => this; // since actual lowering of child items is done on emit, anyway

public void EmitTo(FunctionScope scope)
public void EmitTo(IDeclarationScope scope)
{
foreach (var item in _items)
{
Expand Down
4 changes: 2 additions & 2 deletions Cesium.CodeGen/Ir/BlockItems/DeclarationBlockItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public IBlockItem Lower()
}


public void EmitTo(FunctionScope scope)
public void EmitTo(IDeclarationScope scope)
{
switch (_declaration)
{
Expand All @@ -56,7 +56,7 @@ public void EmitTo(FunctionScope scope)
}
}

private static void EmitScopedIdentifier(FunctionScope scope, ScopedIdentifierDeclaration scopedDeclaration)
private static void EmitScopedIdentifier(IDeclarationScope scope, ScopedIdentifierDeclaration scopedDeclaration)
{
scopedDeclaration.Deconstruct(out var declarations);
foreach (var (declaration, initializer) in declarations)
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/BlockItems/ExpressionStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public ExpressionStatement(Ast.ExpressionStatement statement) : this(statement.E
}

public IBlockItem Lower() => new ExpressionStatement(_expression?.Lower());
public void EmitTo(FunctionScope scope) => _expression?.EmitTo(scope);
public void EmitTo(IDeclarationScope scope) => _expression?.EmitTo(scope);
}
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/BlockItems/ForStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public IBlockItem Lower()
_updateExpression?.Lower(),
_body.Lower());

public void EmitTo(FunctionScope scope)
public void EmitTo(IDeclarationScope scope)
{
var bodyProcessor = scope.Method.Body.GetILProcessor();
var instructions = bodyProcessor.Body.Instructions;
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/BlockItems/IBlockItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Cesium.CodeGen.Ir.BlockItems;
internal interface IBlockItem
{
IBlockItem Lower();
void EmitTo(FunctionScope scope);
void EmitTo(IDeclarationScope scope);
}
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/BlockItems/IfElseStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public IfElseStatement(Ast.IfElseStatement statement)

public IBlockItem Lower() => new IfElseStatement(_expression.Lower(), _trueBranch.Lower(), _falseBranch?.Lower());

public void EmitTo(FunctionScope scope)
public void EmitTo(IDeclarationScope scope)
{
// TODO[#113]: when branch ends with ret opcode if can be optimized and not generate jmp label

Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/BlockItems/ReturnStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private ReturnStatement(IExpression expression)

public IBlockItem Lower() => new ReturnStatement(_expression.Lower());

public void EmitTo(FunctionScope scope)
public void EmitTo(IDeclarationScope scope)
{
_expression.EmitTo(scope);
scope.Method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/AssignmentExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AssignmentExpression(Ast.AssignmentExpression expression) : base(expressi
private AssignmentExpression LowerSmthAndAssign(BinaryOperator @operator)
=> new(Left, BinaryOperator.Assign, new BinaryOperatorExpression(Left, @operator, Right.Lower()));

public override void EmitTo(FunctionScope scope)
public override void EmitTo(IDeclarationScope scope)
{
if (Operator != BinaryOperator.Assign)
throw new NotSupportedException($"Operator {Operator} should've been lowered before emitting.");
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/BinaryOperatorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public BinaryOperatorExpression(Ast.BinaryOperatorExpression expression)
_ => new BinaryOperatorExpression(Left.Lower(), Operator, Right.Lower()),
};

public virtual void EmitTo(FunctionScope scope)
public virtual void EmitTo(IDeclarationScope scope)
{
Left.EmitTo(scope);
Right.EmitTo(scope);
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/ConstantExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ConstantExpression(Ast.ConstantExpression expression)

public IExpression Lower() => this;

public void EmitTo(FunctionScope scope) => _constant.EmitTo(scope);
public void EmitTo(IDeclarationScope scope) => _constant.EmitTo(scope);

public override string ToString() => $"{nameof(ConstantExpression)}: {_constant}";

Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/Constants/CharConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public CharConstant(string value)
_value = checked((byte)UnescapeCharacter(value));
}

public void EmitTo(FunctionScope scope)
public void EmitTo(IDeclarationScope scope)
{
var instructions = scope.Method.Body.Instructions;
instructions.Add(Instruction.Create(OpCodes.Ldc_I4, (int)_value));
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/Constants/IConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Cesium.CodeGen.Ir.Expressions.Constants;

internal interface IConstant
{
void EmitTo(FunctionScope scope);
void EmitTo(IDeclarationScope scope);
}
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/Constants/IntegerConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public IntegerConstant(string value)
throw new NotSupportedException($"Cannot parse an integer literal: {value}.");
}

public void EmitTo(FunctionScope scope)
public void EmitTo(IDeclarationScope scope)
{
// TODO[#92]: Optimizations like Ldc_I4_0 for selected constants
scope.Method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4, _value));
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/Constants/StringConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public StringConstant(IToken<CTokenType> token)
_value = token.UnwrapStringLiteral();
}

public void EmitTo(FunctionScope scope)
public void EmitTo(IDeclarationScope scope)
{
var fieldReference = scope.AssemblyContext.GetConstantPoolReference(_value);
scope.Method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldsflda, fieldReference));
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/FunctionCallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public FunctionCallExpression(Ast.FunctionCallExpression expression)
(IdentifierConstantExpression)_function.Lower(),
_arguments.Select(a => a.Lower()).ToList());

public void EmitTo(FunctionScope scope)
public void EmitTo(IDeclarationScope scope)
{
foreach (var argument in _arguments)
argument.EmitTo(scope);
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/IExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Cesium.CodeGen.Ir.Expressions;
internal interface IExpression
{
IExpression Lower();
void EmitTo(FunctionScope scope);
void EmitTo(IDeclarationScope scope);
}
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/ILValueExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Cesium.CodeGen.Ir.Expressions;

internal interface ILValueExpression
{
ILValue Resolve(FunctionScope scope);
ILValue Resolve(IDeclarationScope scope);
}
10 changes: 7 additions & 3 deletions Cesium.CodeGen/Ir/Expressions/IdentifierConstantExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ public IdentifierConstantExpression(Ast.ConstantExpression expression)

public IExpression Lower() => this;

public ILValue Resolve(FunctionScope scope)
public ILValue Resolve(IDeclarationScope scope)
{
scope.Variables.TryGetValue(Identifier, out var var);
var par = scope.GetParameter(Identifier);
var par = scope switch
{
FunctionScope funcScope => funcScope.GetParameter(Identifier),
_ => throw new NotImplementedException($"Can't get parameter from {scope.GetType()}"),
};

switch (var, par)
{
Expand All @@ -38,5 +42,5 @@ public ILValue Resolve(FunctionScope scope)
}
}

public void EmitTo(FunctionScope scope) => Resolve(scope).EmitGetValue(scope);
public void EmitTo(IDeclarationScope scope) => Resolve(scope).EmitGetValue(scope);
}
4 changes: 2 additions & 2 deletions Cesium.CodeGen/Ir/Expressions/LValues/ILValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Cesium.CodeGen.Ir.Expressions.LValues;

internal interface ILValue
{
void EmitGetValue(FunctionScope scope);
void EmitSetValue(FunctionScope scope);
void EmitGetValue(IDeclarationScope scope);
void EmitSetValue(IDeclarationScope scope);
}
4 changes: 2 additions & 2 deletions Cesium.CodeGen/Ir/Expressions/LValues/LValueLocalVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public LValueLocalVariable(VariableDefinition definition)
_definition = definition;
}

public void EmitGetValue(FunctionScope scope)
public void EmitGetValue(IDeclarationScope scope)
{
// TODO[#92]: Special instructions to emit Ldloc_0 etc.
scope.Method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldloc, _definition));
}

public void EmitSetValue(FunctionScope scope) => scope.StLoc(_definition);
public void EmitSetValue(IDeclarationScope scope) => scope.StLoc(_definition);
}
4 changes: 2 additions & 2 deletions Cesium.CodeGen/Ir/Expressions/LValues/LValueParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public LValueParameter(ParameterDefinition definition)
_definition = definition;
}

public void EmitGetValue(FunctionScope scope)
public void EmitGetValue(IDeclarationScope scope)
{
// TODO[#92]: Special instructions to emit Ldarg_0 etc.
scope.Method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg, _definition));
}

public void EmitSetValue(FunctionScope scope)
public void EmitSetValue(IDeclarationScope scope)
{
// TODO[#92]: Special instructions to emit Starg_0 etc.
scope.Method.Body.Instructions.Add(Instruction.Create(OpCodes.Starg, _definition));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public LogicalBinaryOperatorExpression(Ast.LogicalBinaryOperatorExpression expre

public override IExpression Lower() => new LogicalBinaryOperatorExpression(Left.Lower(), Operator, Right.Lower());

public override void EmitTo(FunctionScope scope)
public override void EmitTo(IDeclarationScope scope)
{
switch (Operator)
{
Expand All @@ -31,7 +31,7 @@ public override void EmitTo(FunctionScope scope)
}
}

private void EmitLogicalAnd(FunctionScope scope)
private void EmitLogicalAnd(IDeclarationScope scope)
{
var bodyProcessor = scope.Method.Body.GetILProcessor();
var fastExitLabel = bodyProcessor.Create(OpCodes.Ldc_I4_0);
Expand All @@ -51,7 +51,7 @@ private void EmitLogicalAnd(FunctionScope scope)
bodyProcessor.Append(exitLabel);
}

private void EmitLogicalOr(FunctionScope scope)
private void EmitLogicalOr(IDeclarationScope scope)
{
var bodyProcessor = scope.Method.Body.GetILProcessor();
var fastExitLabel = bodyProcessor.Create(OpCodes.Ldc_I4_1);
Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/PrefixIncrementExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public IExpression Lower()
);
}

public void EmitTo(FunctionScope scope) => throw new NotSupportedException("Should be lowered");
public void EmitTo(IDeclarationScope scope) => throw new NotSupportedException("Should be lowered");
}
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Ir/Expressions/UnaryOperatorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public UnaryOperatorExpression(Ast.UnaryOperatorExpression expression)

public virtual IExpression Lower() => new UnaryOperatorExpression(_operator, _target.Lower());

public virtual void EmitTo(FunctionScope scope)
public virtual void EmitTo(IDeclarationScope scope)
{
_target.EmitTo(scope);
scope.Method.Body.Instructions.Add(GetInstruction());
Expand Down

0 comments on commit fc6060c

Please sign in to comment.