alive: add parser support for struct types
fixes
MODIFY
ir/instr.cpp
ir/instr.cpp
@@ -236,8 +236,7 @@ void BinOp::print(ostream &os) const {
os << "nuw ";
if (flags & Exact)
os << "exact ";
- os << fmath
- << print_type(getType()) << lhs->getName() << ", " << rhs->getName();
+ os << fmath << *lhs << ", " << rhs->getName();
}
static void div_ub(State &s, const expr &a, const expr &b, const expr &ap,

MODIFY
tests/unit/umul_overflow/const-folds.opt
tests/unit/umul_overflow/const-folds.opt
@@ -42,3 +42,12 @@ Name: overflow
%a = umul_overflow i8 12, -10
%v0 = 136
%v1 = 1
+
+Name: typing
+%X = umul_overflow i8 %a, undef
+%Op = extractvalue {i8, i1} %X, 0
+%Ov = extractvalue {i8, i1} %X, 1
+ =>
+%X = umul_overflow i8 %a, undef
+%Op = 0
+%Ov = 0

MODIFY
tools/alive_lexer.re
tools/alive_lexer.re
@@ -153,23 +153,14 @@ space+ {
return CONSTANT;
}
-"=" {
- return EQUALS;
-}
-
-"," {
- return COMMA;
-}
-
-"=>" {
- return ARROW;
-}
-
[a-zA-Z]+ ":" {
COPY_STR_RTRIM(1);
return LABEL;
}
+"=" { return EQUALS; }
+"," { return COMMA; }
+"=>" { return ARROW; }
"(" { return LPAREN; }
")" { return RPAREN; }
"+" { return PLUS; }
@@ -184,6 +175,8 @@ space+ {
"<u" { return CULT; }
"]" { return RSQBRACKET; }
"[" { return LSQBRACKET; }
+"}" { return RBRACE; }
+"{" { return LBRACE; }
"true" { return TRUE; }
"false" { return FALSE; }

MODIFY
tools/alive_parser.cpp
tools/alive_parser.cpp
@@ -129,8 +129,7 @@ struct tokenizer_t {
}
bool isType() {
- // TODO: support other aggregate types
- return isScalarType() || isVectorType() || isArrayType();
+ return isScalarType() || isVectorType() || isArrayType() || isStructType();
}
bool isScalarType() {
@@ -146,6 +145,10 @@ struct tokenizer_t {
return peek() == ARRAY_TYPE_PREFIX;
}
+ bool isStructType() {
+ return peek() == LBRACE;
+ }
+
private:
token get_new_token() const {
try {
@@ -320,10 +323,9 @@ static Type& get_pointer_type(unsigned address_space_number) {
return *pointer_types[address_space_number].get();
}
-static unsigned vector_num;
static vector<unique_ptr<VectorType>> vector_types;
-static unsigned array_num;
static vector<unique_ptr<ArrayType>> array_types;
+static vector<unique_ptr<StructType>> struct_types;
static Type& parse_scalar_type() {
switch (*tokenizer) {
@@ -358,24 +360,25 @@ static Type& parse_vector_type() {
tokenizer.ensure(CSGT);
return *vector_types.emplace_back(
- make_unique<VectorType>("vty_" + to_string(vector_num++),
+ make_unique<VectorType>("vty_" + to_string(vector_types.size()),
elements, elemTy)).get();
}
static Type& parse_array_type();
+static Type& parse_struct_type();
static Type& parse_type(bool optional = true) {
- if (tokenizer.isScalarType()) {
+ if (tokenizer.isScalarType())
return parse_scalar_type();
- }
- if (tokenizer.isArrayType()) {
+ if (tokenizer.isArrayType())
return parse_array_type();
- }
- if (tokenizer.isVectorType()) {
+ if (tokenizer.isVectorType())
return parse_vector_type();
- }
+
+ if (tokenizer.isStructType())
+ return parse_struct_type();
if (optional)
return get_sym_type();
@@ -392,10 +395,33 @@ static Type& parse_array_type() {
Type &elemTy = parse_type(false);
tokenizer.ensure(RSQBRACKET);
return *array_types.emplace_back(
- make_unique<ArrayType>("aty_" + to_string(array_num++),
+ make_unique<ArrayType>("aty_" + to_string(array_types.size()),
elements, elemTy)).get();
}
+static Type& parse_struct_type() {
+ tokenizer.ensure(LBRACE);
+
+ vector<Type*> tys;
+ vector<bool> padding;
+ do {
+ tys.emplace_back(&parse_type(false));
+ padding.emplace_back(false);
+
+ switch (auto t = *tokenizer) {
+ case COMMA: break;
+ case RBRACE: goto end;
+ default:
+ error("Expected comma or }", t);
+ }
+ } while (true);
+
+end:
+ return *struct_types.emplace_back(
+ make_unique<StructType>("sty_" + to_string(struct_types.size()),
+ move(tys), move(padding))).get();
+}
+
static Type& try_parse_type(Type &default_type) {
if (tokenizer.isType())
return parse_type();
@@ -1188,6 +1214,7 @@ static unique_ptr<Instr> parse_instr(string_view name) {
case FLOAT:
case DOUBLE:
case VECTOR_TYPE_PREFIX:
+ case LBRACE:
case NUM:
case FP_NUM:
case TRUE:

MODIFY
tools/tokens.h
tools/tokens.h
@@ -75,6 +75,7 @@ TOKEN(INSERTELEMENT)
TOKEN(INSERTVALUE)
TOKEN(INT_TYPE)
TOKEN(LABEL)
+TOKEN(LBRACE)
TOKEN(LPAREN)
TOKEN(LSHR)
TOKEN(LSQBRACKET)
@@ -108,6 +109,7 @@ TOKEN(PRE)
TOKEN(PTRTOINT)
TOKEN(REGISTER)
TOKEN(RETURN)
+TOKEN(RBRACE)
TOKEN(RPAREN)
TOKEN(RSQBRACKET)
TOKEN(SADD_OVERFLOW)

alive: add parser support for struct types
fixes #72