public void EmitEntryPointAdapter(FunctionDeclaration entryPoint, Code code)
        {
            if (entryPoint == null)
            {
                return;
            }

            code.Definitions.DeclarationSeparatorLine();
            code.Definitions.AppendLine("// Entry Point Adapter");
            code.Definitions.AppendLine("int32_t main(const int argc, char const * const * const argv)");
            code.Definitions.BeginBlock();
            var arguments = new List <string>();

            foreach (var parameterTypeName in entryPoint.Parameters.Select(p => p.Type).Cast <ObjectType>().Select(t => t.Name))
            {
                if (parameterTypeName.Equals(Name.From("system", "console", "Console")))
                {
                    code.Definitions.AppendLine("system__console__Console console = { &system__console__Console___vtable, malloc(sizeof(system__console__Console___Self)) };");
                    arguments.Add("system__console__Console___new__1(console)");
                }
                else if (parameterTypeName.Equals(Name.From("system", "console", "Arguments")))
                {
                    throw new NotImplementedException();
                }
                else
                {
                    throw new Exception($"Unexpected type for parameter to main: {parameterTypeName}");
                }
            }
            var joinedArguments = string.Join(", ", arguments);

            if (entryPoint.ReturnType == DataType.Void)
            {
                code.Definitions.AppendLine($"{nameMangler.MangleName(entryPoint)}({joinedArguments});");
                code.Definitions.AppendLine("return 0;");
            }
            else
            {
                code.Definitions.AppendLine($"return {nameMangler.MangleName(entryPoint)}({joinedArguments})._value;");
            }

            code.Definitions.EndBlock();
        }
Exemple #2
0
        private void EmitFunction(FunctionDeclaration function, Code code)
        {
            // Don't emit functions without control flow, they are generic
            // TODO need to handle better
            if (function.ControlFlow == null)
            {
                return;
            }

            var name       = nameMangler.MangleName(function);
            var parameters = Convert(function.Parameters);
            var returnType = typeConverter.Convert(function.ReturnType.AssertResolved());

            // Write out the function declaration for C so we can call functions defined after others
            code.FunctionDeclarations.AppendLine($"{returnType} {name}({parameters});");

            code.Definitions.DeclarationSeparatorLine();
            code.Definitions.AppendLine($"{returnType} {name}({parameters})");
            code.Definitions.BeginBlock();
            controlFlowEmitter.Emit(function.ControlFlow, code);
            code.Definitions.EndBlock();
        }
        private void EmitFunction(FunctionIL function, Code code)
        {
            var name       = nameMangler.MangleName(function);
            var parameters = Convert(function.Parameters);
            var returnType = typeConverter.Convert(function.Symbol.ReturnDataType.Known());

            // Write out the function declaration for C so we can call functions defined after others
            code.FunctionDeclarations.AppendLine($"{returnType} {name}({parameters});");

            code.Definitions.DeclarationSeparatorLine();
            code.Definitions.AppendLine($"{returnType} {name}({parameters})");
            code.Definitions.BeginBlock();
            controlFlowEmitter.Emit(function, code);
            code.Definitions.EndBlock();
        }