aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--patio-to-cranelift/src/codegen/mod.rs32
-rw-r--r--patio-to-cranelift/src/patio/flatbuffers.rs26
-rw-r--r--patio-to-cranelift/src/patio/instructions.rs12
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java39
4 files changed, 101 insertions, 8 deletions
diff --git a/patio-to-cranelift/src/codegen/mod.rs b/patio-to-cranelift/src/codegen/mod.rs
index 1a383a7c..20c20874 100644
--- a/patio-to-cranelift/src/codegen/mod.rs
+++ b/patio-to-cranelift/src/codegen/mod.rs
@@ -15,10 +15,10 @@ use crate::{
PatioLocal, PatioLocalType, PatioVisibility,
},
instructions::{
- PatioBlock, PatioBoolToInt, PatioCall, PatioCallPointer, PatioIf, PatioInstruction,
- PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool,
- PatioLocalStore, PatioMemoryLoad, PatioMemoryStore, PatioResume, PatioReturn,
- PatioReturnVoid, PatioVarLoad, PatioVarStore,
+ PatioBlock, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf,
+ PatioInstruction, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub,
+ PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioMemoryLoad, PatioMemoryStore,
+ PatioResume, PatioReturn, PatioReturnVoid, PatioVarLoad, PatioVarStore,
},
operands::{PatioLocalRef, PatioOperand},
types::PatioType,
@@ -242,6 +242,10 @@ fn compile_instruction(instruction: &PatioInstruction, context: &mut CompilerFun
compile_call_pointer(call_pointer, context);
}
+ PatioInstruction::DataPtrToInt(data_ptr_to_int) => {
+ compile_data_ptr_to_int(data_ptr_to_int, context);
+ }
+
PatioInstruction::If(if_) => {
compile_if(if_, context);
}
@@ -266,6 +270,10 @@ fn compile_instruction(instruction: &PatioInstruction, context: &mut CompilerFun
compile_int_to_bool(int_to_bool, context);
}
+ PatioInstruction::IntToDataPtr(int_to_data_ptr) => {
+ compile_int_to_data_ptr(int_to_data_ptr, context);
+ }
+
PatioInstruction::LocalStore(local_store) => {
compile_local_store(local_store, context);
}
@@ -376,6 +384,14 @@ fn compile_call_pointer(
def_destination(&call_pointer.destination, cranelift_value, context);
}
+fn compile_data_ptr_to_int(
+ data_ptr_to_int: &PatioDataPtrToInt,
+ context: &mut CompilerFunctionContext<'_>,
+) {
+ let cranelift_value = compile_operand(&data_ptr_to_int.value, context);
+ def_destination(&data_ptr_to_int.destination, cranelift_value, context);
+}
+
fn compile_if(if_: &PatioIf, context: &mut CompilerFunctionContext<'_>) {
let cranelift_condition = compile_operand(&if_.condition, context);
let cranelift_then_block = context.function_builder.create_block();
@@ -492,6 +508,14 @@ fn compile_int_to_bool(int_to_bool: &PatioIntToBool, context: &mut CompilerFunct
def_destination(&int_to_bool.destination, cranelift_result, context);
}
+fn compile_int_to_data_ptr(
+ int_to_data_ptr: &PatioIntToDataPtr,
+ context: &mut CompilerFunctionContext<'_>,
+) {
+ let cranelift_value = compile_operand(&int_to_data_ptr.value, context);
+ def_destination(&int_to_data_ptr.destination, cranelift_value, context);
+}
+
fn compile_local_store(local_store: &PatioLocalStore, context: &mut CompilerFunctionContext<'_>) {
let cranelift_value = compile_operand(&local_store.value, context);
def_destination(&local_store.destination, cranelift_value, context);
diff --git a/patio-to-cranelift/src/patio/flatbuffers.rs b/patio-to-cranelift/src/patio/flatbuffers.rs
index c219d823..ad667963 100644
--- a/patio-to-cranelift/src/patio/flatbuffers.rs
+++ b/patio-to-cranelift/src/patio/flatbuffers.rs
@@ -6,10 +6,10 @@ use crate::{
PatioLocal, PatioLocalType, PatioVisibility,
},
instructions::{
- PatioBlock, PatioBoolToInt, PatioCall, PatioCallPointer, PatioIf, PatioInstruction,
- PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool,
- PatioLocalStore, PatioMemoryLoad, PatioMemoryStore, PatioResume, PatioReturn,
- PatioReturnVoid, PatioVarLoad, PatioVarStore,
+ PatioBlock, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf,
+ PatioInstruction, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub,
+ PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioMemoryLoad, PatioMemoryStore,
+ PatioResume, PatioReturn, PatioReturnVoid, PatioVarLoad, PatioVarStore,
},
operands::{PatioLocalRef, PatioOperand},
types::{PatioDataLayout, PatioType},
@@ -174,6 +174,15 @@ impl From<patio_generated::patio::PatioInstruction<'_>> for PatioInstruction {
})
}
+ patio_generated::patio::PatioInstructionValue::PatioDataPtrToInt => {
+ let data_ptr_to_int = value.value_as_patio_data_ptr_to_int().unwrap();
+
+ Self::DataPtrToInt(PatioDataPtrToInt {
+ destination: data_ptr_to_int.destination().into(),
+ value: data_ptr_to_int.value().into(),
+ })
+ }
+
patio_generated::patio::PatioInstructionValue::PatioIf => {
let if_ = value.value_as_patio_if().unwrap();
@@ -234,6 +243,15 @@ impl From<patio_generated::patio::PatioInstruction<'_>> for PatioInstruction {
})
}
+ patio_generated::patio::PatioInstructionValue::PatioIntToDataPtr => {
+ let int_to_data_ptr = value.value_as_patio_int_to_data_ptr().unwrap();
+
+ Self::IntToDataPtr(PatioIntToDataPtr {
+ destination: int_to_data_ptr.destination().into(),
+ value: int_to_data_ptr.value().into(),
+ })
+ }
+
patio_generated::patio::PatioInstructionValue::PatioLocalStore => {
let local_store = value.value_as_patio_local_store().unwrap();
diff --git a/patio-to-cranelift/src/patio/instructions.rs b/patio-to-cranelift/src/patio/instructions.rs
index 1cba2953..908a0779 100644
--- a/patio-to-cranelift/src/patio/instructions.rs
+++ b/patio-to-cranelift/src/patio/instructions.rs
@@ -8,12 +8,14 @@ pub(crate) enum PatioInstruction {
BoolToInt(PatioBoolToInt),
Call(PatioCall),
CallPointer(PatioCallPointer),
+ DataPtrToInt(PatioDataPtrToInt),
If(PatioIf),
IntAdd(PatioIntAdd),
IntEquals(PatioIntEquals),
IntNotEqual(PatioIntNotEqual),
IntSub(PatioIntSub),
IntToBool(PatioIntToBool),
+ IntToDataPtr(PatioIntToDataPtr),
LocalStore(PatioLocalStore),
MemoryLoad(PatioMemoryLoad),
MemoryStore(PatioMemoryStore),
@@ -46,6 +48,11 @@ pub(crate) struct PatioCallPointer {
pub(crate) args: Vec<PatioOperand>,
}
+pub(crate) struct PatioDataPtrToInt {
+ pub(crate) destination: PatioLocalRef,
+ pub(crate) value: PatioOperand,
+}
+
pub(crate) struct PatioIf {
pub(crate) destinations: Vec<PatioLocalRef>,
pub(crate) condition: PatioOperand,
@@ -82,6 +89,11 @@ pub(crate) struct PatioIntToBool {
pub(crate) value: PatioOperand,
}
+pub(crate) struct PatioIntToDataPtr {
+ pub(crate) destination: PatioLocalRef,
+ pub(crate) value: PatioOperand,
+}
+
pub(crate) struct PatioLocalStore {
pub(crate) destination: PatioLocalRef,
pub(crate) value: PatioOperand,
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
index fa8dfb08..9fc97792 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
@@ -205,6 +205,45 @@ public abstract class PatioExampleTests {
}
@Test
+ public void canConvertBetweenDataPtrAndInt() throws IOException, InterruptedException {
+ var patioDataElement = new PatioDataElementImmutableDefinition(
+ "answer",
+ PatioVisibility.PRIVATE,
+ PatioDataValues.int_(42),
+ Sources.NULL
+ );
+ var patioFunction = PatioFunctionDefinition.builder()
+ .withName(FUNCTION_NAME)
+ .withReturnType(PatioTypes.VOID)
+ .addLocal(Patio.val("x", PatioTypes.INT))
+ .addLocal(Patio.val("ptr_int", PatioTypes.INT))
+ .addLocal(Patio.val("ptr", PatioTypes.DATA_PTR))
+ .addBodyInstruction(Patio.dataPtrToInt(Patio.localRef("ptr_int"), Patio.dataPtrConst("answer")))
+ .addBodyInstruction(Patio.intToDataPtr(Patio.localRef("ptr"), Patio.localRef("ptr_int")))
+ .addBodyInstruction(Patio.memoryLoad(
+ Patio.localRef("x"),
+ Patio.localRef("ptr"),
+ PatioDataLayouts.INT,
+ List.of()
+ ))
+ .addBodyInstruction(emitInt(
+ Patio.localRef("x")
+ ))
+ .addBodyInstruction(Patio.returnVoid())
+ .build();
+ var patio = Patio.compilationUnit(List.of(
+ patioDataElement,
+ patioFunction
+ ));
+
+ var result = run(patio);
+
+ assertThat(result, isSequence(
+ isInt(42)
+ ));
+ }
+
+ @Test
public void blockSingleResult() throws IOException, InterruptedException {
var patioFunction = PatioFunctionDefinition.builder()
.withName(FUNCTION_NAME)