예제 #1
0
        public override CompilerResult VisitExpression([NotNull] LangParser.ExpressionContext context)
        {
            if (context.NAME() != null)
            {
                string name = context.NAME().GetText();

                Parameter     parameter = CurrContext().lookupParameter(name);
                LLVMValueRef? @ref      = null;

                if (parameter != null)
                {
                    @ref = parameter.Load();
                }
                else
                {
                    LocalVariable lv = CurrContext().lookupLocalVar(name);

                    if (lv != null)
                    {
                        @ref = lv.Load(builder);
                    }
                }


                if ([email protected])
                {
                    throw new Exception("Lookup failed " + name);
                }

                return(new Int32CompilerResult(@ref.Value));
            }

            if (context.NUMBER() != null)
            {
                return(new Int32CompilerResult(LLVM.ConstInt(LLVM.Int32Type(), UInt32.Parse(context.NUMBER().GetText()), true)));
            }

            if (context.ChildCount == 3)
            {
                Int32CompilerResult lhs = (Int32CompilerResult)VisitExpression(context.expression()[0]);
                Int32CompilerResult rhs = (Int32CompilerResult)VisitExpression(context.expression()[1]);

                if (context.MULTIPLY() != null)
                {
                    return(new Int32CompilerResult(LLVM.BuildMul(builder, lhs.reference, rhs.reference, "multmp")));
                }
                if (context.DIVIDE() != null)
                {
                    return(new Int32CompilerResult(LLVM.BuildSDiv(builder, lhs.reference, rhs.reference, "divtmp")));
                }
                if (context.MODULO() != null)
                {
                    return(new Int32CompilerResult(LLVM.BuildSRem(builder, lhs.reference, rhs.reference, "remtmp")));
                }
                if (context.ADD() != null)
                {
                    return(new Int32CompilerResult(LLVM.BuildAdd(builder, lhs.reference, rhs.reference, "addtmp")));
                }
                if (context.SUBSTRACT() != null)
                {
                    return(new Int32CompilerResult(LLVM.BuildSub(builder, lhs.reference, rhs.reference, "subtmp")));
                }
            }

            return(base.VisitExpression(context));
        }
예제 #2
0
        public LLVMValueRef Assign(LLVMBuilderRef builder, Int32CompilerResult compilerResult)
        {
            LLVM.BuildStore(builder, compilerResult.reference, @ref);

            return(Load(builder));
        }