Add reading file to buffer macro, only works for one global per file
This commit is contained in:
		
							parent
							
								
									ebe7fc8d75
								
							
						
					
					
						commit
						f700c577f1
					
				| @ -1,4 +1,5 @@ | ||||
| fn main() -> u8 { | ||||
|     // let message = String::from(include_bytes!("./macro_easy_file.txt")); | ||||
|     return test_macro!("./examples/macro_easy_file.txt"); | ||||
|     let bytes = test_macro!("./examples/macro_easy_file.txt"); | ||||
|     return bytes[0]; | ||||
| } | ||||
|  | ||||
| @ -276,7 +276,7 @@ impl Instr { | ||||
|             Instr::ShiftRightLogical(..) => "lshr", | ||||
|             Instr::ShiftRightArithmetic(..) => "ashr", | ||||
|             Instr::ShiftLeft(..) => "shl", | ||||
|             Instr::GetGlobal(global_value) => todo!(), | ||||
|             Instr::GetGlobal(..) => "global", | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -195,6 +195,7 @@ impl mir::Expression { | ||||
|             mir::ExprKind::AssociatedFunctionCall(ty, fn_call) => { | ||||
|                 allocated.extend(fn_call.allocate(&format!("{}::{}", ty, fn_call.name), scope)) | ||||
|             } | ||||
|             mir::ExprKind::GlobalRef(..) => {} | ||||
|         } | ||||
| 
 | ||||
|         allocated | ||||
|  | ||||
| @ -201,9 +201,10 @@ impl mir::Module { | ||||
|             insert_debug!(&TypeKind::CustomType(type_key.clone())); | ||||
|         } | ||||
| 
 | ||||
|         let mut globals = HashMap::new(); | ||||
|         for global in &self.globals { | ||||
|             let (const_value, _) = global.kind.codegen(context, &type_values, &module)?; | ||||
|             module.add_global(&global.name, const_value); | ||||
|             globals.insert(global.name.clone(), module.add_global(&global.name, const_value)); | ||||
|         } | ||||
| 
 | ||||
|         let mut functions = HashMap::new(); | ||||
| @ -379,6 +380,7 @@ impl mir::Module { | ||||
|                                 functions: &functions, | ||||
|                                 types: &types, | ||||
|                                 type_values: &type_values, | ||||
|                                 globals: &globals, | ||||
|                                 stack_values: HashMap::new(), | ||||
|                                 debug: Some(Debug { | ||||
|                                     info: &debug, | ||||
| @ -461,6 +463,7 @@ impl mir::Module { | ||||
|                         scope: compile_unit.clone(), | ||||
|                         types: &debug_types, | ||||
|                     }), | ||||
|                     globals: &globals, | ||||
|                     binops: &binops, | ||||
|                     allocator: Rc::new(RefCell::new(allocator)), | ||||
|                 }; | ||||
| @ -521,6 +524,7 @@ impl mir::Module { | ||||
|                         scope: compile_unit.clone(), | ||||
|                         types: &debug_types, | ||||
|                     }), | ||||
|                     globals: &globals, | ||||
|                     binops: &binops, | ||||
|                     allocator: Rc::new(RefCell::new(allocator)), | ||||
|                 }; | ||||
| @ -724,6 +728,7 @@ impl mir::Statement { | ||||
|             mir::StmtKind::Let(NamedVariableRef(ty, name, meta), mutable, expression) => { | ||||
|                 let value = expression.codegen(scope, &state)?.unwrap(); | ||||
| 
 | ||||
|                 dbg!(&scope.allocator, &meta, &value.1); | ||||
|                 let alloca = scope | ||||
|                     .allocate(meta, &value.1) | ||||
|                     .unwrap() | ||||
| @ -1351,6 +1356,14 @@ impl mir::Expression { | ||||
|                 } | ||||
|             } | ||||
|             mir::ExprKind::AssociatedFunctionCall(ty, call) => codegen_function_call(Some(ty), call, scope, state)?, | ||||
|             mir::ExprKind::GlobalRef(global_name, ty) => { | ||||
|                 let global_value = scope.globals.get(global_name).unwrap(); | ||||
|                 let a = Some(StackValue( | ||||
|                     StackValueKind::Literal(scope.block.build(Instr::GetGlobal(global_value.clone())).unwrap()), | ||||
|                     TypeKind::UserPtr(Box::new(ty.clone())), | ||||
|                 )); | ||||
|                 a | ||||
|             } | ||||
|         }; | ||||
|         if let Some(value) = &value { | ||||
|             value.instr().maybe_location(&mut scope.block, location.clone()); | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| use std::{cell::RefCell, collections::HashMap, mem, rc::Rc}; | ||||
| 
 | ||||
| use reid_lib::{ | ||||
|     builder::{InstructionValue, TypeValue}, | ||||
|     builder::{GlobalValue, InstructionValue, TypeValue}, | ||||
|     debug_information::{DebugInformation, DebugLocation, DebugScopeValue, DebugTypeValue}, | ||||
|     Block, Context, Function, Instr, Module, | ||||
| }; | ||||
| @ -30,6 +30,7 @@ pub struct Scope<'ctx, 'scope> { | ||||
|     pub(super) functions: &'scope HashMap<String, ScopeFunctionKind<'ctx>>, | ||||
|     pub(super) binops: &'scope HashMap<BinopKey, StackBinopDefinition<'ctx>>, | ||||
|     pub(super) stack_values: HashMap<String, StackValue>, | ||||
|     pub(super) globals: &'scope HashMap<String, GlobalValue>, | ||||
|     pub(super) debug: Option<Debug<'ctx>>, | ||||
|     pub(super) allocator: Rc<RefCell<Allocator>>, | ||||
| } | ||||
| @ -51,6 +52,7 @@ impl<'ctx, 'a> Scope<'ctx, 'a> { | ||||
|             stack_values: self.stack_values.clone(), | ||||
|             debug: self.debug.clone(), | ||||
|             allocator: self.allocator.clone(), | ||||
|             globals: self.globals, | ||||
|             binops: self.binops, | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -280,6 +280,7 @@ impl Display for ExprKind { | ||||
|                 write!(f, "::")?; | ||||
|                 Display::fmt(function_call, f) | ||||
|             } | ||||
|             ExprKind::GlobalRef(global_value, type_kind) => write!(f, "todo globals"), | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -429,6 +429,7 @@ impl Expression { | ||||
|                 Err(_) => Ok((ReturnKind::Soft, type_kind.clone())), | ||||
|             }, | ||||
|             AssociatedFunctionCall(_, fcall) => fcall.return_type(), | ||||
|             GlobalRef(_, type_kind) => Ok((ReturnKind::Soft, TypeKind::UserPtr(Box::new(type_kind.clone())))), | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| @ -448,6 +449,7 @@ impl Expression { | ||||
|             ExprKind::If(_) => None, | ||||
|             ExprKind::CastTo(expression, _) => expression.backing_var(), | ||||
|             ExprKind::AssociatedFunctionCall(..) => None, | ||||
|             ExprKind::GlobalRef(..) => None, | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| @ -493,6 +495,7 @@ impl Expression { | ||||
|             ExprKind::Deref(_) => None, | ||||
|             ExprKind::CastTo(expression, _) => expression.num_value()?, | ||||
|             ExprKind::AssociatedFunctionCall(..) => None, | ||||
|             ExprKind::GlobalRef(..) => None, | ||||
|         }) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -173,6 +173,7 @@ impl mir::Expression { | ||||
|             mir::ExprKind::CastTo(expression, _) => { | ||||
|                 globals.extend(expression.gen_macros(data, state)); | ||||
|             } | ||||
|             mir::ExprKind::GlobalRef(..) => {} | ||||
|         } | ||||
|         globals | ||||
|     } | ||||
| @ -211,12 +212,17 @@ impl MacroFunction for TestMacro { | ||||
|             .map(|c| GlobalKind::Literal(Literal::U8(*c))) | ||||
|             .collect::<Vec<_>>(); | ||||
| 
 | ||||
|         Ok(( | ||||
|             vec![GlobalValue { | ||||
|                 name: "sometestglobalvalue".to_owned(), | ||||
|         let len = literals.len(); | ||||
| 
 | ||||
|         let global_name = "sometestglobalvalue".to_owned(); | ||||
|         let global = GlobalValue { | ||||
|             name: global_name.clone(), | ||||
|             kind: GlobalKind::Array(literals), | ||||
|             }], | ||||
|             mir::ExprKind::Literal(mir::Literal::Vague(mir::VagueLiteral::Number(5))), | ||||
|         }; | ||||
| 
 | ||||
|         Ok(( | ||||
|             vec![global.clone()], | ||||
|             mir::ExprKind::GlobalRef(global_name, TypeKind::U8), | ||||
|         )) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -271,6 +271,7 @@ pub enum ExprKind { | ||||
|     Borrow(Box<Expression>, bool), | ||||
|     Deref(Box<Expression>), | ||||
|     CastTo(Box<Expression>, TypeKind), | ||||
|     GlobalRef(String, TypeKind), | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug, Clone)] | ||||
| @ -427,13 +428,13 @@ pub struct Module { | ||||
|     pub is_main: bool, | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug)] | ||||
| #[derive(Debug, Clone)] | ||||
| pub struct GlobalValue { | ||||
|     pub name: String, | ||||
|     pub kind: GlobalKind, | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug)] | ||||
| #[derive(Debug, Clone)] | ||||
| pub enum GlobalKind { | ||||
|     Literal(Literal), | ||||
|     Array(Vec<GlobalKind>), | ||||
|  | ||||
| @ -615,6 +615,7 @@ impl Expression { | ||||
|             ExprKind::Borrow(expression, _) => expression.pass(pass, state, scope, mod_id)?, | ||||
|             ExprKind::Deref(expression) => expression.pass(pass, state, scope, mod_id)?, | ||||
|             ExprKind::CastTo(expression, _) => expression.pass(pass, state, scope, mod_id)?, | ||||
|             ExprKind::GlobalRef(..) => {} | ||||
|         } | ||||
|         Ok(()) | ||||
|     } | ||||
|  | ||||
| @ -767,6 +767,10 @@ impl Expression { | ||||
|                     Ok(function_call.return_type.clone().resolve_ref(typerefs)) | ||||
|                 } | ||||
|             } | ||||
|             ExprKind::GlobalRef(global_value, type_kind) => Ok(self | ||||
|                 .return_type(typerefs, state.scope.module_id.unwrap()) | ||||
|                 .map(|r| r.1) | ||||
|                 .unwrap()), | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -660,6 +660,10 @@ impl Expression { | ||||
|                 // Provide function return type
 | ||||
|                 Ok(type_refs.from_type(&fn_call.ret).unwrap()) | ||||
|             } | ||||
|             ExprKind::GlobalRef(global_value, type_kind) => Ok(self | ||||
|                 .return_type(type_refs.types, state.scope.module_id.unwrap()) | ||||
|                 .map(|r| type_refs.from_type(&r.1).unwrap()) | ||||
|                 .unwrap()), | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user