Skip to content

Commit

Permalink
add some missing interpreter functions and a test
Browse files Browse the repository at this point in the history
  • Loading branch information
superlopuh committed Jan 8, 2025
1 parent 2fab7c3 commit a8430de
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
39 changes: 39 additions & 0 deletions asl_xdsl/interpreters/asl.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
TerminatorValue,
impl,
impl_callable,
impl_external,
impl_terminator,
register_impls,
)
from xdsl.ir import Operation

from asl_xdsl.dialects import asl

Expand All @@ -32,9 +34,46 @@ def call_func(
else:
return interpreter.run_ssacfg_region(op.body, args, op.sym_name.data)

@impl(asl.CallOp)
def run_call(
self, interpreter: Interpreter, op: asl.CallOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
return interpreter.call_op(op.callee.string_value(), args)

@impl(asl.AddIntOp)
def run_add_int(
self, interpreter: Interpreter, op: asl.AddIntOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
lhs: int
rhs: int
(lhs, rhs) = args
return (lhs + rhs,)

@impl(asl.ConstantIntOp)
def run_constant(
self, interpreter: Interpreter, op: asl.ConstantIntOp, args: PythonValues
) -> PythonValues:
value = op.value
return (value.data,)

# region built-in function implementations

@impl_external("asl_print_int_dec")
def asl_print_int_dec(
self, interpreter: Interpreter, op: Operation, args: PythonValues
) -> PythonValues:
arg: int
(arg,) = args
interpreter.print(arg)
return ()

@impl_external("asl_print_char")
def asl_print_char(
self, interpreter: Interpreter, op: Operation, args: PythonValues
) -> PythonValues:
arg: int
(arg,) = args
interpreter.print(chr(arg))
return ()

# endregion
2 changes: 1 addition & 1 deletion asl_xdsl/tools/asl_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def interpret_target(module: ModuleOp, output: IO[str]):

interpreter = Interpreter(module, file=output)
interpreter.register_implementations(ASLFunctions())
op = interpreter.get_op_for_symbol("main")
op = interpreter.get_op_for_symbol("main.0")
trait = op.get_trait(CallableOpInterface)
assert trait is not None

Expand Down
2 changes: 1 addition & 1 deletion tests/filecheck/exec/exec_simple.asl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: asl-opt --t exec %s | filecheck %s

func main() => integer
func main.0() => integer
begin
return 42;
end;
Expand Down
29 changes: 29 additions & 0 deletions tests/filecheck/exec/t2.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: asl-opt %s --target exec | filecheck %s

// CHECK: 3

asl.func @asl_print_int_dec(!asl.int)
asl.func @asl_print_char(!asl.int)

asl.func @main.0() -> !asl.int {
%0 = asl.constant_int 1 {attr_dict}
%1 = asl.constant_int 2 {attr_dict}
%2 = asl.call @Test.0(%0, %1) : (!asl.int, !asl.int) -> !asl.int
asl.call @asl_print_int_dec (%2) : (!asl.int) -> ()

asl.call @println.0() : () -> ()

%3 = asl.constant_int 0 {attr_dict}
asl.return %3 : !asl.int
}

asl.func @Test.0(%x : !asl.int, %y : !asl.int) -> !asl.int {
%0 = asl.add_int %x, %y : (!asl.int, !asl.int) -> !asl.int
asl.return %0 : !asl.int
}

asl.func @println.0() -> () {
%0 = asl.constant_int 10 {attr_dict}
asl.call @asl_print_char(%0) : (!asl.int) -> ()
asl.return
}

0 comments on commit a8430de

Please sign in to comment.