ssa: TestUnOp

This commit is contained in:
xushiwei
2024-04-20 17:31:49 +08:00
parent 96f9226923
commit 905c05e099
6 changed files with 138 additions and 413 deletions

View File

@@ -149,7 +149,8 @@ func isPredOp(op token.Token) bool {
return op >= predOpBase && op <= predOpLast
}
// op:
// The BinOp instruction yields the result of binary operation (x op y).
// op can be:
// ADD SUB MUL QUO REM + - * / %
// AND OR XOR SHL SHR AND_NOT & | ^ << >> &^
// EQL NEQ LSS LEQ GTR GEQ == != < <= < >=
@@ -195,6 +196,27 @@ func (b Builder) BinOp(op token.Token, x, y Expr) Expr {
panic("todo")
}
// The UnOp instruction yields the result of (op x).
// ARROW is channel receive.
// MUL is pointer indirection (load).
// XOR is bitwise complement.
// SUB is negation.
// NOT is logical negation.
func (b Builder) UnOp(op token.Token, x Expr) Expr {
switch op {
case token.MUL:
return b.Load(x)
}
panic("todo")
}
// Load returns the value at the pointer ptr.
func (b Builder) Load(ptr Expr) Expr {
elem := ptr.t.(*types.Pointer).Elem()
telem := b.prog.llvmType(elem)
return Expr{llvm.CreateLoad(b.impl, telem.ll, ptr.impl), telem}
}
// -----------------------------------------------------------------------------
func (b Builder) Call(fn Expr, args ...Expr) (ret Expr) {

View File

@@ -228,3 +228,25 @@ define i64 @fn(i64 %0, double %1) {
}
`)
}
func TestUnOp(t *testing.T) {
prog := NewProgram(nil)
pkg := prog.NewPackage("bar", "foo/bar")
params := types.NewTuple(
types.NewVar(0, nil, "p", types.NewPointer(types.Typ[types.Int])),
)
rets := types.NewTuple(types.NewVar(0, nil, "", types.Typ[types.Int]))
sig := types.NewSignatureType(nil, nil, nil, params, rets, false)
fn := pkg.NewFunc("fn", sig)
b := fn.MakeBody("")
ret := b.UnOp(token.MUL, fn.Param(0))
b.Return(ret)
assertPkg(t, pkg, `; ModuleID = 'foo/bar'
source_filename = "foo/bar"
define i64 @fn(ptr %0) {
%2 = load i64, ptr %0, align 4
ret i64 %2
}
`)
}