public GetValByAdress(IOperation adress, ValueType retType) { operationString = "get"; a = adress; try { variable = ASTTree.variables[(int)((adress as ASTvalue).getValue)]; operationString = variable.name; returnType = variable.returnTypes(); from = variable; return; } catch (Exception e) { variable = null; try { adder = (a as BinarySummatic).FindNonVariable(out from); } catch (Exception E) { // case if you have only *d, but not d[0], so there is no summ action in there from = adress; adder = new ASTvalue(new ValueType(VT.Cint), (object)0); } returnType = retType.TypeOfPointedByThis(); }; }
protected ASTvariable FindOriginalVariable() { if (variable != null) { return(variable); } if (a as BinarySummatic != null) { ASTvariable var = (a as BinarySummatic).FindVariable(); GetValByAdress deeper = (a as BinarySummatic).Deep(); if (var == null) { return(deeper.FindOriginalVariable()); } return(var); } return(null); }
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(""); }
public ASTvalue(string s, bool calledFromDefined) { LLVMname = "?LLVMNAME?"; string nums = "-1234567890."; bool isnum = true; int numPoints = 0; // cheking if is a nubmer for (int i = 0; i < s.Length; i++) { if (nums.IndexOf(s[i]) < 0) { isnum = false; break; } if (s[i] == '.') { numPoints++; if (numPoints > 1) { isnum = false; break; } } if (i > 0 && s[i] == '-') { isnum = false; break; } } // calculate a number if (isnum) { if (numPoints == 0) { this.valType = new ValueType(VT.Cint); this.data = (object)(int.Parse(s)); clr = ConsoleColor.DarkCyan; } else { this.valType = new ValueType(VT.Cdouble); this.data = (object)(double.Parse(s.Replace('.', ','))); clr = ConsoleColor.DarkCyan; } } else { // detect char if (s.IndexOf('\'') == 0 && s.LastIndexOf('\'') == s.Length - 1) { if (s.Length == 3 || (s.Length == 4 && s[1] == '\\')) { this.valType = new ValueType(VT.Cchar); this.data = (object)(s[1]); LLVMname = s[1] + ""; clr = ConsoleColor.DarkCyan; } else { throw new Exception("Char can not be more than 1 symbol"); } } else { // detect string if (s.IndexOf('\"') == 0 && s.LastIndexOf('\"') == s.Length - 1) { this.valType = new ValueType(VT.Cstring); string STR = s.Substring(1, s.Length - 2); this.data = (object)(STR); clr = ConsoleColor.DarkCyan; string STRLLVM = STR.Replace("\\r", "").Replace("\\n", "\\0A") + "\\00"; int leng = STRLLVM.Length; for (int sk = 0; sk < STRLLVM.Length - 1; sk++) { if (STRLLVM[sk] == '\\') { leng -= 2; } } LLVM.AddToCode(String.Format("@str{0} = private unnamed_addr constant [{1} x i8] c\"{2}\"\n", LLVM.globalVars, leng, STRLLVM)); this.array_type = "[" + leng + " x i8]"; LLVMname = "str" + LLVM.globalVars; LLVM.globalVars++; } else { if (s.ToLower() == "true" || s.ToLower() == "false") { this.valType = new ValueType(VT.Cboolean); this.data = (object)((s.ToLower() == "true")); clr = ConsoleColor.Gray; LLVMname = s.ToLower(); } else { // finally trying to find variable string varName = s; int found = -1; ASTvariable foundedVar = new ASTvariable(new ValueType(VT.Cunknown), "NONE", -1, new AdressType(-1, VAT.Unknown)); for (int i = 0; i < ASTTree.variables.Count; i++) { if (ASTTree.variables[i].name == varName && MISC.isVariableAvailable(i, calledFromDefined)) { foundedVar = ASTTree.variables[i]; found = i; break; } } if (found < 0) { throw new Exception("Used a variable \"" + varName + "\", that was never defined in this context!"); } else { if (!calledFromDefined) { foundedVar.everUsed++; } this.valType = foundedVar.getValueType; throw new Exception("GetAddr_" + found); } } } } } ASTTree.tokens.Add(this); }
public Define(string s, bool autoAssume) { //string firstPart = s.Substring(0, s.IndexOf("$") + 1); // int&a,b //while (s.IndexOf(',') > 0) //{ // int at = s.IndexOf(','); // s = s.Substring(0, s.IndexOf(',')) + ';' + firstPart + s.Substring(s.IndexOf(',') + 1); // // //} string[] ss = s.Split('$'); //string varName; VT varType; bool everDefined = false; for (int i = 0; i < ASTTree.variables.Count; i++) { if (ASTTree.variables[i].name == ss[1] && MISC.isVariableAvailable(i)) { everDefined = true; break; } } if (ss[1].Length > 0 && !everDefined) { varName = ss[1]; } else { throw new Exception("Can not define " + ((everDefined) ? "again " : "") + "variable with name \"" + ss[1] + "\""); } ss[0].ToLower(); varType = detectType(ss[0]); //for (int i =0; i < varName.Length; i++){ //bool isPointer = false; int pointerLevel = 0; while (varName.IndexOf('*') == 0) { pointerLevel++; varName = varName.Substring(1); } returnType = new ValueType(varType, pointerLevel); defineType = returnType; //____________________________________ if (varName.LastIndexOf("]") == varName.Length - 1 && varName.IndexOf("[") > 0) { List <string> inBr = MISC.splitByQuad(varName); // [] [] [ ] [] varName = varName.Substring(0, varName.IndexOf('[')); for (int ib = 0; ib < inBr.Count; ib++) { string inBrack = inBr[ib]; int length = 0; if (inBrack != "") { IOperation arrayLength = BinaryOperation.ParseFrom(inBrack); if (arrayLength.returnTypes() != VT.Cint) { throw new Exception("Int only can be array length parameter"); } //IOperation arrayLength = BinaryOperation.ParseFrom(inBrack); //if (arrayLength as ASTvalue == null || arrayLength.returnTypes() != VT.Cint) // throw new Exception("Incorrect array length parameters!"); //length = (int)(arrayLength as ASTvalue).getValue; //if (length < 1) // throw new Exception("Array length should be 1 and more!"); } //for (int i = 0; i < length; i++) //{ // // as default variable // ASTvariable newVar = new ASTvariable(new ValueType(varType, pointerLevel), varName + "#" + i, 0); // ASTTree.variables.Add(newVar); // MISC.pushVariable(ASTTree.variables.Count - 1); // ASTTree.tokens.Add(newVar); //} defineType = defineType.TypeOfPointerToThis(); } } //_________________________________________ ASTvariable NV = new ASTvariable(defineType, varName, pointerLevel, MISC.GetCurrentVariableAdressType()); var = NV; ASTTree.variables.Add(NV); MISC.pushVariable(ASTTree.variables.Count - 1); if (autoAssume) { MISC.defineVariable(ASTTree.variables.Count - 1); } ASTTree.tokens.Add(NV); a = NV; returnType = a.returnTypes(); b = new ASTvalue(new ValueType(VT.Cadress), (object)(ASTTree.variables.Count - 1)); }
public ASTvalue(string s) { string nums = "-1234567890."; bool isnum = true; int numPoints = 0; // cheking if is a nubmer for (int i = 0; i < s.Length; i++) { if (nums.IndexOf(s[i]) < 0) { isnum = false; break; } if (s[i] == '.') { numPoints++; if (numPoints > 1) { isnum = false; break; } } if (i > 0 && s[i] == '-') { isnum = false; break; } } // calculate a number if (isnum) { if (numPoints == 0) { this.valType = new ValueType(VT.Cint); this.data = (object)(int.Parse(s)); clr = ConsoleColor.DarkCyan; } else { this.valType = new ValueType(VT.Cdouble); this.data = (object)(double.Parse(s.Replace('.', ','))); clr = ConsoleColor.DarkCyan; } } else { // detect char if (s.IndexOf('\'') == 0 && s.LastIndexOf('\'') == s.Length - 1) { if (s.Length == 3 || (s.Length == 4 && s[1] == '\\')) { this.valType = new ValueType(VT.Cchar); this.data = (object)(s[1]); clr = ConsoleColor.DarkCyan; } else { throw new Exception("Char can not be more than 1 symbol"); } } else { // detect string if (s.IndexOf('\"') == 0 && s.LastIndexOf('\"') == s.Length - 1) { this.valType = new ValueType(VT.Cstring); this.data = (object)(s.Substring(1, s.Length - 2)); clr = ConsoleColor.DarkCyan; } else { if (s.ToLower() == "true" || s.ToLower() == "false") { this.valType = new ValueType(VT.Cboolean); this.data = (object)((s.ToLower() == "true")); clr = ConsoleColor.Gray; } else { // finally trying to find variable string varName = s; int found = -1; ASTvariable foundedVar = new ASTvariable(new ValueType(VT.Cunknown), "NONE", 0); for (int i = 0; i < ASTTree.variables.Count; i++) { if (ASTTree.variables[i].name == varName && MISC.isVariableAvailable(i)) { foundedVar = ASTTree.variables[i]; found = i; break; } } if (found < 0) { throw new Exception("Used a variable \"" + varName + "\", that was never defined in this context!"); } else { this.valType = foundedVar.getValueType; throw new Exception("GetAddr_" + found); } } } } } ASTTree.tokens.Add(this); }