implementing embeds

This commit is contained in:
Jakub Doka 2024-10-13 15:33:57 +02:00
parent 2660d976fe
commit 19a6cdd764
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
3 changed files with 15 additions and 5 deletions

View file

@ -727,7 +727,6 @@ mod trap {
#[derive(Default)] #[derive(Default)]
pub struct Codegen { pub struct Codegen {
pub files: Vec<parser::Ast>, pub files: Vec<parser::Ast>,
pub embeds: Vec<Vec<u8>>,
tasks: Vec<Option<FTask>>, tasks: Vec<Option<FTask>>,
tys: Types, tys: Types,
@ -737,6 +736,17 @@ pub struct Codegen {
} }
impl Codegen { impl Codegen {
pub fn push_embeds(&mut self, embeds: Vec<Vec<u8>>) {
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) { pub fn generate(&mut self, root: FileId) {
self.ci.emit_entry_prelude(); self.ci.emit_entry_prelude();
self.find_or_declare(0, root, Err("main"), ""); self.find_or_declare(0, root, Err("main"), "");
@ -791,6 +801,7 @@ impl Codegen {
use {Expr as E, TokenKind as T}; use {Expr as E, TokenKind as T};
let value = match *expr { let value = match *expr {
E::Mod { id, .. } => Some(Value::ty(ty::Kind::Module(id).compress())), 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, .. } => { E::Struct { captured, packed, fields, pos, .. } => {
if captured.is_empty() { if captured.is_empty() {
Some(Value::ty( Some(Value::ty(

View file

@ -10,9 +10,7 @@ use {
collections::VecDeque, collections::VecDeque,
eprintln, eprintln,
ffi::OsStr, ffi::OsStr,
io::{ io::{self, Write as _},
Write as _, {self},
},
path::{Path, PathBuf}, path::{Path, PathBuf},
string::ToString, string::ToString,
sync::Mutex, sync::Mutex,
@ -90,7 +88,7 @@ pub fn run_compiler(root_file: &str, options: Options, out: &mut Vec<u8>) -> std
} else { } else {
let mut codegen = codegen::Codegen::default(); let mut codegen = codegen::Codegen::default();
codegen.files = parsed.ast; codegen.files = parsed.ast;
codegen.embeds = parsed.embeds; codegen.push_embeds(parsed.embeds);
codegen.generate(0); codegen.generate(0);
if options.dump_asm { if options.dump_asm {

View file

@ -667,6 +667,7 @@ struct TypedReloc {
reloc: Reloc, reloc: Reloc,
} }
#[derive(Clone)]
struct Global { struct Global {
file: FileId, file: FileId,
name: Ident, name: Ident,