📄 llvm.g
字号:
/* * LA-CC 05-135 Trident 0.7.1Copyright NoticeCopyright 2006 (c) the Regents of the University of California.This Software was produced under a U.S. Government contract(W-7405-ENG-36) by Los Alamos National Laboratory, which is operatedby the University of California for the U.S. Department of Energy. TheU.S. Government is licensed to use, reproduce, and distribute thisSoftware. Permission is granted to the public to copy and use thisSoftware without charge, provided that this Notice and any statementof authorship are reproduced on all copies. Neither the Government northe University makes any warranty, express or implied, or assumes anyliability or responsibility for the user of this Software. */header{ package fp.parser; import fp.flowgraph.*; import fp.util.*; import java.util.*; import java.lang.reflect.Array; import java.math.BigInteger;}{ import java.util.*; import java.math.BigInteger; import java.io.*; import antlr.collections.AST; import antlr.collections.impl.*; import antlr.debug.misc.*; import antlr.*;}class LlvmLexer extends Lexer;options { k = 4;/* filter = true; *//* analyzerDebug = true; */}tokens {SIGNED = "signed";STATIC = "static";BEGIN = "begin";END = "END";TRUE = "true";FALSE = "false";DECLARE = "declare";GLOBAL = "GLOBAL";CONSTANT = "constant";CONST = "const";INTERNAL = "internal";LINKONCE = "linkonce";WEAK = "weak";APPENDING = "appending";UNINITIALIZED = "uninitialized"; EXTERNAL = "EXT";IMPLEMENTATION = "implementation";ZEROINITIALIZER = "zeroinitializer";DOTDOTDOT = "...";NULLTOK = "null";TO = "to";EXCEPT = "except"; NOT = "not";TARGET = "target";ENDIAN = "endian";POINTERSIZE = "pointersize";LITTLE = "little";BIG = "big";VOLATILE = "volatile";VOID = "void";BOOL = "bool";SBYTE = "sbyte";UBYTE = "ubyte";SHORT = "short";USHORT = "ushort";INT = "int";UINT = "uint";LONG = "long";ULONG = "ulong";FLOAT = "float";DOUBLE = "double";TYPE = "type";LABEL = "label";OPAQUE = "opaque";ADD = "add";SUB = "sub";MUL = "mul";DIV = "div";REM = "rem";AND = "and";OR = "or";XOR = "xor";SETNE = "setne";SETEQ = "seteq";SETLT = "setlt";SETGT = "setgt";SETLE = "setle";SETGE = "setge";PHI = "phi";CALL = "call";CAST = "cast";SELECT = "select";SHL = "shl";SHR = "shr";VANEXT = "vanext";VAARG = "vaarg";RET = "ret";BR = "br";SWITCH = "switch";INVOKE = "invoke";UNWIND = "unwind";TAIL = "tail";MALLOC = "malloc";ALLOCA = "alloca";FREE = "free";LOAD = "load";STORE = "store";GETELEMENTPTR = "getelementptr";VAR = "VAR";FUNCTION = "FUNCTION";BLOCK = "BLOCK";LOOP = "LOOP";NINTEGER;PINTEGER;FPCONSTANT;HEX_INTCONSTANT;HEX_FPCONSTANT;}// Whitespace -- ignoredWS : ( ' ' | '\t' | '\f' ) { _ttype =Token.SKIP; };EOL : ( "\r\n" | '\r' | '\n' ) {newline();};/* Comments start with a ; and go till end of line */SL_COMMENT: ';'(~('\n'|'\r'))* { _ttype = Token.SKIP;};// OperatorsEQUAL : '=';LBRACE : '{';RBRACE : '}';LPAREN : '(';RPAREN : ')';STAR : '*';LBRACKET : '[';RBRACKET : ']';COLON : ':';COMMA : ',';QUES : '?';RARROW : '>';LARROW : '<';HASH : '#';protected LOWER: 'a'..'z';protected UPPER: 'A'..'Z';protected DIGIT: '0'..'9';protected HEXDIGIT: (DIGIT | 'a'..'f' | 'A'..'F');protected IDCHAR: (LOWER|UPPER|'$'|'.'|'_');/* ID: (IDCHAR)+; *//* Variable(Value) identifiers start with a % sign */VARID: '%'! (IDCHAR) (IDCHAR|DIGIT)* { };/* Quoted names can contain any character except " and \ */STRINGCONSTANT: '"' (~('"'|'\\'))* '"' { };protected NUMBER: (DIGIT)+;/* [PN]Integer: match positive and negative literal integer values that * are preceeded by a '%' character. These represent unnamed variable slots. */EPINTEGER: '%' NUMBER;ENINTEGER: '%''-'NUMBER;/* E[PN]Integer: match positive and negative literal integer values *//*PINTEGER: NUMBER;NINTEGER: '-' NUMBER;*//* FPConstant - A Floating point constant. */protected INTEGER: ('+'|'-')? NUMBER;protected EXPONENT: ('e'|'E')(INTEGER);/*FPCONSTANT: INTEGER '.' (DIGIT)*(EXPONENT)?;*//* HexFPConstant - Floating point constant represented in IEEE format as a * hexadecimal number for when exponential notation is not precise enough.HexFPConstant 0x[0-9A-Fa-f]+ *//* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing * it to deal with 64 bit numbers.HexIntConstant [us]0x[0-9A-Fa-f]+ */NUM_CONST: (('-' NUMBER { _ttype = NINTEGER;}) | (NUMBER { _ttype = PINTEGER;})) ('.' (DIGIT)* (EXPONENT)? { _ttype = FPCONSTANT;})? | ('+' NUMBER '.' (DIGIT)* (EXPONENT)? {_ttype = FPCONSTANT;});HEX_CONST: ((('0' 'x' { _ttype = HEX_FPCONSTANT;} | ('u' '0' 'x') {_ttype = HEX_INTCONSTANT;}) | ('s' '0' 'x') {_ttype = HEX_INTCONSTANT;}) (HEXDIGIT)+); DECLVARID: (LOWER | UPPER) (IDCHAR|DIGIT)*;FUNNAME: (LOWER | UPPER | DIGIT)+;{}class LlvmParser extends Parser;options {/* analyzerDebug = true; */ k = 3;}tokens { FLIST; BLIST; ILIST;}module [BlockGraph blockGraph] { Variable ev = null; // external variable GlobalVariable gv = null; }: (eol!)? (ev=extvardecl { if (blockGraph == null) { System.err.println("BlockGraph object required for parsing"); System.exit(-1); } blockGraph.addVariable(ev); } )* (gv=globalvardecl { blockGraph.addVariable(gv); } )* function[blockGraph] (FUNCTION {System.err.println("Warning: multiple functions detected. Only first function is parsed.");})? { }; /*function [BlockGraph blockGraph] { Loop firstLoop = null; BlockNode firstBlock = null; BlockNode blockNode = null; Loop lp = null; Loop parentLoop = null; }: FUNCTION id:DECLVARID { parentLoop = new Loop(id.getText(), Loop.ROOT); blockGraph.setRootLoop(parentLoop); } eol! ((blockNode=block[parentLoop] { if (firstBlock == null) { firstBlock = blockNode; blockGraph.setFirstBlock(blockNode); if (firstLoop == null) { parentLoop.setLoopEntry(blockNode); //System.out.println("Loop entry block is: " + blockNode.getName()); } } blockGraph.addNode(blockNode); parentLoop.addLoopNode(blockNode); //System.out.println("Added block node to loop tree: " + blockNode.getName()); } )| (lp=loop[parentLoop, blockGraph] { if ((firstBlock == null) && (firstLoop == null)) { firstLoop = lp; parentLoop.setLoopEntry(lp); //System.out.println("Loop entry loop is: " + lp.getName()); } parentLoop.addLoopNode(lp); //System.out.println("Added loop to loop tree: " + lp.getName()); } ))* FUNCTION END DECLVARID eol! { try { PhiInfo.patch(); } catch (IllegalOperand e) { System.err.println("Error patching phi instruction."); throw(e); } if (parentLoop != null) { //System.out.println("Loop tree: " + parentLoop.toString()); } };*/arraydecl [Type t] returns [ArrayType at] { Type eltType = t; at = null; }: (LBRACKET num:PINTEGER RBRACKET) (eltType=arraydecl[t])* { at = new ArrayType(eltType, Integer.parseInt(num.getText())); };extvardecl returns [Variable v] { Operand o = null; ArrayType at = null; Type t = null; v = null; }: EXTERNAL VAR (STATIC)? (SIGNED)? t=type dv:DECLVARID (at=arraydecl[t] {t = at;} )? eol! { try { o = Operand.newPrimal(dv.getText()); } catch (IllegalOperand e) { System.err.println("Error creating primal operand: " + dv.getText() + " on line: " + dv.getLine()); throw(e); } v = new Variable(o, t, true); };globalvardecl returns [GlobalVariable v] { Operand o = null; ArrayType at = null; Type t = null; v = null; Object iv = null; }: GLOBAL VAR (STATIC)? (SIGNED)? t=type dv:DECLVARID { //System.out.println("Parsing global var: " + dv.getText()); } (at=arraydecl[t] {t = at;})* (EQUAL iv=initialValue[t])? eol! { try { o = Operand.newPrimal(dv.getText()); } catch (IllegalOperand e) { System.err.println("Error creating primal operand: " + dv.getText() + " on line: " + dv.getLine()); throw(e); } v = new GlobalVariable(o, t, iv); };initialValue [Type t] returns [Object o] { Token tok = null; String s = null; o = null; Object iv = null; Type eltType = null; Object array = null; Object dims = null; int index = 0; int len = 0; LinkedList ll = null; Class cls = null; Integer nextInt = null; int dim = 0; }: (NINTEGER | PINTEGER | FPCONSTANT) { tok = LT(0); //System.out.println("initialValue of: " + tok.getText()); if ((t == Type.Long) || (t == Type.Ulong)) { o = new Long(tok.getText()); } else if ((t == Type.Int) || (t == Type.Uint)) { o = new Integer(tok.getText()); } else if (t == Type.Float) { o = new Float(tok.getText()); } else if (t == Type.Double) { o = new Double(tok.getText()); } } | HEX_INTCONSTANT { tok = LT(0); s = tok.getText(); boolean signed = false; if (s.charAt(0) == 's') { signed = true; } s = s.substring(3, s.length()); if ((t == Type.Long) || (t == Type.Ulong)) { o = new Long(s); } else if ((t == Type.Int) || (t == Type.Uint)) { o = new Integer(s);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -