From 5058d0fe741657650a9f64909e2d4dbb61e01dc2 Mon Sep 17 00:00:00 2001 From: Alexandre Bique Date: Thu, 29 Jan 2009 13:54:52 +0100 Subject: [PATCH] support for sub, mul, sdiv, srem, xor, or, and --- Makefile | 2 +- src/ast/generator-data.cc | 15 +++++++++++++ src/ast/llvm-generator-visitor.cc | 34 +++++++++++++++++------------- src/ast/llvm-generator-visitor.hh | 8 +++++++ src/ast/simplify-visitor.cc | 4 ++++ src/ast/simplify-visitor.hh | 7 ++++++ src/lang/mugiwara/bison-parser/lexer.l | 13 ++++++++---- src/lang/mugiwara/bison-parser/parser.y | 25 ++++++++++++---------- tests/lang/mugiwara/input/multiple_ops.mgw | 13 ++++++++++++ 9 files changed, 90 insertions(+), 31 deletions(-) create mode 100644 tests/lang/mugiwara/input/multiple_ops.mgw diff --git a/Makefile b/Makefile index 1260472..c17def9 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ check: all clean: make $(MAKEOPT) -C $(BUILDDIR) clean - find . \( -name '*~' \) -exec rm -f {} \; +# find . \( -name '*~' \) -exec rm -f {} \; distclean: rm -rf $(BUILDDIR) config.mk diff --git a/src/ast/generator-data.cc b/src/ast/generator-data.cc index 8a23e84..7407bac 100644 --- a/src/ast/generator-data.cc +++ b/src/ast/generator-data.cc @@ -134,6 +134,21 @@ const ag::Node ag::nodes[] = { } }, + {"LAndExp", "BinaryExp", "Expression `&'", { + {"", ""} + } + }, + + {"LOrExp", "BinaryExp", "Expression `|'", { + {"", ""} + } + }, + + {"LXorExp", "BinaryExp", "Expression `^'", { + {"", ""} + } + }, + {"NumberExp", "Exp", "A number", { {"int", "number"} } diff --git a/src/ast/llvm-generator-visitor.cc b/src/ast/llvm-generator-visitor.cc index 46f30e9..72e4fef 100644 --- a/src/ast/llvm-generator-visitor.cc +++ b/src/ast/llvm-generator-visitor.cc @@ -49,26 +49,30 @@ namespace ast LLVMGeneratorVisitor::visit(const AssignExp & node) { out_ << " %" << node.name << " = "; - if (typeid (*node.value) == typeid (NumberExp) || - typeid (*node.value) == typeid (IdExp)) - { - out_ << "add i32 0, "; - node.value->accept(*this); - } - else - node.value->accept(*this); + node.value->accept(*this); out_ << std::endl; } - void - LLVMGeneratorVisitor::visit(const AddExp & node) - { - out_ << "add i32 "; - node.left->accept(*this); - out_ << ", "; - node.right->accept(*this); +#define GEN_BINARY_EXP(Type, Instr) \ + void \ + LLVMGeneratorVisitor::visit(const Type & node) \ + { \ + out_ << Instr" i32 "; \ + node.left->accept(*this); \ + out_ << ", "; \ + node.right->accept(*this); \ } + GEN_BINARY_EXP(AddExp, "add") + GEN_BINARY_EXP(SubExp, "sub") + GEN_BINARY_EXP(MulExp, "mul") + GEN_BINARY_EXP(DivExp, "sdiv") /// @todo play with udiv and sdiv instructions + GEN_BINARY_EXP(ModExp, "srem") /// @todo play with urem and srem instructions + + GEN_BINARY_EXP(LAndExp, "and") + GEN_BINARY_EXP(LOrExp, "or") + GEN_BINARY_EXP(LXorExp, "xor") + void LLVMGeneratorVisitor::visit(const IdExp & node) { diff --git a/src/ast/llvm-generator-visitor.hh b/src/ast/llvm-generator-visitor.hh index aca2b9a..f14d593 100644 --- a/src/ast/llvm-generator-visitor.hh +++ b/src/ast/llvm-generator-visitor.hh @@ -33,6 +33,14 @@ namespace ast virtual void visit(const AssignExp & node); virtual void visit(const AddExp & node); + virtual void visit(const SubExp & node); + virtual void visit(const MulExp & node); + virtual void visit(const DivExp & node); + virtual void visit(const ModExp & node); + + virtual void visit(const LAndExp & node); + virtual void visit(const LOrExp & node); + virtual void visit(const LXorExp & node); protected: std::ostream & out_; diff --git a/src/ast/simplify-visitor.cc b/src/ast/simplify-visitor.cc index 44c3cf1..a980a97 100644 --- a/src/ast/simplify-visitor.cc +++ b/src/ast/simplify-visitor.cc @@ -106,6 +106,10 @@ namespace ast SIMPLIFY_BINARY_EXP(DivExp) SIMPLIFY_BINARY_EXP(ModExp) + SIMPLIFY_BINARY_EXP(LAndExp) + SIMPLIFY_BINARY_EXP(LOrExp) + SIMPLIFY_BINARY_EXP(LXorExp) + void SimplifyVisitor::visit(AssignExp & node) { diff --git a/src/ast/simplify-visitor.hh b/src/ast/simplify-visitor.hh index e72cd0d..72dcc3c 100644 --- a/src/ast/simplify-visitor.hh +++ b/src/ast/simplify-visitor.hh @@ -32,6 +32,7 @@ namespace ast //virtual void visit(Exp & node); virtual void visit(AssignExp & node); //virtual void visit(BinaryExp & node); + virtual void visit(EqExp & node); virtual void visit(NeqExp & node); virtual void visit(LtExp & node); @@ -43,6 +44,12 @@ namespace ast virtual void visit(MulExp & node); virtual void visit(DivExp & node); virtual void visit(ModExp & node); + + virtual void visit(LAndExp & node); + virtual void visit(LOrExp & node); + virtual void visit(LXorExp & node); + + virtual void visit(NumberExp & node); virtual void visit(IdExp & node); virtual void visit(Block & node); diff --git a/src/lang/mugiwara/bison-parser/lexer.l b/src/lang/mugiwara/bison-parser/lexer.l index f2e2c59..69cf478 100644 --- a/src/lang/mugiwara/bison-parser/lexer.l +++ b/src/lang/mugiwara/bison-parser/lexer.l @@ -29,10 +29,15 @@ \{ return LBR; \} return RBR; -\+ return OP_ADD; -- return OP_SUB; -\* return OP_MUL; -\/ return OP_DIV; +\+ return ADD; +- return SUB; +\* return MUL; +\/ return DIV; +% return MOD; + +\| return LOR; +& return LAND; +\^ return LXOR; const return CONST; diff --git a/src/lang/mugiwara/bison-parser/parser.y b/src/lang/mugiwara/bison-parser/parser.y index b52bad5..9b4a031 100644 --- a/src/lang/mugiwara/bison-parser/parser.y +++ b/src/lang/mugiwara/bison-parser/parser.y @@ -60,7 +60,8 @@ int yywrap(yyscan_t /*yyscanner*/) %token CONST %token EQ SEMICOL DOT %token EQEQ LT LTEQ GT GTEQ NEQ -%token OP_ADD OP_SUB OP_DIV OP_MUL +%token ADD SUB DIV MUL MOD +%token LOR LAND LXOR %token LPAR RPAR LBR RBR %type decls @@ -72,12 +73,8 @@ int yywrap(yyscan_t /*yyscanner*/) %type block %type instrs %type instr -%type exp -%type assign_exp -%type eq_exp -%type aexp -%type mexp -%type uexp +%type exp assign_exp eq_exp +%type aexp mexp bexp uexp %type is_const %% @@ -163,12 +160,18 @@ eq_exp: aexp EQEQ aexp { MAKE_BINARY_EXP(EqExp, $$, $1, $3); } | aexp GTEQ aexp { MAKE_BINARY_EXP(GtEqExp, $$, $1, $3); } | aexp { $$ = $1; }; -aexp: aexp OP_ADD mexp { MAKE_BINARY_EXP(AddExp, $$, $1, $3); } -| aexp OP_SUB mexp { MAKE_BINARY_EXP(SubExp, $$, $1, $3); } +aexp: aexp ADD mexp { MAKE_BINARY_EXP(AddExp, $$, $1, $3); } +| aexp SUB mexp { MAKE_BINARY_EXP(SubExp, $$, $1, $3); } | mexp { assert($1); $$ = $1; }; -mexp: mexp OP_MUL uexp { MAKE_BINARY_EXP(MulExp, $$, $1, $3); } -| mexp OP_DIV uexp { MAKE_BINARY_EXP(DivExp, $$, $1, $3); } +mexp: mexp MUL bexp { MAKE_BINARY_EXP(MulExp, $$, $1, $3); } +| mexp DIV bexp { MAKE_BINARY_EXP(DivExp, $$, $1, $3); } +| mexp MOD bexp { MAKE_BINARY_EXP(ModExp, $$, $1, $3); } +| bexp { assert($1); $$ = $1; }; + +bexp: bexp LOR uexp { MAKE_BINARY_EXP(LOrExp, $$, $1, $3); } +| bexp LAND uexp { MAKE_BINARY_EXP(LAndExp, $$, $1, $3); } +| bexp LXOR uexp { MAKE_BINARY_EXP(LXorExp, $$, $1, $3); } | uexp { assert($1); $$ = $1; }; uexp: NUMBER { diff --git a/tests/lang/mugiwara/input/multiple_ops.mgw b/tests/lang/mugiwara/input/multiple_ops.mgw new file mode 100644 index 0000000..3a42d3b --- /dev/null +++ b/tests/lang/mugiwara/input/multiple_ops.mgw @@ -0,0 +1,13 @@ +int main() +{ + int a; + + a = 1 + 3; + a = a - 4; + a = a * 9; + a = a / 4; + a = a % 34; + a = a ^ 1; + a = a & 432592; + a = a | 3452; +} -- 2.11.4.GIT