lexer/parser: add support for fn call
ADD
tests/unit/call-error.opt
+; ERROR: Target is more poisonous
+%call = call i32 @f(i32 32)
+ =>
+%call = call i32 @f(i32 poison)

ADD
tests/unit/call.opt
+Name: poison->poison
+%call = call i32 @f(i32 poison)
+ =>
+%call = call i32 @f(i32 poison)
+
+Name: poison->val
+%call = call i32 @f(i32 poison)
+ =>
+%call = call i32 @f(i32 42)
+
+Name: val->val
+%call = call i32 @f(i32 42)
+ =>
+%call = call i32 @f(i32 42)
+
+Name: 2 vals
+%call = call i32 @g(i32 42, i32 3)
+ =>
+%call = call i32 @g(i32 42, i32 3)

MODIFY
tools/alive_lexer.re
tools/alive_lexer.re
@@ -123,6 +123,11 @@ re2c:yyfill:check = 0;
return REGISTER;
}
+"@" [a-zA-Z0-9_.]+ {
+ COPY_STR();
+ return GLOBAL_NAME;
+}
+
"C" [0-9]+ {
COPY_STR();
return CONSTANT;
@@ -198,6 +203,7 @@ re2c:yyfill:check = 0;
"ult" { return ULT; }
"uge" { return UGE; }
"ugt" { return UGT; }
+"call" { return CALL; }
"freeze" { return FREEZE; }
"fshl" { return FSHL; }
"fshr" { return FSHR; }

MODIFY
tools/alive_parser.cpp
tools/alive_parser.cpp
@@ -145,6 +145,11 @@ static void parse_name(Transform &t) {
t.name = yylval.str;
}
+static string parse_global_name() {
+ tokenizer.ensure(GLOBAL_NAME);
+ return string(yylval.str);
+}
+
static Value& parse_const_expr(Type &type);
static Predicate* parse_predicate(Predicate *last = nullptr,
@@ -612,6 +617,26 @@ static unique_ptr<Instr> parse_freeze(string_view name) {
return make_unique<Freeze>(ty, string(name), op);
}
+static unique_ptr<Instr> parse_call(string_view name) {
+ // call ty name(ty_1 %op_1, ..., ty_n %op_n)
+ auto &ret_ty = parse_type();
+ auto fn_name = parse_global_name();
+ tokenizer.ensure(LPAREN);
+
+ auto call = make_unique<FnCall>(ret_ty, string(name), move(fn_name));
+ bool first = true;
+
+ while (tokenizer.peek() != RPAREN) {
+ if (!first)
+ tokenizer.ensure(COMMA);
+ first = false;
+ auto &ty = parse_type();
+ call->addArg(parse_operand(ty));
+ }
+ tokenizer.ensure(RPAREN);
+ return call;
+}
+
static unique_ptr<Instr> parse_copyop(string_view name, token t) {
tokenizer.unget(t);
auto &ty = parse_type();
@@ -670,6 +695,8 @@ static unique_ptr<Instr> parse_instr(string_view name) {
return parse_icmp(name);
case FREEZE:
return parse_freeze(name);
+ case CALL:
+ return parse_call(name);
case INT_TYPE:
case NUM:
case TRUE:

MODIFY
tools/tokens.h
tools/tokens.h
@@ -10,6 +10,7 @@ TOKEN(BAND)
TOKEN(BITREVERSE)
TOKEN(BSWAP)
TOKEN(BOR)
+TOKEN(CALL)
TOKEN(COMMA)
TOKEN(CONSTANT)
TOKEN(CEQ)
@@ -34,6 +35,7 @@ TOKEN(FREEZE)
TOKEN(FSHL)
TOKEN(FSHR)
TOKEN(FSUB)
+TOKEN(GLOBAL_NAME)
TOKEN(ICMP)
TOKEN(IDENTIFIER)
TOKEN(INT_TYPE)

lexer/parser: add support for fn call