Store all variables in pointers
This commit is contained in:
		
							parent
							
								
									94fbd51d35
								
							
						
					
					
						commit
						59ce454f91
					
				| @ -97,7 +97,7 @@ pub struct StackFunction<'ctx> { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Debug, Clone, PartialEq, Eq)] | #[derive(Debug, Clone, PartialEq, Eq)] | ||||||
| pub struct StackValue(StackValueKind, Type); | pub struct StackValue(StackValueKind, TypeKind); | ||||||
| 
 | 
 | ||||||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||||||
| pub enum StackValueKind { | pub enum StackValueKind { | ||||||
| @ -295,12 +295,17 @@ impl mir::Module { | |||||||
|             let mut stack_values = HashMap::new(); |             let mut stack_values = HashMap::new(); | ||||||
|             for (i, (p_name, p_ty)) in mir_function.parameters.iter().enumerate() { |             for (i, (p_name, p_ty)) in mir_function.parameters.iter().enumerate() { | ||||||
|                 // Codegen actual parameters
 |                 // Codegen actual parameters
 | ||||||
|  |                 let param = entry.build(Instr::Param(i)).unwrap(); | ||||||
|  |                 let alloca = entry | ||||||
|  |                     .build(Instr::Alloca( | ||||||
|  |                         p_name.clone(), | ||||||
|  |                         p_ty.get_type(&type_values, &types), | ||||||
|  |                     )) | ||||||
|  |                     .unwrap(); | ||||||
|  |                 entry.build(Instr::Store(alloca, param)).unwrap(); | ||||||
|                 stack_values.insert( |                 stack_values.insert( | ||||||
|                     p_name.clone(), |                     p_name.clone(), | ||||||
|                     StackValue( |                     StackValue(StackValueKind::Immutable(alloca), p_ty.clone()), | ||||||
|                         StackValueKind::Immutable(entry.build(Instr::Param(i)).unwrap()), |  | ||||||
|                         p_ty.get_type(&type_values, &types), |  | ||||||
|                     ), |  | ||||||
|                 ); |                 ); | ||||||
| 
 | 
 | ||||||
|                 // Generate debug info
 |                 // Generate debug info
 | ||||||
| @ -413,47 +418,36 @@ impl mir::Statement { | |||||||
|         match &self.0 { |         match &self.0 { | ||||||
|             mir::StmtKind::Let(NamedVariableRef(ty, name, _), mutable, expression) => { |             mir::StmtKind::Let(NamedVariableRef(ty, name, _), mutable, expression) => { | ||||||
|                 let value = expression.codegen(scope, &state).unwrap(); |                 let value = expression.codegen(scope, &state).unwrap(); | ||||||
|                 let (stack_value, _) = match mutable { | 
 | ||||||
|                     false => (StackValueKind::Immutable(value), value), |                 let alloca = scope | ||||||
|                     true => match ty { |                     .block | ||||||
|                         // Struct is already allocated at initialization
 |                     .build(Instr::Alloca( | ||||||
|                         TypeKind::Array(_, _) => (StackValueKind::Mutable(value), value), |                         name.clone(), | ||||||
|                         TypeKind::CustomType(n) => { |                         ty.get_type(scope.type_values, scope.types), | ||||||
|                             match scope.types.get(scope.type_values.get(n).unwrap()).unwrap() { |                     )) | ||||||
|                                 // Struct also is allocated at initialization
 |                     .unwrap() | ||||||
|                                 TypeDefinitionKind::Struct(_) => { |                     .maybe_location(&mut scope.block, location); | ||||||
|                                     (StackValueKind::Mutable(value), value) | 
 | ||||||
|                                 } |                 scope | ||||||
|                             } |                     .block | ||||||
|                         } |                     .build(Instr::Store(alloca, value)) | ||||||
|                         _ => { |                     .unwrap() | ||||||
|                             let alloca = scope |                     .maybe_location(&mut scope.block, location); | ||||||
|                                 .block | 
 | ||||||
|                                 .build(Instr::Alloca( |                 let stack_value = match mutable { | ||||||
|                                     name.clone(), |                     true => StackValueKind::Mutable(alloca), | ||||||
|                                     ty.get_type(scope.type_values, scope.types), |                     false => StackValueKind::Immutable(alloca), | ||||||
|                                 )) |  | ||||||
|                                 .unwrap() |  | ||||||
|                                 .maybe_location(&mut scope.block, location); |  | ||||||
|                             let store = scope |  | ||||||
|                                 .block |  | ||||||
|                                 .build(Instr::Store(alloca, value)) |  | ||||||
|                                 .unwrap() |  | ||||||
|                                 .maybe_location(&mut scope.block, location); |  | ||||||
|                             (StackValueKind::Mutable(alloca), store) |  | ||||||
|                         } |  | ||||||
|                     }, |  | ||||||
|                 }; |                 }; | ||||||
|                 scope.stack_values.insert( | 
 | ||||||
|                     name.clone(), |                 scope | ||||||
|                     StackValue(stack_value, ty.get_type(scope.type_values, scope.types)), |                     .stack_values | ||||||
|                 ); |                     .insert(name.clone(), StackValue(stack_value, ty.clone())); | ||||||
|                 if let Some(debug) = &scope.debug { |                 if let Some(debug) = &scope.debug { | ||||||
|                     match stack_value { |                     match stack_value { | ||||||
|                         StackValueKind::Immutable(_) => {} |                         StackValueKind::Immutable(_) => {} | ||||||
|                         StackValueKind::Mutable(_) => { |                         StackValueKind::Mutable(_) => { | ||||||
|                             let location = self.1.into_debug(scope.tokens).unwrap(); |                             let location = self.1.into_debug(scope.tokens).unwrap(); | ||||||
|                             let var = debug.info.metadata( |                             debug.info.metadata( | ||||||
|                                 &debug.scope, |                                 &debug.scope, | ||||||
|                                 DebugMetadata::LocalVar(DebugLocalVariable { |                                 DebugMetadata::LocalVar(DebugLocalVariable { | ||||||
|                                     name: name.clone(), |                                     name: name.clone(), | ||||||
| @ -524,19 +518,15 @@ impl mir::Expression { | |||||||
|                     .stack_values |                     .stack_values | ||||||
|                     .get(&varref.1) |                     .get(&varref.1) | ||||||
|                     .expect("Variable reference not found?!"); |                     .expect("Variable reference not found?!"); | ||||||
|  |                 dbg!(varref); | ||||||
|                 Some(match v.0 { |                 Some(match v.0 { | ||||||
|                     StackValueKind::Immutable(val) => val.clone(), |                     StackValueKind::Immutable(val) | StackValueKind::Mutable(val) => scope | ||||||
|                     StackValueKind::Mutable(val) => { |                         .block | ||||||
|                         if state.should_load { |                         .build(Instr::Load( | ||||||
|                             match v.1 { |                             val, | ||||||
|                                 // TODO probably wrong ..?
 |                             v.1.get_type(scope.type_values, scope.types), | ||||||
|                                 Type::Ptr(_) => val, |                         )) | ||||||
|                                 _ => scope.block.build(Instr::Load(val, v.1.clone())).unwrap(), |                         .unwrap(), | ||||||
|                             } |  | ||||||
|                         } else { |  | ||||||
|                             val |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                     _ => panic!("Found an unknown-mutable variable!"), |                     _ => panic!("Found an unknown-mutable variable!"), | ||||||
|                 }) |                 }) | ||||||
|             } |             } | ||||||
| @ -687,8 +677,6 @@ impl mir::Expression { | |||||||
|                 let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name).unwrap(); |                 let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name).unwrap(); | ||||||
|                 let idx = struct_ty.find_index(field).unwrap(); |                 let idx = struct_ty.find_index(field).unwrap(); | ||||||
| 
 | 
 | ||||||
|                 dbg!(&scope.context); |  | ||||||
|                 dbg!(&struct_val); |  | ||||||
|                 let mut value = scope |                 let mut value = scope | ||||||
|                     .block |                     .block | ||||||
|                     .build(Instr::GetStructElemPtr(struct_val, idx as u32)) |                     .build(Instr::GetStructElemPtr(struct_val, idx as u32)) | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user