Skip to content

Commit

Permalink
sulong: start ANTLR parser
Browse files Browse the repository at this point in the history
  • Loading branch information
zapster authored and rschatz committed Oct 24, 2019
1 parent 7cb6f92 commit 48a6d58
Show file tree
Hide file tree
Showing 4 changed files with 837 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.oracle.truffle.api.Scope;
import com.oracle.truffle.api.TruffleLanguage.InlineParsingRequest;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.llvm.runtime.LLVMLanguage;
import com.oracle.truffle.llvm.runtime.debug.debugexpr.parser.DebugExprException;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode;
Expand All @@ -53,7 +54,7 @@ private static final class BailoutErrorListener extends BaseErrorListener {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
String location = "-- line " + line + " col " + (charPositionInLine + 1) + ": ";
throw DebugExprException.create(null, String.format("ASM error in %s:\n%s%s", snippet, location, msg));
throw DebugExprException.create(null, String.format("Debug Expression error in %s:\n%s%s", snippet, location, msg));
}
}

Expand Down Expand Up @@ -83,14 +84,19 @@ public LLVMExpressionNode parse() throws DebugExprException {
BailoutErrorListener listener = new BailoutErrorListener(asmSnippet);
lexer.addErrorListener(listener);
parser.addErrorListener(listener);
parser.r();
parser.debugExpr();
// parser.snippet = asmSnippet;
// parser.factory = new AsmFactory(language, argTypes, asmFlags, retType, retTypes, retOffsets);
// parser.inline_assembly();
// if (parser.root == null) {
// throw new IllegalStateException("no roots produced by inline assembly snippet");
// }
// return parser.root;
return null;
return new LLVMExpressionNode() {
@Override
public Object executeGeneric(VirtualFrame frame) {
throw DebugExprException.create(this, "Not Yet Implemented: " + asmSnippet);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,315 @@ grammar DebugExpression;
}


r : 'hello' ID;
ID : [a-z]+ ;
fragment
LETTER : [a-zA-Z_];
//DIGIT : '0123456789';
//CR : '\r';
//LF : '\n';
//SINGLECOMMA : '\'';
//QUOTE : '"';


// Add token declarations here.
// Example:
//IDENT : LETTER (LETTER | DIGIT)*;
IDENT : LETTER;
//NUMBER : DIGIT+;
//FLOATNUMBER : DIGIT+ '.' DIGIT+ ( [eE] [+-] DIGIT+ )?;
//CHARCONST : SINGLECOMMA (LETTER|DIGIT) SINGLECOMMA;

WS : [ \t\r\n]+ -> skip ;

// PRODUCTIONS

//DebugExpr (. DebugExpressionPair p=null; .)
//=
//Expr<out p> (. if(errors.count==0) astRoot =p.getNode(); .)
//.

debugExpr : IDENT;
//debugExpr : expr;

//
//PrimExpr<out DebugExpressionPair p> (. p=null; .)
//=
//ident (. p = NF.createVarNode(t.val);.)
//|
//number (. p = NF.createIntegerConstant(Integer.parseInt(t.val)); .)
//|
//floatnumber (. p = NF.createFloatConstant(Float.parseFloat(t.val)); .)
//|
//charConst (. p = NF.createCharacterConstant(t.val); .)
//|
//"(" Expr<out p> ")"
//.
primExpr : IDENT // (. p = NF.createVarNode(t.val);.)
| NUMBER // (. p = NF.createIntegerConstant(Integer.parseInt(t.val)); .)
| FLOATNUMBER // (. p = NF.createFloatConstant(Float.parseFloat(t.val)); .)
| CHARCONST // (. p = NF.createCharacterConstant(t.val); .)
;
//
//Designator<out DebugExpressionPair p> (. DebugExpressionPair idxPair=null; List<DebugExpressionPair> l; .)
//=
//PrimExpr<out p>
//{
// "[" Expr<out idxPair> "]" (. p = NF.createArrayElement(p, idxPair); .)
// |
// ActPars<out l> (. p = NF.createFunctionCall(p, l); .)
// |
// "." ident (. p = NF.createObjectMember(p, t.val); .)
// |
// "->" ident (. p = NF.createObjectPointerMember(p, t.val); .)
//}
//.
designator : primExpr;
//
//ActPars<out List l> (. DebugExpressionPair p1=null, p2=null; l = new LinkedList<DebugExpressionPair>(); .)
//=
//"("
//[
// Expr<out p1> (. l.add(p1); .)
//
// {
// "," Expr<out p2> (. l.add(p2); .)
//
// }
//
//]
//")"
//.
//
//UnaryExpr<out DebugExpressionPair p> (. p=null; char kind='\0'; DebugExprType typeP=null;.)
//=
//Designator<out p>
//|
//UnaryOp<out kind> CastExpr<out p> (. p = NF.createUnaryOpNode(p, kind); .)
//|
//"sizeof" "(" DType<out typeP> ")" (. p=NF.createSizeofNode(typeP); .)
//.
unaryExpr : designator;
//
//UnaryOp<out char kind> (. kind='\0'; .)
//=
//(
// "*"
// |
// "+"
// |
// "-"
// |
// "~"
// |
// "!"
//)
// (. kind = t.val.charAt(0); .)
//.
//CastExpr<out DebugExpressionPair p> (. DebugExprType typeP=null; DebugExprTypeofNode typeNode=null; .)
//=
//[
// IF (IsCast()) "("
// (
// DType<out typeP>
// |
// "typeof" "(" ident (. typeNode = NF.createTypeofNode(t.val); .)
// ")"
// )
// ")"
//]
//UnaryExpr<out p> (. if(typeP!=null) { p = NF.createCastIfNecessary(p, typeP); }
// if(typeNode!=null) {p = NF.createPointerCastNode(p, typeNode);} .)
//.
castExpr : unaryExpr;
//
//MultExpr<out DebugExpressionPair p> (. DebugExpressionPair p1=null; .)
//=
//CastExpr<out p>
//{
// "*" CastExpr<out p1> (. p = NF.createArithmeticOp(ArithmeticOperation.MUL, p, p1); .)
//
// |
// "/" CastExpr<out p1> (. p = NF.createDivNode(p, p1); .)
//
// |
// "%" CastExpr<out p1> (. p = NF.createRemNode(p, p1); .)
//
//}
//.
multExpr : castExpr;
//AddExpr<out DebugExpressionPair p> (. DebugExpressionPair p1=null; .)
//=
//MultExpr<out p>
//{
// "+" MultExpr<out p1> (. p = NF.createArithmeticOp(ArithmeticOperation.ADD, p, p1); .)
//
// |
// "-" MultExpr<out p1> (.p = NF.createArithmeticOp(ArithmeticOperation.SUB, p, p1); .)
//
//}
//.
addExpr : multExpr;
//ShiftExpr<out DebugExpressionPair p> (. DebugExpressionPair p1=null; .)
//=
//AddExpr<out p>
//{
// "<<" AddExpr<out p1> (.p = NF.createShiftLeft(p, p1); .)
//
// |
// ">>" AddExpr<out p1> (.p = NF.createShiftRight(p, p1); .)
//
//}
//.
shiftExpr : addExpr;
//RelExpr<out DebugExpressionPair p> (. DebugExpressionPair p1=null; .)
//=
//ShiftExpr<out p>
//{
// "<" ShiftExpr<out p1> (. p = NF.createCompareNode(p, CompareKind.LT, p1); .)
//
// |
// ">" ShiftExpr<out p1> (. p = NF.createCompareNode(p, CompareKind.GT, p1); .)
//
// |
// "<=" ShiftExpr<out p1> (. p = NF.createCompareNode(p, CompareKind.LE, p1); .)
//
// |
// ">=" ShiftExpr<out p1> (. p = NF.createCompareNode(p, CompareKind.GE, p1); .)
//
//}
//.
relExpr : shiftExpr;
//EqExpr<out DebugExpressionPair p> (. DebugExpressionPair p1=null; .)
//=
//RelExpr<out p>
//{
// "==" RelExpr<out p1> (. p = NF.createCompareNode(p, CompareKind.EQ, p1); .)
//
// |
// "!=" RelExpr<out p1> (. p = NF.createCompareNode(p, CompareKind.NE, p1); .)
//
//}
//.
eqExpr : relExpr;
//AndExpr<out DebugExpressionPair p> (. DebugExpressionPair p1=null; .)
//=
//EqExpr<out p>
//{
// "&" EqExpr<out p1> (. p = NF.createArithmeticOp(ArithmeticOperation.AND, p, p1); .)
//
//}
//.
andExpr : eqExpr;
//
//XorExpr<out DebugExpressionPair p> (. DebugExpressionPair p1=null; .)
//=
//AndExpr<out p>
//{
// "^" AndExpr<out p1> (. p = NF.createArithmeticOp(ArithmeticOperation.XOR, p, p1); .)
//
//}
//.
xorExpr : andExpr;
//
//OrExpr<out DebugExpressionPair p> (. DebugExpressionPair p1=null; .)
//=
//XorExpr<out p>
//{
// "|" XorExpr<out p1> (. p = NF.createArithmeticOp(ArithmeticOperation.OR, p, p1); .)
//
//}
//.
orExpr : xorExpr;
//
//LogAndExpr<out DebugExpressionPair p> (. DebugExpressionPair p1=null; .)
//=
//OrExpr<out p>
//{
// "&&" OrExpr<out p1> (. p= NF.createLogicalAndNode(p, p1); .)
//
//}
//.
logAndExpr : orExpr;
//
//LogOrExpr<out DebugExpressionPair p> (. DebugExpressionPair p1=null; .)
//=
//LogAndExpr<out p>
//{
// "||" LogAndExpr<out p> (. p= NF.createLogicalOrNode(p, p1); .)
//
//}
//.
logOrExpr : logAndExpr;
//
//Expr<out DebugExpressionPair p> (. DebugExpressionPair pThen=null, pElse=null; .)
//=
//LogOrExpr<out p>
//[
// "?" Expr<out pThen> ":" Expr<out pElse> (. p = NF.createTernaryNode(p, pThen, pElse);.)
//
//]
//.
expr : logOrExpr;

//
//DType<out DebugExprType ty>
//=
//BaseType<out ty>
//{
// "*" (. ty = ty.createPointer(); .)
//
//}
//{
// "["
//
// (
// number (. ty = ty.createArrayType(Integer.parseInt(t.val)); .)
// |
// (. ty = ty.createArrayType(-1); .)
// )
// "]"
//}
//.
//BaseType<out DebugExprType ty> (. ty=null; boolean signed=false;.)
//=
//"(" DType<out ty> ")"
//|
//"void" (. ty = DebugExprType.getVoidType(); .)
//|
//
//(
// "signed" (. signed = true; .)
//
// |
// "unsigned" (. signed = false; .)
//
//)
//[
// "char" (. ty = DebugExprType.getIntType(8, signed); .)
//
// |
// "short" (. ty = DebugExprType.getIntType(16, signed); .)
//
// |
// "int" (. ty = DebugExprType.getIntType(32, signed); .)
//
// |
// "long" (. ty = DebugExprType.getIntType(64, signed); .)
//
//]
//|
//"char" (. ty = DebugExprType.getIntType(8, false);.)
//|
//"short" (. ty = DebugExprType.getIntType(16, true);.)
//|
//"int" (. ty = DebugExprType.getIntType(32, true);.)
//|
//"long" (. ty = DebugExprType.getIntType(64, true);.)
//[
// "double" (. ty = DebugExprType.getFloatType(128); .)
//
//]
//|
//"float" (. ty = DebugExprType.getFloatType(32);.)
//|
//"double" (. ty = DebugExprType.getFloatType(64);.)
//.
Loading

0 comments on commit 48a6d58

Please sign in to comment.