diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-15 15:47:58 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-15 15:47:58 +0000 |
| commit | 3ed2ca517aa5f2424f238817844d8a8eb98e64c2 (patch) | |
| tree | 8dc70ba88ce76b22f2539adc932deab0c00ac0c1 | |
| parent | 549ec911f67484c875a09d4b5b8a9a4fc0ecc806 (diff) | |
Implement int ops in runtime
15 files changed, 250 insertions, 645 deletions
diff --git a/runtime/meson.build b/runtime/meson.build index dbef403a..0dbb5277 100644 --- a/runtime/meson.build +++ b/runtime/meson.build @@ -3,6 +3,7 @@ static_library( 'shed', 'src/effects.c', 'src/exceptions.c', + 'src/ints.c', 'src/shed.c', 'src/strings.c', 'src/namespaces/Shed/Builtins.c', diff --git a/runtime/src/ints.c b/runtime/src/ints.c new file mode 100644 index 00000000..32bc15bb --- /dev/null +++ b/runtime/src/ints.c @@ -0,0 +1,43 @@ +#include <stdbool.h> + +#include "./shed.h" + +struct ShedInt { + PatioInt value; +}; + +ShedObject shed__runtime__int_create(PatioInt value) { + struct ShedInt* result = shed__runtime__alloc(sizeof(struct ShedInt)); + result->value = value; + return (ShedObject) result; +} + +PatioInt shed__runtime__int_value(ShedObject object) { + return ((struct ShedInt*) object)->value; +} + +ShedObject shed__runtime__int_add(ShedObject left, ShedObject right) { + PatioInt left_value = shed__runtime__int_value(left); + PatioInt right_value = shed__runtime__int_value(right); + PatioInt result_value = left_value + right_value; + return shed__runtime__int_create(result_value); +} + +ShedObject shed__runtime__int_sub(ShedObject left, ShedObject right) { + PatioInt left_value = shed__runtime__int_value(left); + PatioInt right_value = shed__runtime__int_value(right); + PatioInt result_value = left_value - right_value; + return shed__runtime__int_create(result_value); +} + +bool shed__runtime__int_equals(ShedObject left, ShedObject right) { + PatioInt left_value = shed__runtime__int_value(left); + PatioInt right_value = shed__runtime__int_value(right); + return left_value == right_value; +} + +bool shed__runtime__int_not_equal(ShedObject left, ShedObject right) { + PatioInt left_value = shed__runtime__int_value(left); + PatioInt right_value = shed__runtime__int_value(right); + return left_value != right_value; +} diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 0388fdf5..acc0a844 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenDefineFunctionTests]/[method:positionalAndNamedParamsAreDefined(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,10 +1,16 @@ Compilation unit elements: ( + function-decl + "shed__runtime__int_add" + params=((param "left" int) (param "right" int)) + return-type=int +) +( function "shed__function__f__0" visibility=private - locals=((param "closurePointer__0" data-ptr) (param "local__param1" int) (param "local__param2" int) (param "local__paramA" int) (param "local__paramB" int) (param "local__paramC" int) (val "local__a" int) (val "local__b" int) (val "binaryOpResult__0" int) (val "intPointer__0" data-ptr) (val "intBinaryOpLeftValue__0" int) (val "intPointer__1" data-ptr) (val "intBinaryOpLeftValue__1" int) (val "intBinaryOpResultValue__0" int) (val "intPointer__2" data-ptr) (val "binaryOpResult__1" int) (val "intPointer__3" data-ptr) (val "intBinaryOpLeftValue__2" int) (val "intPointer__4" data-ptr) (val "intBinaryOpLeftValue__3" int) (val "intBinaryOpResultValue__1" int) (val "intPointer__5" data-ptr)) + locals=((param "closurePointer__0" data-ptr) (param "local__param1" int) (param "local__param2" int) (param "local__paramA" int) (param "local__paramB" int) (param "local__paramC" int) (val "local__a" int) (val "local__b" int) (val "binaryOpResult__0" int) (val "binaryOpResult__1" int)) return-type=int body=( ( @@ -21,58 +27,8 @@ Compilation unit elements: layout=(struct (function-ptr (struct (int int)))) offset=(1 1) ) - (int-to-data-ptr (local "intPointer__0") (local "local__a")) - ( - memory-load - (local "intBinaryOpLeftValue__0") - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - ) - (int-to-data-ptr (local "intPointer__1") (local "local__param1")) - ( - memory-load - (local "intBinaryOpLeftValue__1") - pointer=(local "intPointer__1") - layout=(struct (int)) - offset=(0) - ) - (int-add (local "intBinaryOpResultValue__0") (local "intBinaryOpLeftValue__0") (local "intBinaryOpLeftValue__1")) - (alloc-heap (local "intPointer__2") (struct (int))) - ( - memory-store - pointer=(local "intPointer__2") - layout=(struct (int)) - offset=(0) - value=(local "intBinaryOpResultValue__0") - ) - (data-ptr-to-int (local "binaryOpResult__0") (local "intPointer__2")) - (int-to-data-ptr (local "intPointer__3") (local "binaryOpResult__0")) - ( - memory-load - (local "intBinaryOpLeftValue__2") - pointer=(local "intPointer__3") - layout=(struct (int)) - offset=(0) - ) - (int-to-data-ptr (local "intPointer__4") (local "local__paramA")) - ( - memory-load - (local "intBinaryOpLeftValue__3") - pointer=(local "intPointer__4") - layout=(struct (int)) - offset=(0) - ) - (int-add (local "intBinaryOpResultValue__1") (local "intBinaryOpLeftValue__2") (local "intBinaryOpLeftValue__3")) - (alloc-heap (local "intPointer__5") (struct (int))) - ( - memory-store - pointer=(local "intPointer__5") - layout=(struct (int)) - offset=(0) - value=(local "intBinaryOpResultValue__1") - ) - (data-ptr-to-int (local "binaryOpResult__1") (local "intPointer__5")) + (call (local "binaryOpResult__0") "shed__runtime__int_add" ((local "local__a") (local "local__param1"))) + (call (local "binaryOpResult__1") "shed__runtime__int_add" ((local "binaryOpResult__0") (local "local__paramA"))) (return (local "binaryOpResult__1")) ) ) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithMultipleResults(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithMultipleResults(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 1dce9179..26b88216 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithMultipleResults(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithMultipleResults(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,5 +1,11 @@ Compilation unit elements: +( + function-decl + "shed__runtime__int_create" + params=((param "value" int)) + return-type=int +) Locals: * (val "tmp__conditionInt__0" int) @@ -7,13 +13,9 @@ Locals: * (val "tmp__0" int) * (val "tmp__1" int) * (val "int__0" int) - * (val "intPointer__0" data-ptr) * (val "int__1" int) - * (val "intPointer__1" data-ptr) * (val "int__2" int) - * (val "intPointer__2" data-ptr) * (val "int__3" int) - * (val "intPointer__3" data-ptr) Instructions: @@ -27,48 +29,16 @@ Instructions: ( block ( - (alloc-heap (local "intPointer__0") (struct (int))) - ( - memory-store - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - value=(int 1) - ) - (data-ptr-to-int (local "int__0") (local "intPointer__0")) - (alloc-heap (local "intPointer__1") (struct (int))) - ( - memory-store - pointer=(local "intPointer__1") - layout=(struct (int)) - offset=(0) - value=(int 2) - ) - (data-ptr-to-int (local "int__1") (local "intPointer__1")) + (call (local "int__0") "shed__runtime__int_create" ((int 1))) + (call (local "int__1") "shed__runtime__int_create" ((int 2))) ) results=((local "int__0") (local "int__1")) ) ( block ( - (alloc-heap (local "intPointer__2") (struct (int))) - ( - memory-store - pointer=(local "intPointer__2") - layout=(struct (int)) - offset=(0) - value=(int 101) - ) - (data-ptr-to-int (local "int__2") (local "intPointer__2")) - (alloc-heap (local "intPointer__3") (struct (int))) - ( - memory-store - pointer=(local "intPointer__3") - layout=(struct (int)) - offset=(0) - value=(int 102) - ) - (data-ptr-to-int (local "int__3") (local "intPointer__3")) + (call (local "int__2") "shed__runtime__int_create" ((int 101))) + (call (local "int__3") "shed__runtime__int_create" ((int 102))) ) results=((local "int__2") (local "int__3")) ) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithNoResults(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithNoResults(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 3aeb727c..e19dd75b 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithNoResults(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithNoResults(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,13 +1,17 @@ Compilation unit elements: +( + function-decl + "shed__runtime__int_create" + params=((param "value" int)) + return-type=int +) Locals: * (val "tmp__conditionInt__0" int) * (val "tmp__conditionBool__0" bool) * (val "int__0" int) - * (val "intPointer__0" data-ptr) * (val "int__1" int) - * (val "intPointer__1" data-ptr) Instructions: @@ -21,30 +25,14 @@ Instructions: ( block ( - (alloc-heap (local "intPointer__0") (struct (int))) - ( - memory-store - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - value=(int 1) - ) - (data-ptr-to-int (local "int__0") (local "intPointer__0")) + (call (local "int__0") "shed__runtime__int_create" ((int 1))) ) results=() ) ( block ( - (alloc-heap (local "intPointer__1") (struct (int))) - ( - memory-store - pointer=(local "intPointer__1") - layout=(struct (int)) - offset=(0) - value=(int 101) - ) - (data-ptr-to-int (local "int__1") (local "intPointer__1")) + (call (local "int__1") "shed__runtime__int_create" ((int 101))) ) results=() ) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 1e61b1a6..e8ed6200 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIfTests]/[method:canGenerateCodeForIfWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,32 +1,28 @@ Compilation unit elements: +( + function-decl + "shed__runtime__int_create" + params=((param "value" int)) + return-type=int +) +( + function-decl + "shed__runtime__int_add" + params=((param "left" int) (param "right" int)) + return-type=int +) Locals: * (val "tmp__conditionInt__0" int) * (val "tmp__conditionBool__0" bool) * (val "tmp__0" int) * (val "int__0" int) - * (val "intPointer__0" data-ptr) * (val "int__1" int) - * (val "intPointer__1" data-ptr) * (val "binaryOpResult__0" int) - * (val "intPointer__2" data-ptr) - * (val "intBinaryOpLeftValue__0" int) - * (val "intPointer__3" data-ptr) - * (val "intBinaryOpLeftValue__1" int) - * (val "intBinaryOpResultValue__0" int) - * (val "intPointer__4" data-ptr) * (val "int__2" int) - * (val "intPointer__5" data-ptr) * (val "int__3" int) - * (val "intPointer__6" data-ptr) * (val "binaryOpResult__1" int) - * (val "intPointer__7" data-ptr) - * (val "intBinaryOpLeftValue__2" int) - * (val "intPointer__8" data-ptr) - * (val "intBinaryOpLeftValue__3" int) - * (val "intBinaryOpResultValue__1" int) - * (val "intPointer__9" data-ptr) Instructions: @@ -40,100 +36,18 @@ Instructions: ( block ( - (alloc-heap (local "intPointer__0") (struct (int))) - ( - memory-store - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - value=(int 1) - ) - (data-ptr-to-int (local "int__0") (local "intPointer__0")) - (alloc-heap (local "intPointer__1") (struct (int))) - ( - memory-store - pointer=(local "intPointer__1") - layout=(struct (int)) - offset=(0) - value=(int 2) - ) - (data-ptr-to-int (local "int__1") (local "intPointer__1")) - (int-to-data-ptr (local "intPointer__2") (local "int__0")) - ( - memory-load - (local "intBinaryOpLeftValue__0") - pointer=(local "intPointer__2") - layout=(struct (int)) - offset=(0) - ) - (int-to-data-ptr (local "intPointer__3") (local "int__1")) - ( - memory-load - (local "intBinaryOpLeftValue__1") - pointer=(local "intPointer__3") - layout=(struct (int)) - offset=(0) - ) - (int-add (local "intBinaryOpResultValue__0") (local "intBinaryOpLeftValue__0") (local "intBinaryOpLeftValue__1")) - (alloc-heap (local "intPointer__4") (struct (int))) - ( - memory-store - pointer=(local "intPointer__4") - layout=(struct (int)) - offset=(0) - value=(local "intBinaryOpResultValue__0") - ) - (data-ptr-to-int (local "binaryOpResult__0") (local "intPointer__4")) + (call (local "int__0") "shed__runtime__int_create" ((int 1))) + (call (local "int__1") "shed__runtime__int_create" ((int 2))) + (call (local "binaryOpResult__0") "shed__runtime__int_add" ((local "int__0") (local "int__1"))) ) results=((local "binaryOpResult__0")) ) ( block ( - (alloc-heap (local "intPointer__5") (struct (int))) - ( - memory-store - pointer=(local "intPointer__5") - layout=(struct (int)) - offset=(0) - value=(int 101) - ) - (data-ptr-to-int (local "int__2") (local "intPointer__5")) - (alloc-heap (local "intPointer__6") (struct (int))) - ( - memory-store - pointer=(local "intPointer__6") - layout=(struct (int)) - offset=(0) - value=(int 102) - ) - (data-ptr-to-int (local "int__3") (local "intPointer__6")) - (int-to-data-ptr (local "intPointer__7") (local "int__2")) - ( - memory-load - (local "intBinaryOpLeftValue__2") - pointer=(local "intPointer__7") - layout=(struct (int)) - offset=(0) - ) - (int-to-data-ptr (local "intPointer__8") (local "int__3")) - ( - memory-load - (local "intBinaryOpLeftValue__3") - pointer=(local "intPointer__8") - layout=(struct (int)) - offset=(0) - ) - (int-add (local "intBinaryOpResultValue__1") (local "intBinaryOpLeftValue__2") (local "intBinaryOpLeftValue__3")) - (alloc-heap (local "intPointer__9") (struct (int))) - ( - memory-store - pointer=(local "intPointer__9") - layout=(struct (int)) - offset=(0) - value=(local "intBinaryOpResultValue__1") - ) - (data-ptr-to-int (local "binaryOpResult__1") (local "intPointer__9")) + (call (local "int__2") "shed__runtime__int_create" ((int 101))) + (call (local "int__3") "shed__runtime__int_create" ((int 102))) + (call (local "binaryOpResult__1") "shed__runtime__int_add" ((local "int__2") (local "int__3"))) ) results=((local "binaryOpResult__1")) ) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntAddTests]/[method:canGenerateCodeForIntAdd(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntAddTests]/[method:canGenerateCodeForIntAdd(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 74c72b1b..7a188f28 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntAddTests]/[method:canGenerateCodeForIntAdd(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntAddTests]/[method:canGenerateCodeForIntAdd(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,44 +1,19 @@ Compilation unit elements: +( + function-decl + "shed__runtime__int_add" + params=((param "left" int) (param "right" int)) + return-type=int +) Locals: * (val "binaryOpResult__0" int) - * (val "intPointer__0" data-ptr) - * (val "intBinaryOpLeftValue__0" int) - * (val "intPointer__1" data-ptr) - * (val "intBinaryOpLeftValue__1" int) - * (val "intBinaryOpResultValue__0" int) - * (val "intPointer__2" data-ptr) Instructions: ( - (int-to-data-ptr (local "intPointer__0") (local "b")) - ( - memory-load - (local "intBinaryOpLeftValue__0") - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - ) - (int-to-data-ptr (local "intPointer__1") (local "c")) - ( - memory-load - (local "intBinaryOpLeftValue__1") - pointer=(local "intPointer__1") - layout=(struct (int)) - offset=(0) - ) - (int-add (local "intBinaryOpResultValue__0") (local "intBinaryOpLeftValue__0") (local "intBinaryOpLeftValue__1")) - (alloc-heap (local "intPointer__2") (struct (int))) - ( - memory-store - pointer=(local "intPointer__2") - layout=(struct (int)) - offset=(0) - value=(local "intBinaryOpResultValue__0") - ) - (data-ptr-to-int (local "binaryOpResult__0") (local "intPointer__2")) + (call (local "binaryOpResult__0") "shed__runtime__int_add" ((local "b") (local "c"))) ) Temporary stack: diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntEqualsTests]/[method:canGenerateCodeForIntEquals(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntEqualsTests]/[method:canGenerateCodeForIntEquals(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 964c884b..17417a37 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntEqualsTests]/[method:canGenerateCodeForIntEquals(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntEqualsTests]/[method:canGenerateCodeForIntEquals(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,34 +1,20 @@ Compilation unit elements: +( + function-decl + "shed__runtime__int_equals" + params=((param "left" int) (param "right" int)) + return-type=bool +) Locals: * (val "binaryOpResult__0" int) * (val "tmp__0" int) - * (val "intPointer__0" data-ptr) - * (val "intBinaryOpLeftValue__0" int) - * (val "intPointer__1" data-ptr) - * (val "intBinaryOpLeftValue__1" int) Instructions: ( - (int-to-data-ptr (local "intPointer__0") (local "b")) - ( - memory-load - (local "intBinaryOpLeftValue__0") - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - ) - (int-to-data-ptr (local "intPointer__1") (local "c")) - ( - memory-load - (local "intBinaryOpLeftValue__1") - pointer=(local "intPointer__1") - layout=(struct (int)) - offset=(0) - ) - (int-eq (local "tmp__0") (local "intBinaryOpLeftValue__0") (local "intBinaryOpLeftValue__1")) + (call (local "tmp__0") "shed__runtime__int_equals" ((local "b") (local "c"))) (bool-to-int (local "binaryOpResult__0") (local "tmp__0")) ) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntNotEqualTests]/[method:canGenerateCodeForIntNotEqual(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntNotEqualTests]/[method:canGenerateCodeForIntNotEqual(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index c935f1c2..c9d0f655 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntNotEqualTests]/[method:canGenerateCodeForIntNotEqual(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntNotEqualTests]/[method:canGenerateCodeForIntNotEqual(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,34 +1,20 @@ Compilation unit elements: +( + function-decl + "shed__runtime__int_not_equal" + params=((param "left" int) (param "right" int)) + return-type=bool +) Locals: * (val "binaryOpResult__0" int) * (val "tmp__0" int) - * (val "intPointer__0" data-ptr) - * (val "intBinaryOpLeftValue__0" int) - * (val "intPointer__1" data-ptr) - * (val "intBinaryOpLeftValue__1" int) Instructions: ( - (int-to-data-ptr (local "intPointer__0") (local "b")) - ( - memory-load - (local "intBinaryOpLeftValue__0") - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - ) - (int-to-data-ptr (local "intPointer__1") (local "c")) - ( - memory-load - (local "intBinaryOpLeftValue__1") - pointer=(local "intPointer__1") - layout=(struct (int)) - offset=(0) - ) - (int-ne (local "tmp__0") (local "intBinaryOpLeftValue__0") (local "intBinaryOpLeftValue__1")) + (call (local "tmp__0") "shed__runtime__int_not_equal" ((local "b") (local "c"))) (bool-to-int (local "binaryOpResult__0") (local "tmp__0")) ) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntSubTests]/[method:canGenerateCodeForIntSub(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntSubTests]/[method:canGenerateCodeForIntSub(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index dcaff51f..f34f9280 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntSubTests]/[method:canGenerateCodeForIntSub(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntSubTests]/[method:canGenerateCodeForIntSub(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,44 +1,19 @@ Compilation unit elements: +( + function-decl + "shed__runtime__int_sub" + params=((param "left" int) (param "right" int)) + return-type=int +) Locals: * (val "binaryOpResult__0" int) - * (val "intPointer__0" data-ptr) - * (val "intBinaryOpLeftValue__0" int) - * (val "intPointer__1" data-ptr) - * (val "intBinaryOpLeftValue__1" int) - * (val "intBinaryOpResultValue__0" int) - * (val "intPointer__2" data-ptr) Instructions: ( - (int-to-data-ptr (local "intPointer__0") (local "b")) - ( - memory-load - (local "intBinaryOpLeftValue__0") - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - ) - (int-to-data-ptr (local "intPointer__1") (local "c")) - ( - memory-load - (local "intBinaryOpLeftValue__1") - pointer=(local "intPointer__1") - layout=(struct (int)) - offset=(0) - ) - (int-sub (local "intBinaryOpResultValue__0") (local "intBinaryOpLeftValue__0") (local "intBinaryOpLeftValue__1")) - (alloc-heap (local "intPointer__2") (struct (int))) - ( - memory-store - pointer=(local "intPointer__2") - layout=(struct (int)) - offset=(0) - value=(local "intBinaryOpResultValue__0") - ) - (data-ptr-to-int (local "binaryOpResult__0") (local "intPointer__2")) + (call (local "binaryOpResult__0") "shed__runtime__int_sub" ((local "b") (local "c"))) ) Temporary stack: diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopTests]/[method:canGenerateCodeForLoop(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopTests]/[method:canGenerateCodeForLoop(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 476d643a..cc33a70a 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopTests]/[method:canGenerateCodeForLoop(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopTests]/[method:canGenerateCodeForLoop(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,9 +1,14 @@ Compilation unit elements: +( + function-decl + "shed__runtime__int_create" + params=((param "value" int)) + return-type=int +) Locals: * (val "int__0" int) - * (val "intPointer__0" data-ptr) Instructions: @@ -11,15 +16,7 @@ Instructions: ( loop ( - (alloc-heap (local "intPointer__0") (struct (int))) - ( - memory-store - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - value=(int 1) - ) - (data-ptr-to-int (local "int__0") (local "intPointer__0")) + (call (local "int__0") "shed__runtime__int_create" ((int 1))) ) ) ) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushIntTests]/[method:pushIntInstructionGeneratesIntGetInstruction(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushIntTests]/[method:pushIntInstructionGeneratesIntGetInstruction(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 524c5179..24502c39 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushIntTests]/[method:pushIntInstructionGeneratesIntGetInstruction(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushIntTests]/[method:pushIntInstructionGeneratesIntGetInstruction(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,22 +1,19 @@ Compilation unit elements: +( + function-decl + "shed__runtime__int_create" + params=((param "value" int)) + return-type=int +) Locals: * (val "int__0" int) - * (val "intPointer__0" data-ptr) Instructions: ( - (alloc-heap (local "intPointer__0") (struct (int))) - ( - memory-store - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - value=(int 42) - ) - (data-ptr-to-int (local "int__0") (local "intPointer__0")) + (call (local "int__0") "shed__runtime__int_create" ((int 42))) ) Temporary stack: diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 031ddf07..7b853588 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,32 +1,28 @@ Compilation unit elements: +( + function-decl + "shed__runtime__int_create" + params=((param "value" int)) + return-type=int +) +( + function-decl + "shed__runtime__int_add" + params=((param "left" int) (param "right" int)) + return-type=int +) Locals: * (val "tmp__0" int) * (val "switchTag__0" int) * (val "structValuePointer__0" data-ptr) * (val "int__0" int) - * (val "intPointer__0" data-ptr) * (val "int__1" int) - * (val "intPointer__1" data-ptr) * (val "binaryOpResult__0" int) - * (val "intPointer__2" data-ptr) - * (val "intBinaryOpLeftValue__0" int) - * (val "intPointer__3" data-ptr) - * (val "intBinaryOpLeftValue__1" int) - * (val "intBinaryOpResultValue__0" int) - * (val "intPointer__4" data-ptr) * (val "int__2" int) - * (val "intPointer__5" data-ptr) * (val "int__3" int) - * (val "intPointer__6" data-ptr) * (val "binaryOpResult__1" int) - * (val "intPointer__7" data-ptr) - * (val "intBinaryOpLeftValue__2" int) - * (val "intPointer__8" data-ptr) - * (val "intBinaryOpLeftValue__3" int) - * (val "intBinaryOpResultValue__1" int) - * (val "intPointer__9" data-ptr) Instructions: @@ -50,50 +46,9 @@ Instructions: ( block ( - (alloc-heap (local "intPointer__0") (struct (int))) - ( - memory-store - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - value=(int 1) - ) - (data-ptr-to-int (local "int__0") (local "intPointer__0")) - (alloc-heap (local "intPointer__1") (struct (int))) - ( - memory-store - pointer=(local "intPointer__1") - layout=(struct (int)) - offset=(0) - value=(int 2) - ) - (data-ptr-to-int (local "int__1") (local "intPointer__1")) - (int-to-data-ptr (local "intPointer__2") (local "int__0")) - ( - memory-load - (local "intBinaryOpLeftValue__0") - pointer=(local "intPointer__2") - layout=(struct (int)) - offset=(0) - ) - (int-to-data-ptr (local "intPointer__3") (local "int__1")) - ( - memory-load - (local "intBinaryOpLeftValue__1") - pointer=(local "intPointer__3") - layout=(struct (int)) - offset=(0) - ) - (int-add (local "intBinaryOpResultValue__0") (local "intBinaryOpLeftValue__0") (local "intBinaryOpLeftValue__1")) - (alloc-heap (local "intPointer__4") (struct (int))) - ( - memory-store - pointer=(local "intPointer__4") - layout=(struct (int)) - offset=(0) - value=(local "intBinaryOpResultValue__0") - ) - (data-ptr-to-int (local "binaryOpResult__0") (local "intPointer__4")) + (call (local "int__0") "shed__runtime__int_create" ((int 1))) + (call (local "int__1") "shed__runtime__int_create" ((int 2))) + (call (local "binaryOpResult__0") "shed__runtime__int_add" ((local "int__0") (local "int__1"))) ) results=((local "binaryOpResult__0")) ) @@ -104,50 +59,9 @@ Instructions: ( block ( - (alloc-heap (local "intPointer__5") (struct (int))) - ( - memory-store - pointer=(local "intPointer__5") - layout=(struct (int)) - offset=(0) - value=(int 101) - ) - (data-ptr-to-int (local "int__2") (local "intPointer__5")) - (alloc-heap (local "intPointer__6") (struct (int))) - ( - memory-store - pointer=(local "intPointer__6") - layout=(struct (int)) - offset=(0) - value=(int 102) - ) - (data-ptr-to-int (local "int__3") (local "intPointer__6")) - (int-to-data-ptr (local "intPointer__7") (local "int__2")) - ( - memory-load - (local "intBinaryOpLeftValue__2") - pointer=(local "intPointer__7") - layout=(struct (int)) - offset=(0) - ) - (int-to-data-ptr (local "intPointer__8") (local "int__3")) - ( - memory-load - (local "intBinaryOpLeftValue__3") - pointer=(local "intPointer__8") - layout=(struct (int)) - offset=(0) - ) - (int-add (local "intBinaryOpResultValue__1") (local "intBinaryOpLeftValue__2") (local "intBinaryOpLeftValue__3")) - (alloc-heap (local "intPointer__9") (struct (int))) - ( - memory-store - pointer=(local "intPointer__9") - layout=(struct (int)) - offset=(0) - value=(local "intBinaryOpResultValue__1") - ) - (data-ptr-to-int (local "binaryOpResult__1") (local "intPointer__9")) + (call (local "int__2") "shed__runtime__int_create" ((int 101))) + (call (local "int__3") "shed__runtime__int_create" ((int 102))) + (call (local "binaryOpResult__1") "shed__runtime__int_add" ((local "int__2") (local "int__3"))) ) results=((local "binaryOpResult__1")) ) diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTryTests]/[method:canGenerateCodeForTry(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTryTests]/[method:canGenerateCodeForTry(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index d41b1648..e0d7c0f6 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTryTests]/[method:canGenerateCodeForTry(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTryTests]/[method:canGenerateCodeForTry(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -18,12 +18,17 @@ Compilation unit elements: params=() return-type=void ) +( + function-decl + "shed__runtime__int_create" + params=((param "value" int)) + return-type=int +) Locals: * (val "tmp__0" int) * (val "effectHandler__0" data-ptr) * (val "int__0" int) - * (val "intPointer__0" data-ptr) * (val "tmp__1" int) Instructions: @@ -52,15 +57,7 @@ Instructions: ( block ( - (alloc-heap (local "intPointer__0") (struct (int))) - ( - memory-store - pointer=(local "intPointer__0") - layout=(struct (int)) - offset=(0) - value=(int 42) - ) - (data-ptr-to-int (local "int__0") (local "intPointer__0")) + (call (local "int__0") "shed__runtime__int_create" ((int 42))) (call _ "shed__runtime__effects_pop_handler" ()) ) results=((local "int__0")) diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java index 0178033f..b4b480ab 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java @@ -1,25 +1,71 @@ package org.shedlang.compiler.patio; -import org.shedlang.compiler.patio.ast.instructions.*; +import org.shedlang.compiler.patio.ast.PatioFunctionDeclaration; +import org.shedlang.compiler.patio.ast.PatioLocal; +import org.shedlang.compiler.patio.ast.instructions.PatioCall; import org.shedlang.compiler.patio.ast.operands.PatioInt; import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; import org.shedlang.compiler.patio.ast.operands.PatioOperand; -import org.shedlang.compiler.patio.ast.types.PatioDataLayout; -import org.shedlang.compiler.patio.ast.types.PatioDataLayouts; import org.shedlang.compiler.patio.ast.types.PatioType; import org.shedlang.compiler.patio.ast.types.PatioTypes; import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.sources.Sources; import java.math.BigInteger; import java.util.List; +import java.util.Optional; public class PatioCodeGenInts { - public static final PatioType INT_TYPE = PatioCodeGenTypes.VALUE_TYPE; - - public static final PatioDataLayout INT_DATA_LAYOUT = - PatioDataLayouts.struct(List.of(PatioDataLayouts.INT)); + public static final PatioFunctionDeclaration INT_CREATE_DECLARATION = new PatioFunctionDeclaration( + "shed__runtime__int_create", + List.of( + PatioLocal.param("value", PatioTypes.INT) + ), + PatioCodeGenTypes.VALUE_TYPE, + Sources.RUNTIME + ); + + public static final PatioFunctionDeclaration INT_ADD_DECLARATION = new PatioFunctionDeclaration( + "shed__runtime__int_add", + List.of( + PatioLocal.param("left", PatioCodeGenTypes.VALUE_TYPE), + PatioLocal.param("right", PatioCodeGenTypes.VALUE_TYPE) + ), + PatioCodeGenTypes.VALUE_TYPE, + Sources.RUNTIME + ); + + public static final PatioFunctionDeclaration INT_SUB_DECLARATION = new PatioFunctionDeclaration( + "shed__runtime__int_sub", + List.of( + PatioLocal.param("left", PatioCodeGenTypes.VALUE_TYPE), + PatioLocal.param("right", PatioCodeGenTypes.VALUE_TYPE) + ), + PatioCodeGenTypes.VALUE_TYPE, + Sources.RUNTIME + ); + + public static final PatioFunctionDeclaration INT_EQUALS_DECLARATION = new PatioFunctionDeclaration( + "shed__runtime__int_equals", + List.of( + PatioLocal.param("left", PatioCodeGenTypes.VALUE_TYPE), + PatioLocal.param("right", PatioCodeGenTypes.VALUE_TYPE) + ), + PatioTypes.BOOL, + Sources.RUNTIME + ); + + public static final PatioFunctionDeclaration INT_NOT_EQUAL_DECLARATION = new PatioFunctionDeclaration( + "shed__runtime__int_not_equal", + List.of( + PatioLocal.param("left", PatioCodeGenTypes.VALUE_TYPE), + PatioLocal.param("right", PatioCodeGenTypes.VALUE_TYPE) + ), + PatioTypes.BOOL, + Sources.RUNTIME + ); - public static final List<Integer> VALUE_OFFSET = List.of(0); + public static final PatioType INT_TYPE = PatioCodeGenTypes.VALUE_TYPE; private PatioCodeGenInts() { } @@ -30,12 +76,14 @@ public class PatioCodeGenInts { Source source, PatioCodeGenContext context ) { - return store( - target, - new PatioInt(value.intValueExact()), - source, - context - ); + return context + .ensureCompilationUnitElement(INT_CREATE_DECLARATION) + .addInstruction(new PatioCall( + Optional.of(target), + INT_CREATE_DECLARATION.name(), + List.of(new PatioInt(value.intValueExact())), + source + )); } public static PatioCodeGenContext add( @@ -45,14 +93,14 @@ public class PatioCodeGenInts { Source source, PatioCodeGenContext context ) { - return binaryOpToInt( - PatioIntAdd::new, - target, - left, - right, - source, - context - ); + return context + .ensureCompilationUnitElement(INT_ADD_DECLARATION) + .addInstruction(new PatioCall( + Optional.of(target), + INT_ADD_DECLARATION.name(), + List.of(left, right), + source + )); } public static PatioCodeGenContext sub( @@ -62,14 +110,14 @@ public class PatioCodeGenInts { Source source, PatioCodeGenContext context ) { - return binaryOpToInt( - PatioIntSub::new, - target, - left, - right, - source, - context - ); + return context + .ensureCompilationUnitElement(INT_SUB_DECLARATION) + .addInstruction(new PatioCall( + Optional.of(target), + INT_SUB_DECLARATION.name(), + List.of(left, right), + source + )); } public static PatioCodeGenContext equals( @@ -79,14 +127,14 @@ public class PatioCodeGenInts { Source source, PatioCodeGenContext context ) { - return binaryOpToBool( - PatioIntEquals::new, - target, - left, - right, - source, - context - ); + return context + .ensureCompilationUnitElement(INT_EQUALS_DECLARATION) + .addInstruction(new PatioCall( + Optional.of(target), + INT_EQUALS_DECLARATION.name(), + List.of(left, right), + source + )); } public static PatioCodeGenContext notEqual( @@ -96,155 +144,13 @@ public class PatioCodeGenInts { Source source, PatioCodeGenContext context ) { - return binaryOpToBool( - PatioIntNotEqual::new, - target, - left, - right, - source, - context - ); - } - - private interface BinaryInstruction { - PatioInstruction create( - PatioLocalRef target, - PatioOperand left, - PatioOperand right, - Source source - ); - } - - private static PatioCodeGenContext binaryOpToBool( - BinaryInstruction instruction, - PatioLocalRef target, - PatioOperand left, - PatioOperand right, - Source source, - PatioCodeGenContext context - ) { - var leftValueResult = loadIntValue(left, source, context); - var leftValue = leftValueResult.value(); - context = leftValueResult.context(); - - var rightValueResult = loadIntValue(right, source, context); - var rightValue = rightValueResult.value(); - context = rightValueResult.context(); - - context = context.addInstruction(instruction.create( - target, - leftValue, - rightValue, - source - )); - - return context; - } - - private static PatioCodeGenContext binaryOpToInt( - BinaryInstruction instruction, - PatioLocalRef target, - PatioOperand left, - PatioOperand right, - Source source, - PatioCodeGenContext context - ) { - var leftValueResult = loadIntValue(left, source, context); - var leftValue = leftValueResult.value(); - context = leftValueResult.context(); - - var rightValueResult = loadIntValue(right, source, context); - var rightValue = rightValueResult.value(); - context = rightValueResult.context(); - - var targetValueResult = context.allocateVal( - PatioCodeGenLocalSpace.INT_BINARY_OP_RESULT_VALUE, - PatioTypes.INT - ); - var targetValue = targetValueResult.value(); - context = targetValueResult.context(); - - context = context.addInstruction(instruction.create( - targetValue, - leftValue, - rightValue, - source - )); - - context = store(target, targetValue, source, context); - - return context; - } - - private static PatioCodeGenResult<PatioOperand> loadIntValue( - PatioOperand genericValue, - Source source, - PatioCodeGenContext context - ) { - var pointerResult = context.allocateVal( - PatioCodeGenLocalSpace.INT_POINTER, - PatioTypes.DATA_POINTER - ); - var pointer = pointerResult.value(); - context = pointerResult.context(); - - context = context.addInstruction(new PatioIntToDataPtr( - pointer, - genericValue, - source - )); - - var valueResult = context.allocateVal( - PatioCodeGenLocalSpace.INT_BINARY_OP_LEFT_VALUE, - PatioTypes.INT - ); - var value = valueResult.value(); - context = valueResult.context(); - - context = context.addInstruction(new PatioMemoryLoad( - value, - pointer, - INT_DATA_LAYOUT, - VALUE_OFFSET, - source - )); - - return context.toResult(value); - } - - private static PatioCodeGenContext store( - PatioLocalRef target, - PatioOperand value, - Source source, - PatioCodeGenContext context - ) { - var targetPointerResult = context.allocateVal( - PatioCodeGenLocalSpace.INT_POINTER, - PatioTypes.DATA_POINTER - ); - var targetPointer = targetPointerResult.value(); - context = targetPointerResult.context(); - - context = context.addInstruction(new PatioAllocHeap( - targetPointer, - INT_DATA_LAYOUT, - source - )); - - context = context.addInstruction(new PatioMemoryStore( - targetPointer, - INT_DATA_LAYOUT, - VALUE_OFFSET, - value, - source - )); - - context = context.addInstruction(new PatioDataPtrToInt( - target, - targetPointer, - source - )); - - return context; + return context + .ensureCompilationUnitElement(INT_NOT_EQUAL_DECLARATION) + .addInstruction(new PatioCall( + Optional.of(target), + INT_NOT_EQUAL_DECLARATION.name(), + List.of(left, right), + source + )); } } |
