private void EmitVariable(LocalVariableDeclaration variable, CCodeBuilder code)
        {
            Requires.That(nameof(variable), variable.Exists, "tried to look up variable that does not exist");
            var initializer = variable.IsParameter ? $" = {nameMangler.Mangle(variable.Name)}" : "";

            code.AppendLine($"{typeConverter.Convert(variable.Type)} _{NameOf(variable.Reference)}{initializer}; // {variable}");
        }
Ejemplo n.º 2
0
        private void EmitVariable(VariableDeclaration declaration, CCodeBuilder code)
        {
            Requires.That(nameof(declaration), declaration.TypeIsNotEmpty, "tried to look up variable that does not exist");
            var initializer = declaration.IsParameter ? $" = {(declaration.Symbol is SelfParameterSymbol ? nameMangler.SelfName : nameMangler.Mangle(declaration.Symbol!.Name!))}" : "";

            code.AppendLine($"{typeConverter.Convert(declaration.Type)} _{declaration.Variable.Name}{initializer}; // {declaration}");
        }
 private void EmitBlock(BasicBlock block, bool voidReturn, CCodeBuilder code)
 {
     code.AppendLine($"bb{block.Number}:");
     code.BeginBlock();
     foreach (var statement in block.Statements)
     {
         EmitStatement(statement, voidReturn, code);
     }
     code.EndBlock();
 }
Ejemplo n.º 4
0
        private void EmitBlock(Block block, bool isConstructor, CCodeBuilder code)
        {
            code.AppendLine($"bb{block.Number}:");
            code.BeginBlock();
            foreach (var instruction in block.Instructions)
            {
                EmitInstruction(instruction, code);
            }

            EmitInstruction(block.Terminator, isConstructor, code);
            code.EndBlock();
        }
Ejemplo n.º 5
0
        private void EmitInstruction(Instruction instruction, CCodeBuilder code)
        {
            code.AppendLine("// " + instruction);
            switch (instruction)
            {
            default:
                throw ExhaustiveMatch.Failed(instruction);

            case InstructionWithResult ins:
                EmitInstructionWithResult(ins, code);
                break;

            case CallInstruction ins:
            {
                if (!(ins.ResultPlace is null))
                {
                    EmitResultPlace(ins.ResultPlace, code);
                }
        private void EmitStatement(Statement statement, bool voidReturn, CCodeBuilder code)
        {
            code.AppendLine("// " + statement);
            switch (statement)
            {
            case IfStatement ifStatement:
                code.AppendLine($"if({ConvertValue(ifStatement.Condition)}._value) goto bb{ifStatement.ThenBlockNumber}; else goto bb{ifStatement.ElseBlockNumber};");
                break;

            case GotoStatement gotoStatement:
                code.AppendLine($"goto bb{gotoStatement.GotoBlockNumber};");
                break;

            case ReturnStatement _:
                code.AppendLine(voidReturn ? "return;" : "return _result;");
                break;

            case AssignmentStatement assignment:
                code.AppendLine(
                    $"{ConvertPlace(assignment.Place)} = {ConvertValue(assignment.Value)};");
                break;

            case ActionStatement action:
                code.AppendLine(ConvertValue(action.Value) + ";");
                break;

            case DeleteStatement deleteStatement:
            {
                var self     = ConvertValue(deleteStatement.Place);
                var typeName = nameMangler.Mangle(deleteStatement.Type);
                // TODO once deletes are implemented, call them
                //code.AppendLine($"{self}._vtable->{typeName}___delete__1({self});");
                code.AppendLine($"free({self}._self);");
                break;
            }

            default:
                throw NonExhaustiveMatchException.For(statement);
            }
        }
Ejemplo n.º 7
0
        private void EmitFieldInitialization(FieldInitializationIL fieldInitialization, CCodeBuilder code)
        {
            var fieldName     = nameMangler.Mangle(fieldInitialization.Field);
            var parameterName = nameMangler.Mangle(fieldInitialization.Field.Name);

            // Emit direct access to avoid any issues about whether corresponding variables exist
            code.AppendLine($"_self._self->{fieldName} = {parameterName};");
        }