Add GetGlobal "instruction"
This commit is contained in:
		
							parent
							
								
									140d963d9b
								
							
						
					
					
						commit
						ebe7fc8d75
					
				| @ -28,10 +28,10 @@ pub struct BlockValue(pub(crate) FunctionValue, pub(crate) usize); | |||||||
| pub struct InstructionValue(pub(crate) BlockValue, pub(crate) usize); | pub struct InstructionValue(pub(crate) BlockValue, pub(crate) usize); | ||||||
| 
 | 
 | ||||||
| #[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)] | #[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)] | ||||||
| pub struct ConstantValue(pub(crate) usize); | pub struct ConstantValue(pub(crate) ModuleValue, pub(crate) usize); | ||||||
| 
 | 
 | ||||||
| #[derive(Clone, Hash, Copy, PartialEq, Eq)] | #[derive(Debug, Clone, Hash, Copy, PartialEq, Eq)] | ||||||
| pub struct GlobalValue(pub(crate) usize); | pub struct GlobalValue(pub(crate) ModuleValue, pub(crate) usize); | ||||||
| 
 | 
 | ||||||
| #[derive(Clone)] | #[derive(Clone)] | ||||||
| pub struct ModuleHolder { | pub struct ModuleHolder { | ||||||
| @ -194,7 +194,7 @@ impl Builder { | |||||||
|         unsafe { |         unsafe { | ||||||
|             let mut modules = self.modules.borrow_mut(); |             let mut modules = self.modules.borrow_mut(); | ||||||
|             let module = modules.get_unchecked_mut(module.0); |             let module = modules.get_unchecked_mut(module.0); | ||||||
|             let value = ConstantValue(module.constants.len()); |             let value = ConstantValue(module.value, module.constants.len()); | ||||||
|             module.constants.push(ConstantValueHolder { value, kind }); |             module.constants.push(ConstantValueHolder { value, kind }); | ||||||
|             value |             value | ||||||
|         } |         } | ||||||
| @ -209,7 +209,7 @@ impl Builder { | |||||||
|         unsafe { |         unsafe { | ||||||
|             let mut modules = self.modules.borrow_mut(); |             let mut modules = self.modules.borrow_mut(); | ||||||
|             let module = modules.get_unchecked_mut(module.0); |             let module = modules.get_unchecked_mut(module.0); | ||||||
|             let value = GlobalValue(module.globals.len()); |             let value = GlobalValue(module.value, module.globals.len()); | ||||||
|             module.globals.push(GlobalValueHolder { |             module.globals.push(GlobalValueHolder { | ||||||
|                 value, |                 value, | ||||||
|                 name, |                 name, | ||||||
| @ -383,6 +383,24 @@ impl Builder { | |||||||
|         self.modules.clone() |         self.modules.clone() | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     pub(crate) fn get_global_initializer(&self, value: GlobalValue) -> ConstantValue { | ||||||
|  |         unsafe { | ||||||
|  |             let modules = self.modules.borrow(); | ||||||
|  |             let module = modules.get_unchecked(value.0.0); | ||||||
|  |             let global = module.globals.get_unchecked(value.1); | ||||||
|  |             global.initializer | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub(crate) fn get_const_kind(&self, value: ConstantValue) -> ConstValueKind { | ||||||
|  |         unsafe { | ||||||
|  |             let modules = self.modules.borrow(); | ||||||
|  |             let module = modules.get_unchecked(value.0.0); | ||||||
|  |             let constant = module.constants.get_unchecked(value.1); | ||||||
|  |             constant.kind.clone() | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     pub fn check_instruction(&self, instruction: &InstructionValue) -> CompileResult<()> { |     pub fn check_instruction(&self, instruction: &InstructionValue) -> CompileResult<()> { | ||||||
|         unsafe { |         unsafe { | ||||||
|             match self.instr_data(&instruction).kind { |             match self.instr_data(&instruction).kind { | ||||||
| @ -617,6 +635,7 @@ impl Builder { | |||||||
|                         Err(ErrorKind::Null) |                         Err(ErrorKind::Null) | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|  |                 Instr::GetGlobal(_) => Ok(()), | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @ -768,6 +787,11 @@ impl InstructionValue { | |||||||
|                 ShiftRightLogical(lhs, _) => lhs.get_type(builder), |                 ShiftRightLogical(lhs, _) => lhs.get_type(builder), | ||||||
|                 ShiftRightArithmetic(lhs, _) => lhs.get_type(builder), |                 ShiftRightArithmetic(lhs, _) => lhs.get_type(builder), | ||||||
|                 ShiftLeft(lhs, _) => lhs.get_type(builder), |                 ShiftLeft(lhs, _) => lhs.get_type(builder), | ||||||
|  |                 GetGlobal(global_value) => { | ||||||
|  |                     let constant = builder.get_global_initializer(*global_value); | ||||||
|  |                     let kind = builder.get_const_kind(constant); | ||||||
|  |                     Ok(kind.get_type()) | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -1052,6 +1052,7 @@ impl InstructionHolder { | |||||||
|                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|                     LLVMBuildShl(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) |                     LLVMBuildShl(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|                 } |                 } | ||||||
|  |                 GetGlobal(global_value) => module.globals.get(global_value).unwrap().clone(), | ||||||
|             } |             } | ||||||
|         }; |         }; | ||||||
|         if let Some(record) = &self.record { |         if let Some(record) = &self.record { | ||||||
|  | |||||||
| @ -392,6 +392,7 @@ impl Debug for Instr { | |||||||
|             Instr::ShiftRightLogical(lhs, rhs) => fmt_binop(f, lhs, &">>l", rhs), |             Instr::ShiftRightLogical(lhs, rhs) => fmt_binop(f, lhs, &">>l", rhs), | ||||||
|             Instr::ShiftRightArithmetic(lhs, rhs) => fmt_binop(f, lhs, &">>a", rhs), |             Instr::ShiftRightArithmetic(lhs, rhs) => fmt_binop(f, lhs, &">>a", rhs), | ||||||
|             Instr::ShiftLeft(lhs, rhs) => fmt_binop(f, lhs, &"<<", rhs), |             Instr::ShiftLeft(lhs, rhs) => fmt_binop(f, lhs, &"<<", rhs), | ||||||
|  |             Instr::GetGlobal(global_value) => write!(f, "global {:?}", global_value), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -276,6 +276,7 @@ impl Instr { | |||||||
|             Instr::ShiftRightLogical(..) => "lshr", |             Instr::ShiftRightLogical(..) => "lshr", | ||||||
|             Instr::ShiftRightArithmetic(..) => "ashr", |             Instr::ShiftRightArithmetic(..) => "ashr", | ||||||
|             Instr::ShiftLeft(..) => "shl", |             Instr::ShiftLeft(..) => "shl", | ||||||
|  |             Instr::GetGlobal(global_value) => todo!(), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -373,6 +374,7 @@ pub enum CmpPredicate { | |||||||
| pub enum Instr { | pub enum Instr { | ||||||
|     Param(usize), |     Param(usize), | ||||||
|     Constant(ConstValueKind), |     Constant(ConstValueKind), | ||||||
|  |     GetGlobal(GlobalValue), | ||||||
| 
 | 
 | ||||||
|     /// Add two integers
 |     /// Add two integers
 | ||||||
|     Add(InstructionValue, InstructionValue), |     Add(InstructionValue, InstructionValue), | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user