⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 llvm.g

📁 一种将c高级语言转化给VHDL的编译器
💻 G
📖 第 1 页 / 共 4 页
字号:
	  	  try {	    if (t1 == Type.Bool) {	      if (resVarid.isVersioned()) {		resOperand = Operand.newBoolean(resVarid.getBasename(), resVarid.getVersion());	      } else {		resOperand = Operand.newBoolean(resVarid.getBasename());	      }	    } else {	      if (resVarid.isVersioned()) {		resOperand = Operand.newBlock(resVarid.getBasename(), resVarid.getVersion());	      } else {		resOperand = Operand.newBlock(resVarid.getBasename());	      }	    }	  } catch (IllegalOperand e) {	    System.err.println("Error creating result operand: " + resVarid.getBasename() + " on line: " + LT(0).getLine());	    throw(e);	  }	  inst = Select.create(Operators.SELECT, t1, resOperand, (BooleanOperand)cond, op1, op2);	  iList.addLast(inst);	};getelementptrI returns [LinkedList iList]	{	  iList = new LinkedList();	  Instruction inst = null;	  Varid resVarid = null;	  Operand resOperand = null;	  GepInfo gepInfo = null;	}:	resVarid=varid EQUAL GETELEMENTPTR gepInfo=getelementptrInfo	{	  try {	      if (resVarid.isVersioned()) {		resOperand = Operand.newAddr(resVarid.getBasename(), resVarid.getVersion());	      } else {		resOperand = Operand.newAddr(resVarid.getBasename());	      }	  } catch (IllegalOperand e) {	    System.err.println("Error creating result operand: " + resVarid.getBasename() + " on line: " + LT(0).getLine());	    throw(e);	  }	  inst = Getelementptr.create(Operators.GETELEMENTPTR, resOperand, gepInfo.type, gepInfo.operand, gepInfo.pairs);	  GepInfo.addrOperandHashMap.put(resOperand, gepInfo.operand);	  iList.addLast(inst);	};	getelementptrInfo returns [GepInfo info]	{	  Type t = null;	  Type t1 = null;	  LinkedList pairs = new LinkedList();	  Operand val = null;	  info = null; 	}:	// if we handled user-defined structs, then you'd have to add that here	t=type ptrVarid:VARID (COMMA t1=type val=value[t1, false]	{	  pairs.addLast(new TypeOperand(t1));	  pairs.addLast(val);	}	)*	{	  try {	    //System.out.println("Making a primal from: " + ptrVarid.getText());	    info = new GepInfo(t, pairs, Operand.newPrimal(ptrVarid.getText()));	  } catch (IllegalOperand e) {	    System.err.println("Error creating primal operand: " + ptrVarid.getText() + " on line: " + LT(0).getLine());	    throw(e);	  }	};switchI returns [LinkedList iList]	{	  iList = new LinkedList();	  Instruction inst = null;	  Operand val, dest, val2, dest2 = null;	  Type t, t2 = null;	  LinkedList cases = new LinkedList();	}:	SWITCH t=type 	{	  if (!t.isInteger()){	    System.err.println("Error:  First type argument to switch instruction must be integer on line: " + LT(0).getLine());	    System.exit(-1);	  }	}	val=value[t, false] COMMA LABEL dest=value[Type.Label, false] LBRACKET eol! (t2=type val2=value[t, false] COMMA LABEL dest2=value[Type.Label, false] 	{	  if (!t2.isInteger()){	    System.err.println("Error:  Type argument to switch case must be integer on line: " + LT(0).getLine());	    System.exit(-1);	  }	  cases.addLast(val2);	  cases.addLast(dest2);	}	eol!)* RBRACKET	{	  inst = Switch.create(Operators.SWITCH, t, val, (LabelOperand)dest, cases);	  iList.addLast(inst);	};type returns [ Type t ]	{ 	  Token tok = null;	  t = null;	  Type t2 = null;	}: 	((VOID {t = Type.Void;}	  | BOOL {t = Type.Bool;}	  | SBYTE {t = Type.Sbyte;} 	  | UBYTE { t = Type.Ubyte;}	  | SHORT { t = Type.Short;}	  | USHORT { t = Type.Ushort; }	  | INT { t = Type.Int;} 	  | UINT { t = Type.Uint;}	  | LONG { t = Type.Long;}	  | ULONG { t = Type.Ulong;}	  | FLOAT { t = Type.Float;}	  | DOUBLE { t = Type.Double;}	  | LABEL { t = Type.Label;}	  | (LBRACKET num:PINTEGER dv:DECLVARID t2=type RBRACKET	  { 	    if (dv.getText().compareTo("x") != 0) {	      System.err.println("Error: array dimensions must be separated by x on line: " + LT(0).getLine());	      System.exit(-1);	    }	    t = new ArrayType(t2, Integer.parseInt(num.getText()));	  })	  // would put case for user-defined structure type here if we handled it	  ) (STAR {t = new PointerType(t);})*)	 {	   //tok = LT(0); 	   //System.out.println("type="+tok.getText());	 };varid returns [Varid v]	{	  Token tok = null;	  String s = null;	  String numString = "";	  String zeroString = "";	  String nameString = "";	  int index, version = 0;	  boolean isVersioned = false;	  v = null;	}:	(VARID	  {	    tok = LT(0);	    s = tok.getText(); 	    //System.out.print("varid: " + s);	    // Find out where last '.' is.	    index = s.lastIndexOf('.');	    //System.out.print(" index: " + index);	    // If there is a last '.', add one to the index, so we can get to the numbers	    // after it, if there are any.	    if (index != -1) index++;            // Get ahold of the numbers after the '.', if there are any.	    // For example, tmp.134  would have numString 134, that is the version.	    if ((index <= (s.length()-1)) && (index > -1)) {	      numString = s.substring(index);	      //System.out.print(" num: " + numString);	      for (int i = 0; i < numString.length(); i++) {		if (!Character.isDigit(numString.charAt(i))) {		  break;		}		isVersioned = true;	      }	    }            // In the case of tmp.345, 345 is the version.	    // tmp.foo is not versioned.	    if (isVersioned) {	      //System.out.println(" versioned");              // Check for leading zeros in the numString,	      // for example, tmp.001	      for (int i=0; i < numString.length(); i++) {		if (numString.charAt(i) == '0') {		  zeroString = zeroString + '0';	          //System.out.print(" zeroString: " + zeroString);		} else {		  break;		}		// Special case: if the numString is all zeros		if (zeroString.length() == numString.length()) {		  // If numString is only one 0, then don't make a zeroString.		  // The 0 is the version and there is no confusion.		  if (numString.length() == 1) {		    zeroString = "";		  } else {		    // Otherwise, take one 0 off the zeroString,		    // because we want to use that 0 for its version number.		    // The other zeros are for its unique name we're making.		    zeroString = zeroString.substring(1, zeroString.length());		  }		}	      }	      	      // Create the version number from the numString	      version = Integer.parseInt(numString);	      // If the version number is preceded by 0's, we have a problem,	      // because if we just make tmp.001 have name tmp and version 1,	      // that would collide with tmp.1, which also has name tmp and version	      // 1.  So, what we need to do is to make the new unique	      // name the name base, followed by "__", and then followed by	      // the zero string.  For example, tmp.001 would become tmp__00 and have	      // version 1.  Similarly, tmp.000 would become tmp__00 and have version 0.	      if ((zeroString.compareTo("")) != 0) {		nameString = s.substring(0, index-1) + "__" + s.substring(index, index + zeroString.length());	        //System.out.print(" nameString w/ 0s: " + nameString);	      } else {		// In the case of no zerostring (no preceding zeros in the		// number part of the string), just use the base name and use the		// number part for the version (tmp.345 has name tmp and number 345).		nameString = s.substring(0, index-1);	        //System.out.print(" nameString w/o 0s: " + nameString);	      }	      v = new Varid(nameString , version);	    } else {	      // In the case of no version number, it is probably a primal	      v = new Varid(s);	      //System.out.println(" NOT versioned");	    }	    //System.out.println(); 	  }	  )	  {	  };value [Type t, boolean must_exist] returns [ Operand o ]	/* must_exist means that value() can't return null.  If anything, it would 	return UnknownOperand.  This is used mostly for phi instructions.  */	{ 	  Token tok = null;	  o = null;	  String s = null;	  Varid v = null;	}:  	(v=varid	  { 	    try {	      if (v.isVersioned()) {	          o = Operand.getOperand(v.getBasename(), v.getVersion());	        } else {	          o = Operand.getOperand(v.getBasename(), Operand.NOT_ASSIGNED);	        }	      if ((o == null) && !must_exist) {	        if (t == Type.Label) { 	          if (v.isVersioned()) {	            o = Operand.newLabel(v.getBasename(), v.getVersion());	          } else {	            o = Operand.newLabel(v.getBasename());	          }	        } else if (t == Type.Bool) {	          if (v.isVersioned()) {	            o = Operand.newBoolean(v.getBasename(), v.getVersion());	          } else {	            o = Operand.newBoolean(v.getBasename());	          }	        } else {	          if (v.isVersioned()) {	            o = Operand.newBlock(v.getBasename(), v.getVersion());	          } else {	            o = Operand.newBlock(v.getBasename());	          }		}	      } else if (o == null) {	          if (v.isVersioned()) {	            o = Operand.newUnknown(LT(0).getText(), v.getBasename(), v.getVersion());	          } else {	            o = Operand.newUnknown(v.getBasename());	          }	      }	    } catch (IllegalOperand e) {	      tok = LT(0);	      System.err.println("Error creating operand: " + tok.getText() + " on line: " + tok.getLine());	      throw(e);	    }	     	    //System.out.println("value Operand " + o.toString());	  }	  /* There is a problem with integer constants coming in as decimal strings	     because they can be unsigned and Java doesn't handle reading in	     unsigned decimal strings.  It assumes they are signed, so the number	     could go past the bounds of a signed int (or long if that is the case)	     and I haven't handled this yet.	     */	  | NINTEGER  	  { 	    tok = LT(0); 	    if ((t == Type.Long) || (t == Type.Ulong)) {	      o = Operand.newLongConstant(Long.parseLong(tok.getText())); 	    } else if ((t == Type.Int) || (t == Type.Uint) || (t == Type.Ubyte)	    || (t == Type.Sbyte) || (t == Type.Ushort) || (t == Type.Short)) {	      o = Operand.newIntConstant(Integer.parseInt(tok.getText())); 	    }	  }	  | PINTEGER  	  { 	    tok = LT(0); 	    if ((t == Type.Long) || (t == Type.Ulong)) {	      o = Operand.newLongConstant(Long.parseLong(tok.getText())); 	    } else if ((t == Type.Int) || (t == Type.Ubyte)	    || (t == Type.Sbyte) || (t == Type.Ushort) || (t == Type.Short)) {	      o = Operand.newIntConstant(Integer.parseInt(tok.getText())); 	    } else if (t == Type.Uint) {	      BigInteger uInt = new BigInteger(tok.getText());	      if(uInt.compareTo(new BigInteger("2147483647")) == 1) {	        BigInteger uIntMinusOne = uInt.subtract(new BigInteger("1"));		BigInteger inverseInt = uIntMinusOne.not();		Integer convertedInt = new Integer(-inverseInt.intValue());		o = Operand.newIntConstant(convertedInt.intValue());	      }	      else	        o = Operand.newIntConstant(Integer.parseInt(tok.getText())); 					    }	  }	  | FPCONSTANT 	  { 	    tok = LT(0);	    if (t == Type.Float) {	      o = Operand.newFloatConstant(Float.parseFloat(tok.getText())); 	    } else  if (t == Type.Double) {	      o = Operand.newDoubleConstant(Double.parseDouble(tok.getText())); 	    } else {	      System.err.println("Type not correct for floating point constant\n");	      System.exit(-1);	    }	  } 	  | HEX_INTCONSTANT 	  { 	    tok = LT(0);	    s = tok.getText(); 	    boolean signed = false;	    if (s.charAt(0) == 's') {	      signed = true;	      if ((t == Type.Ulong) || (t == Type.Uint)) {		System.err.println("Error:  Unsigned type does not match signed literal");		System.exit(-1);	      }	    }  else {	      if ((t == Type.Long) || (t == Type.Int)) {		System.err.println("Error:  Signed type does not match unsigned literal");		System.exit(-1);	      }	    }	    s = s.substring(3, s.length());	    if ((t == Type.Long) || (t == Type.Ulong)) {	      o = Operand.newLongConstant(Long.parseLong(s)); 	    } else if ((t == Type.Int) || (t == Type.Uint)) {	      o = Operand.newIntConstant(Integer.parseInt(s)); 	    }	  } 	  | TRUE	  {	    try {	      tok = LT(0);	      o = Operand.newBoolean(tok.getText());	    } catch (IllegalOperand e) {	      System.err.println("Error creating boolean operand: " + tok.getText() + " on line: " + tok.getLine());	      throw(e);	    }	  }	  | FALSE	  {	    try {	      tok = LT(0);	      o = Operand.newBoolean(tok.getText());	    } catch (IllegalOperand e) {	      System.err.println("Error creating boolean operand: " + tok.getText() + " on line: " + tok.getLine());	      throw(e);	    }	  }	  | HEX_FPCONSTANT 	  { 	    tok = LT(0); 	    s = tok.getText();	    s = s.substring(2, s.length());	    //System.err.println("FPConstant: " + s);	    if (s.length() <= Convert.ieeeFloatHexStringLength) {	      if (t == Type.Float) {		float f = Convert.ieeeFloatHexStringToFloat(s);		o = Operand.newFloatConstant(f); 	      } else if (t == Type.Double) {		double d = Convert.ieeeDoubleHexStringToDouble(s);		o = Operand.newDoubleConstant(d); 	      } else {		System.err.println("Type not correct for constant\n");		System.exit(-1);	      }	    } else if (s.length() <= Convert.ieeeDoubleHexStringLength) {		if (t == Type.Double) {		  double d = Convert.ieeeDoubleHexStringToDouble(s);		  o = Operand.newDoubleConstant(d); 		} else if (t == Type.Float) {		  float f = Convert.ieeeDoubleHexStringToFloat(s);		  o = Operand.newFloatConstant(f); 		} else System.err.println("Type not correct for constant\n");	    } else System.err.println("Type not correct for constant\n");         }	  ) 	 {	  if (o == null) {	    tok = LT(0);	    System.err.println("Error:  Null operand for variable: " + tok.getText() + " on line: " + tok.getLine());	    System.exit(-1);	  }	   //s = tok.getText(); 	   //System.out.println("value="+s);	 };eol! :	(options { warnWhenFollowAmbig = false; } :	EOL!	)+;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -