From 19a6cdd7644d4ed144398d4937e67b115fec042a Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Sun, 13 Oct 2024 15:33:57 +0200 Subject: [PATCH] implementing embeds --- lang/src/codegen.rs | 13 ++++++++++++- lang/src/fs.rs | 6 ++---- lang/src/lib.rs | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lang/src/codegen.rs b/lang/src/codegen.rs index 81ec90b..dc692ec 100644 --- a/lang/src/codegen.rs +++ b/lang/src/codegen.rs @@ -727,7 +727,6 @@ mod trap { #[derive(Default)] pub struct Codegen { pub files: Vec, - pub embeds: Vec>, tasks: Vec>, tys: Types, @@ -737,6 +736,17 @@ pub struct Codegen { } impl Codegen { + pub fn push_embeds(&mut self, embeds: Vec>) { + self.tys.ins.globals = embeds + .into_iter() + .map(|data| Global { + ty: self.tys.make_array(ty::Id::U8, data.len() as _), + data, + ..Default::default() + }) + .collect(); + } + pub fn generate(&mut self, root: FileId) { self.ci.emit_entry_prelude(); self.find_or_declare(0, root, Err("main"), ""); @@ -791,6 +801,7 @@ impl Codegen { use {Expr as E, TokenKind as T}; let value = match *expr { E::Mod { id, .. } => Some(Value::ty(ty::Kind::Module(id).compress())), + E::Embed { id, .. } => Some(Value::ty(ty::Kind::Global(id).compress())), E::Struct { captured, packed, fields, pos, .. } => { if captured.is_empty() { Some(Value::ty( diff --git a/lang/src/fs.rs b/lang/src/fs.rs index 05dac21..a128bec 100644 --- a/lang/src/fs.rs +++ b/lang/src/fs.rs @@ -10,9 +10,7 @@ use { collections::VecDeque, eprintln, ffi::OsStr, - io::{ - Write as _, {self}, - }, + io::{self, Write as _}, path::{Path, PathBuf}, string::ToString, sync::Mutex, @@ -90,7 +88,7 @@ pub fn run_compiler(root_file: &str, options: Options, out: &mut Vec) -> std } else { let mut codegen = codegen::Codegen::default(); codegen.files = parsed.ast; - codegen.embeds = parsed.embeds; + codegen.push_embeds(parsed.embeds); codegen.generate(0); if options.dump_asm { diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 29d2967..96d197c 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -667,6 +667,7 @@ struct TypedReloc { reloc: Reloc, } +#[derive(Clone)] struct Global { file: FileId, name: Ident,