Exemple #1
0
        public override string ToLLVM(int depth)
        {
            string res = var.ToLLVM();

            if (var.adress.typ != VAT.Global)
            {
                LLVM.AddToCode(String.Format("{0}{1} = alloca {2}\n", MISC.tabsLLVM(depth), res, var.returnTypes().ToLLVM()));
            }
            return(res);
            // %X = alloca i32 ; we have a pointer for %X, we like writing int*X = new int(); or some shit like it
            // now we can store i32 100, i32* %X
        }
Exemple #2
0
        public override string ToLLVM(int depth)
        {
            if (variable == null)
            {
                bool no_need_getelementptr = false;
                if ((adder as ASTvalue) != null && ((adder as ASTvalue).getValueType == VT.Cint) && (int)((adder as ASTvalue).getValue) == 0)
                {
                    no_need_getelementptr = true;
                }

                // MISC.LLVMtmpNumber += (no_need_getelementptr || LLVM_isLeftOperand) ? ((LLVM_isLeftOperand)? 0 : 1) : 2;   // need only 1 variable if not getelementptr
                string store_or_load_from = "????";
                if (!no_need_getelementptr)
                {
                    MISC.LLVMtmpNumber++;
                    int num = MISC.LLVMtmpNumber;
                    //LLVM.AddToCode(MISC.tabsLLVM(depth) + "%tmp" + (num) + " = " + LLVM.ParamToLLVM(depth, "getelementptr", returnTypes(), from, adder));
                    string FROM = from.ToLLVM(depth), ADDER = adder.ToLLVM(depth);
                    LLVM.AddToCode(String.Format("{0}%tmp{1} = getelementptr {2}, {3} {4}, {5} {6}\n", MISC.tabsLLVM(depth),
                                                 num, from.returnTypes().ToLLVM(), from.returnTypes().TypeOfPointerToThis().ToLLVM(), FROM, adder.returnTypes().ToLLVM(), ADDER));
                    store_or_load_from = "%tmp" + num;
                }
                else
                {
                    store_or_load_from = "" + from.ToLLVM(0);
                }

                MISC.LLVMtmpNumber++;
                int num2 = MISC.LLVMtmpNumber;
                if (!LLVM_isLeftOperand)
                {
                    LLVM.AddToCode(MISC.tabsLLVM(depth) + "%tmp" + (num2 + 1) + " = " + LLVM.Load(returnTypes(), from.returnTypes().ToLLVM() + " " + store_or_load_from) + "\n");
                    return("%tmp" + (num2 + 1));
                }
                else
                {
                    MISC.LLVMtmpNumber++;
                    int num12 = MISC.LLVMtmpNumber;
                    LLVM.AddToCode(String.Format("{0}%tmp{1} = load {2}, {3} {4}\n",
                                                 MISC.tabsLLVM(depth), num12, from.returnTypes().ToLLVM(), from.returnTypes().TypeOfPointerToThis().ToLLVM(), store_or_load_from));
                    store_or_load_from = "%tmp" + num12;

                    LLVM_isLeftOperand = false;
                    LLVM.AddToCode(MISC.tabsLLVM(depth) + "store ");
                    return(String.Format(" {0} {1}, align {2}", from.returnTypes().ToLLVM(), store_or_load_from, MISC.SyzeOf(returnTypes())));
                }
            }
            return(variable.ToLLVM());
        }
        public override string ToLLVM(int depth)
        {
            ASTvariable vari = null;

            if (a as GetValByAdress != null)
            {
                vari = ((a as GetValByAdress).from as ASTvariable);
            }
            if (a as Define != null)
            {
                vari = (a as Define).var;
            }

            if (vari != null && vari.everUsed > 0 && vari.adress.typ != VAT.Global)// && !vari.wasLoaded)
            {
                LLVM.varisReload.Add(vari);
                vari.reloadedTimes++;
                LLVM.CommandOrderQueueCode += String.Format("{0}{1} = load {2}, {3} {4}\n", MISC.tabsLLVM(depth), vari.ToLLVM(), vari.returnTypes().ToLLVM(), vari.returnTypes().TypeOfPointerToThis().ToLLVM(), MISC.RemoveCall(vari.ToLLVM()));
                vari.reloadedTimes--;
            }

            if (a as GetValByAdress != null)
            {
                // variable? from
                ASTvariable vari2 = ((a as GetValByAdress).from as ASTvariable);
                // NOT случай, когда переменная просто проходная- не глобальная, не параметр, и обращение идет прямо на неё
                if (!(vari2 != null /* && !vari.everPointed*/ && vari2.adress.typ != VAT.Parameter))
                {
                    (a as GetValByAdress).LLVM_isLeftOperand = true;
                    string number = b.ToLLVM(depth), add_last = a.ToLLVM(depth);
                    LLVM.AddToCode(b.returnTypes().ToLLVM() + " " + number + "," + add_last);
                    return("");
                }
            }
            if (!(vari != null && vari.adress.typ == VAT.Global))
            {
                return(String.Format("{0}store {3} {4}, {1} {2}", MISC.tabsLLVM(depth), a.returnTypes().TypeOfPointerToThis().ToLLVM(), MISC.RemoveCall(a.ToLLVM(depth)), b.returnTypes().ToLLVM(), b.ToLLVM(depth)));
            }
            return("");
        }