Ejemplo n.º 1
0
        protected void EmitFunction(
            CParser.TypeSpecifierContext typeSpecifier,
            ITerminalNode identifier,
            CParser.ParameterTypeListContext parameters,
            CParser.CompoundStatementContext compoundStatement)
        {
            var functionName = identifier.ToString();

            //CurrentArgs_ = Functions_[CurrentTypeBuilder_.Name][functionName].Args;
            _generatorIL = _functions[functionName].MethodBuilder.GetILGenerator();

            LocalObjectDef.InitGenerator(_generatorIL);

            if (compoundStatement.blockItemList() != null)
            {
                var returnObjectDef = EmitCompoundStatement(compoundStatement);

                returnObjectDef.Load();

                if (_functions[functionName].MethodBuilder.ReturnType == typeof(void))
                {
                    _generatorIL.Emit(OpCodes.Pop);
                }

                _generatorIL.Emit(OpCodes.Ret);

                returnObjectDef.Remove();
            }
            else
            {
                _generatorIL.Emit(OpCodes.Ret);
            }
        }
Ejemplo n.º 2
0
        protected void DefineFunction(
            CParser.TypeSpecifierContext typeSpecifier,
            ITerminalNode identifier,
            CParser.ParameterTypeListContext parameters)
        {
            Type[] inputTypes = Type.EmptyTypes;

            var functionName       = identifier.ToString();
            var functionReturnType = typeSpecifier.RBAType(_moduleBuilder);
            var args = new Dictionary <string, MethodArgDef>
            {
                { "this", new MethodArgDef(_programClass, 0, "this") }
            };

            if (parameters != null)
            {
                //var parametersStack
                //= new Stack<CParser.ExternalDeclarationContext>();

                //parameters.
                //var functionListNode = treeNode.GetChild(3);
                //inputTypes = new Type[functionListNode.ChildCount];
                //for (int k = 0; k < functionListNode.ChildCount; k++)
                //{
                //    inputTypes[k] = GetType(functionListNode.GetChild(k).GetChild(1).Text);
                //    var argName = functionListNode.GetChild(k).GetChild(0).Text;
                //    args.Add(argName, new ArgObjectDef(inputTypes[k], k + 1, argName));
                //}
            }
            else
            {
                inputTypes = Type.EmptyTypes;
            }

            MethodBuilder methodBuilder;

            if (functionName == "main")
            {
                methodBuilder = _programClass.DefineMethod(functionName,
                                                           MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig, typeof(void), Type.EmptyTypes);
                methodBuilder.SetCustomAttribute(new CustomAttributeBuilder(
                                                     typeof(STAThreadAttribute).GetConstructor(Type.EmptyTypes), new object[] { }));
                _assemblyBuilder.SetEntryPoint(methodBuilder);
                _entryPoint = methodBuilder;
            }
            else
            {
                methodBuilder = _programClass.DefineMethod(functionName, MethodAttributes.Public | MethodAttributes.HideBySig, functionReturnType, inputTypes);
            }

            _functions.Add(functionName, new MethodDef(functionName, args, methodBuilder));
        }