Skip to content

Commit

Permalink
irgen: Add support for binary addition expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mewmew committed Jun 15, 2016
1 parent 40117c7 commit 6c4c69c
Show file tree
Hide file tree
Showing 25 changed files with 60 additions and 2,113 deletions.
61 changes: 60 additions & 1 deletion irgen/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (m *Module) expr(f *Function, expr ast.Expr) value.Value {
case *ast.BasicLit:
return m.basicLit(f, expr)
case *ast.BinaryExpr:
panic(fmt.Sprintf("support for type %T not yet implemented", expr))
return m.binaryExpr(f, expr)
case *ast.CallExpr:
panic(fmt.Sprintf("support for type %T not yet implemented", expr))
case *ast.Ident:
Expand Down Expand Up @@ -337,6 +337,64 @@ func (m *Module) basicLit(f *Function, n *ast.BasicLit) value.Value {
default:
panic(fmt.Sprintf("support for basic literal kind %v not yet implemented", n.Kind))
}
panic("unreachable")
}

// binaryExpr lowers the given binary expression to LLVM IR, emitting code to f.
func (m *Module) binaryExpr(f *Function, n *ast.BinaryExpr) value.Value {
if n.Op == token.Assign {
panic("support for assignment expression not yet implement")
}
x := m.expr(f, n.X)
y := m.expr(f, n.Y)
switch n.Op {
// +
case token.Add:
inst, err := instruction.NewAdd(x, y)
if err != nil {
panic(fmt.Sprintf("unable to create add instruction; %v", err))
}
// Emit add instruction.
return f.emitLocal("", inst)

// -
case token.Sub:

// *
case token.Mul:

// /
case token.Div:

// <
case token.Lt:

// >
case token.Gt:

// <=
case token.Le:

// >=
case token.Ge:

// !=
case token.Ne:

// ==
case token.Eq:

// &&
case token.Land:

// TODO: Remove comment.
// // =
// case token.Assign:

default:
panic(fmt.Sprintf("support for binary operator %v not yet implemented", n.Op))
}
panic("unreachable")
}

// ident lowers the given identifier to LLVM IR, emitting code to f.
Expand Down Expand Up @@ -409,6 +467,7 @@ func (m *Module) unaryExpr(f *Function, n *ast.UnaryExpr) value.Value {
default:
panic(fmt.Sprintf("support for unary operator %v not yet implemented", n.Op))
}
panic("unreachable")
}

// constExpr converts the given expression to an LLVM IR constant expression.
Expand Down
66 changes: 0 additions & 66 deletions testdata/extra/irgen/binary_expr_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,3 @@ int f() {
int b;
return a + b;
}

int g() {
int a;
int b;
return a - b;
}

int h() {
int a;
int b;
return a * b;
}

int i() {
int a;
int b;
return a / b;
}

int j() {
int a;
int b;
return a < b;
}

int k() {
int a;
int b;
return a > b;
}

int l() {
int a;
int b;
return a <= b;
}

int m() {
int a;
int b;
return a >= b;
}

int n() {
int a;
int b;
return a != b;
}

int o() {
int a;
int b;
return a == b;
}

int p() {
int a;
int b;
return a && b;
}

int q() {
int a;
int b;
return a = b;
}
111 changes: 0 additions & 111 deletions testdata/extra/irgen/binary_expr_add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -7,114 +7,3 @@ define i32 @f() {
%3 = add i32 %1, %2
ret i32 %3
}
define i32 @g() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %a
%2 = load i32, i32* %b
%3 = sub i32 %1, %2
ret i32 %3
}
define i32 @h() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %a
%2 = load i32, i32* %b
%3 = mul i32 %1, %2
ret i32 %3
}
define i32 @i() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %a
%2 = load i32, i32* %b
%3 = sdiv i32 %1, %2
ret i32 %3
}
define i32 @j() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %a
%2 = load i32, i32* %b
%3 = icmp slt i32 %1, %2
%4 = zext i1 %3 to i32
ret i32 %4
}
define i32 @k() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %a
%2 = load i32, i32* %b
%3 = icmp sgt i32 %1, %2
%4 = zext i1 %3 to i32
ret i32 %4
}
define i32 @l() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %a
%2 = load i32, i32* %b
%3 = icmp sle i32 %1, %2
%4 = zext i1 %3 to i32
ret i32 %4
}
define i32 @m() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %a
%2 = load i32, i32* %b
%3 = icmp sge i32 %1, %2
%4 = zext i1 %3 to i32
ret i32 %4
}
define i32 @n() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %a
%2 = load i32, i32* %b
%3 = icmp ne i32 %1, %2
%4 = zext i1 %3 to i32
ret i32 %4
}
define i32 @o() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %a
%2 = load i32, i32* %b
%3 = icmp eq i32 %1, %2
%4 = zext i1 %3 to i32
ret i32 %4
}
define i32 @p() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %a
%2 = icmp ne i32 %1, 0
br i1 %2, label %3, label %6
3:
%4 = load i32, i32* %b
%5 = icmp ne i32 %4, 0
br label %6
6:
%7 = phi i1 [ false, %0 ], [ %5, %3 ]
%8 = zext i1 %7 to i32
ret i32 %8
}
define i32 @q() {
0:
%a = alloca i32
%b = alloca i32
%1 = load i32, i32* %b
store i32 %1, i32* %a
ret i32 %1
}
66 changes: 0 additions & 66 deletions testdata/extra/irgen/binary_expr_assign.c
Original file line number Diff line number Diff line change
@@ -1,70 +1,4 @@
int f() {
int a;
int b;
return a + b;
}

int g() {
int a;
int b;
return a - b;
}

int h() {
int a;
int b;
return a * b;
}

int i() {
int a;
int b;
return a / b;
}

int j() {
int a;
int b;
return a < b;
}

int k() {
int a;
int b;
return a > b;
}

int l() {
int a;
int b;
return a <= b;
}

int m() {
int a;
int b;
return a >= b;
}

int n() {
int a;
int b;
return a != b;
}

int o() {
int a;
int b;
return a == b;
}

int p() {
int a;
int b;
return a && b;
}

int q() {
int a;
int b;
return a = b;
Expand Down
Loading

0 comments on commit 6c4c69c

Please sign in to comment.