From 5a1fa18bb347ff034c2adea0e924ecf2ee73425f Mon Sep 17 00:00:00 2001
From: Russel Arbore <russel.jma@gmail.com>
Date: Sun, 16 Feb 2025 09:33:18 -0600
Subject: [PATCH] Lower read and write in rt backend

---
 hercules_cg/src/rt.rs | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/hercules_cg/src/rt.rs b/hercules_cg/src/rt.rs
index f758fed3..8c5775d8 100644
--- a/hercules_cg/src/rt.rs
+++ b/hercules_cg/src/rt.rs
@@ -622,9 +622,26 @@ impl<'a> RTContext<'a> {
             } => {
                 let block = &mut blocks.get_mut(&bb).unwrap().data;
                 let collect_ty = self.typing[collect.idx()];
-                let out_size = self.codegen_type_size(self.typing[id.idx()]);
+                let self_ty = self.typing[id.idx()];
                 let offset = self.codegen_index_math(collect_ty, indices, bb)?;
-                todo!();
+                if self.module.types[self_ty.idx()].is_primitive() {
+                    write!(
+                        block,
+                        "{} = ({}.byte_add({} as usize).0 as *mut {}).read();",
+                        self.get_value(id, bb, true),
+                        self.get_value(collect, bb, false),
+                        offset,
+                        self.get_type(self_ty)
+                    )?;
+                } else {
+                    write!(
+                        block,
+                        "{} = {}.byte_add({} as usize);",
+                        self.get_value(id, bb, true),
+                        self.get_value(collect, bb, false),
+                        offset,
+                    )?;
+                }
             }
             Node::Write {
                 collect,
@@ -633,11 +650,18 @@ impl<'a> RTContext<'a> {
             } => {
                 let block = &mut blocks.get_mut(&bb).unwrap().data;
                 let collect_ty = self.typing[collect.idx()];
-                let data_size = self.codegen_type_size(self.typing[data.idx()]);
-                let offset = self.codegen_index_math(collect_ty, indices, bb)?;
                 let data_ty = self.typing[data.idx()];
+                let data_size = self.codegen_type_size(data_ty);
+                let offset = self.codegen_index_math(collect_ty, indices, bb)?;
                 if self.module.types[data_ty.idx()].is_primitive() {
-                    todo!();
+                    write!(
+                        block,
+                        "({}.byte_add({} as usize).0 as *mut {}).write({});",
+                        self.get_value(collect, bb, false),
+                        offset,
+                        self.get_type(data_ty),
+                        self.get_value(data, bb, false),
+                    )?;
                 } else {
                     // If the data item being written is not a primitive type,
                     // then perform a memcpy from the data collection to the
-- 
GitLab