aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-16 13:41:42 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-16 13:41:42 +0000
commit006d465c265246fa84e7926f2f0d3fe0ac19b2e2 (patch)
treea4778584645d4886fa38c1c6f2c2f5eb65d832e4
parent20377ab1d402ccc75ecc6af0354a1d726be3d4cd (diff)
Make strings GC objects
-rw-r--r--runtime/src/gc.h8
-rw-r--r--runtime/src/strings.c2
-rw-r--r--runtime/src/strings.h2
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushStringTests]/[method:canHaveMultipleStrings(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected4
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushStringTests]/[method:pushStringIsCompiledToDataAndIndexToData(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenGc.java32
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenStrings.java4
7 files changed, 48 insertions, 6 deletions
diff --git a/runtime/src/gc.h b/runtime/src/gc.h
new file mode 100644
index 00000000..d7f5f9af
--- /dev/null
+++ b/runtime/src/gc.h
@@ -0,0 +1,8 @@
+#ifndef SHED_GC_H
+#define SHED_GC_H
+
+struct ShedGcObjectHeader {
+ struct ShedGcObjectHeader* next;
+};
+
+#endif
diff --git a/runtime/src/strings.c b/runtime/src/strings.c
index c19bb3bd..9fd545f0 100644
--- a/runtime/src/strings.c
+++ b/runtime/src/strings.c
@@ -2,7 +2,7 @@
#include "./strings.h"
ShedString shed__runtime__string_alloc(ShedStringLength length) {
- return shed__runtime__alloc(sizeof(ShedStringLength) + length);
+ return shed__runtime__alloc(sizeof(struct ShedString) + length);
}
ShedString shed__runtime__string_add(ShedString left, ShedString right) {
diff --git a/runtime/src/strings.h b/runtime/src/strings.h
index 3e964c71..d299a098 100644
--- a/runtime/src/strings.h
+++ b/runtime/src/strings.h
@@ -4,10 +4,12 @@
#include <stdint.h>
#include "./patio/patio.h"
+#include "./gc.h"
typedef PatioInt ShedStringLength;
struct ShedString {
+ struct ShedGcObjectHeader gc_object_header;
ShedStringLength length;
uint8_t data[];
};
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushStringTests]/[method:canHaveMultipleStrings(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushStringTests]/[method:canHaveMultipleStrings(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
index e834a6f5..d8ea95b2 100644
--- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushStringTests]/[method:canHaveMultipleStrings(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
+++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushStringTests]/[method:canHaveMultipleStrings(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
@@ -1,7 +1,7 @@
Compilation unit elements:
-(data-immutable "shed__string__0" (struct ((int 5) (array byte ((byte 0x68) (byte 0x65) (byte 0x6c) (byte 0x6c) (byte 0x6f))))))
-(data-immutable "shed__string__1" (struct ((int 5) (array byte ((byte 0x77) (byte 0x6f) (byte 0x72) (byte 0x6c) (byte 0x64))))))
+(data-immutable "shed__string__0" (struct ((int 0) (struct ((int 5) (array byte ((byte 0x68) (byte 0x65) (byte 0x6c) (byte 0x6c) (byte 0x6f))))))))
+(data-immutable "shed__string__1" (struct ((int 0) (struct ((int 5) (array byte ((byte 0x77) (byte 0x6f) (byte 0x72) (byte 0x6c) (byte 0x64))))))))
Locals:
* (val "string__0" int)
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushStringTests]/[method:pushStringIsCompiledToDataAndIndexToData(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushStringTests]/[method:pushStringIsCompiledToDataAndIndexToData(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
index 21ca0baf..04540bea 100644
--- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushStringTests]/[method:pushStringIsCompiledToDataAndIndexToData(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
+++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenPushStringTests]/[method:pushStringIsCompiledToDataAndIndexToData(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
@@ -1,6 +1,6 @@
Compilation unit elements:
-(data-immutable "shed__string__0" (struct ((int 5) (array byte ((byte 0x68) (byte 0x65) (byte 0x6c) (byte 0x6c) (byte 0x6f))))))
+(data-immutable "shed__string__0" (struct ((int 0) (struct ((int 5) (array byte ((byte 0x68) (byte 0x65) (byte 0x6c) (byte 0x6c) (byte 0x6f))))))))
Locals:
* (val "string__0" int)
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenGc.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenGc.java
new file mode 100644
index 00000000..e67554fe
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenGc.java
@@ -0,0 +1,32 @@
+package org.shedlang.compiler.patio;
+
+import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
+import org.shedlang.compiler.patio.ast.types.PatioDataLayouts;
+import org.shedlang.compiler.patio.ast.types.PatioDataValue;
+import org.shedlang.compiler.patio.ast.types.PatioDataValues;
+import org.shedlang.compiler.util.Lists;
+
+import java.util.List;
+
+public class PatioCodeGenGc {
+ private PatioCodeGenGc() {
+ }
+
+ public static PatioDataLayout gcObjectDataLayout(PatioDataLayout valueLayout) {
+ return PatioDataLayouts.struct(List.of(
+ PatioDataLayouts.DATA_POINTER,
+ valueLayout
+ ));
+ }
+
+ public static List<Integer> gcObjectOffset(List<Integer> valueOffset) {
+ return Lists.concat(List.of(1), valueOffset);
+ }
+
+ public static PatioDataValue gcObjectStaticDataValue(PatioDataValue value) {
+ return PatioDataValues.struct(List.of(
+ PatioDataValues.int_(0),
+ value
+ ));
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStrings.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStrings.java
index a0d11ca7..3269057b 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStrings.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStrings.java
@@ -43,10 +43,10 @@ public class PatioCodeGenStrings {
codeUnitsDataValues.add(PatioDataValues.byte_(codeUnit));
}
- var dataValue = PatioDataValues.struct(List.of(
+ var dataValue = PatioCodeGenGc.gcObjectStaticDataValue(PatioDataValues.struct(List.of(
PatioDataValues.int_(codeUnits.length),
PatioDataValues.array(PatioDataLayouts.BYTE, codeUnitsDataValues)
- ));
+ )));
var elementNameResult = context.allocateCompilationUnitElementName(
PatioCodeGenCompilationUnitElementSpace.STRING