diff --git a/asl_xdsl/interpreters/asl.py b/asl_xdsl/interpreters/asl.py index 49db157..414b4af 100644 --- a/asl_xdsl/interpreters/asl.py +++ b/asl_xdsl/interpreters/asl.py @@ -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 @@ -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 diff --git a/asl_xdsl/tools/asl_opt.py b/asl_xdsl/tools/asl_opt.py index f952a0c..3ede26b 100644 --- a/asl_xdsl/tools/asl_opt.py +++ b/asl_xdsl/tools/asl_opt.py @@ -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 diff --git a/tests/filecheck/exec/exec_simple.asl b/tests/filecheck/exec/exec_simple.asl index bd1b2e6..efad5d4 100644 --- a/tests/filecheck/exec/exec_simple.asl +++ b/tests/filecheck/exec/exec_simple.asl @@ -1,6 +1,6 @@ // RUN: asl-opt --t exec %s | filecheck %s -func main() => integer +func main.0() => integer begin return 42; end; diff --git a/tests/filecheck/exec/t2.mlir b/tests/filecheck/exec/t2.mlir new file mode 100644 index 0000000..846946f --- /dev/null +++ b/tests/filecheck/exec/t2.mlir @@ -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 +}