Change intrinsic alloca to malloc and actually use libc malloc
This commit is contained in:
		
							parent
							
								
									13be3e9c02
								
							
						
					
					
						commit
						89850d7b4f
					
				| @ -13,7 +13,7 @@ struct String { | |||||||
| impl String { | impl String { | ||||||
|     pub fn new() -> String { |     pub fn new() -> String { | ||||||
|         String { |         String { | ||||||
|             inner: char::alloca(0), |             inner: char::malloc(0), | ||||||
|             length: 0, |             length: 0, | ||||||
|             max_length: 0, |             max_length: 0, | ||||||
|             must_be_freed: true, |             must_be_freed: true, | ||||||
| @ -41,7 +41,7 @@ impl String { | |||||||
| 
 | 
 | ||||||
|     pub fn add_char(&mut self, c: char) { |     pub fn add_char(&mut self, c: char) { | ||||||
|         if ((*self).length + 1) >= (*self).max_length { |         if ((*self).length + 1) >= (*self).max_length { | ||||||
|             let new = char::alloca((*self).max_length + 4); |             let new = char::malloc((*self).max_length + 4); | ||||||
|             copy_bits((*self).inner, new, (*self).max_length); |             copy_bits((*self).inner, new, (*self).max_length); | ||||||
| 
 | 
 | ||||||
|             if (*self).must_be_freed == true { |             if (*self).must_be_freed == true { | ||||||
| @ -121,7 +121,7 @@ pub fn int_div(numerator: i32, denominator: i32) -> div_t { | |||||||
| 
 | 
 | ||||||
| pub fn new_string() -> String { | pub fn new_string() -> String { | ||||||
|     String { |     String { | ||||||
|         inner: char::alloca(0), |         inner: char::malloc(0), | ||||||
|         length: 0, |         length: 0, | ||||||
|         max_length: 0, |         max_length: 0, | ||||||
|         must_be_freed: true, |         must_be_freed: true, | ||||||
| @ -143,7 +143,7 @@ pub fn from_str(str: *char) -> String { | |||||||
| 
 | 
 | ||||||
| pub fn add_char(string: &mut String, c: char) { | pub fn add_char(string: &mut String, c: char) { | ||||||
|     if ((*string).length + 1) >= (*string).max_length { |     if ((*string).length + 1) >= (*string).max_length { | ||||||
|         let new = char::alloca((*string).max_length + 4); |         let new = char::malloc((*string).max_length + 4); | ||||||
|         copy_bits((*string).inner, new, (*string).max_length); |         copy_bits((*string).inner, new, (*string).max_length); | ||||||
| 
 | 
 | ||||||
|         if (*string).must_be_freed == true { |         if (*string).must_be_freed == true { | ||||||
|  | |||||||
| @ -33,7 +33,8 @@ const FLOATS: [TypeKind; 7] = [ | |||||||
|     TypeKind::F128PPC, |     TypeKind::F128PPC, | ||||||
| ]; | ]; | ||||||
| 
 | 
 | ||||||
| const MALLOC_IDENT: &str = "reid.malloc"; | const INTRINSIC_IDENT: &str = "reid.intrinsic"; | ||||||
|  | const MALLOC_IDENT: &str = "malloc"; | ||||||
| 
 | 
 | ||||||
| pub fn form_intrinsics() -> Vec<FunctionDefinition> { | pub fn form_intrinsics() -> Vec<FunctionDefinition> { | ||||||
|     let mut intrinsics = Vec::new(); |     let mut intrinsics = Vec::new(); | ||||||
| @ -68,8 +69,8 @@ pub fn get_intrinsic_assoc_func(ty: &TypeKind, name: &str) -> Option<FunctionDef | |||||||
|             kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicSizeOf(ty.clone()))), |             kind: FunctionDefinitionKind::Intrinsic(Box::new(IntrinsicSizeOf(ty.clone()))), | ||||||
|             source: None, |             source: None, | ||||||
|         }), |         }), | ||||||
|         "alloca" => Some(FunctionDefinition { |         "malloc" => Some(FunctionDefinition { | ||||||
|             name: "alloca".to_owned(), |             name: "malloc".to_owned(), | ||||||
|             linkage_name: None, |             linkage_name: None, | ||||||
|             is_pub: true, |             is_pub: true, | ||||||
|             is_imported: false, |             is_imported: false, | ||||||
| @ -363,7 +364,7 @@ impl IntrinsicFunction for IntrinsicSizeOf { | |||||||
|     fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, _: &[StackValue]) -> Result<StackValue, ErrorKind> { |     fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, _: &[StackValue]) -> Result<StackValue, ErrorKind> { | ||||||
|         let instr = scope |         let instr = scope | ||||||
|             .block |             .block | ||||||
|             .build(Instr::Constant(reid_lib::ConstValue::U64(self.0.size_of()))) |             .build(Instr::Constant(reid_lib::ConstValue::U64(self.0.size_of() / 8))) | ||||||
|             .unwrap(); |             .unwrap(); | ||||||
|         Ok(StackValue(StackValueKind::Literal(instr), self.0.clone())) |         Ok(StackValue(StackValueKind::Literal(instr), self.0.clone())) | ||||||
|     } |     } | ||||||
| @ -374,11 +375,17 @@ pub struct IntrinsicMalloc(TypeKind); | |||||||
| impl IntrinsicFunction for IntrinsicMalloc { | impl IntrinsicFunction for IntrinsicMalloc { | ||||||
|     fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, params: &[StackValue]) -> Result<StackValue, ErrorKind> { |     fn codegen<'ctx, 'a>(&self, scope: &mut Scope<'ctx, 'a>, params: &[StackValue]) -> Result<StackValue, ErrorKind> { | ||||||
|         let amount = params.get(0).unwrap(); |         let amount = params.get(0).unwrap(); | ||||||
|         let function = scope.block.find_function(&MALLOC_IDENT.to_owned()).unwrap(); |         let function = scope | ||||||
|         let instr = scope |  | ||||||
|             .block |             .block | ||||||
|             .build(Instr::FunctionCall(function, vec![amount.instr()])) |             .find_function(&format!("{}.{}", INTRINSIC_IDENT, MALLOC_IDENT)) | ||||||
|             .unwrap(); |             .unwrap(); | ||||||
|  | 
 | ||||||
|  |         let sizeof = scope | ||||||
|  |             .block | ||||||
|  |             .build(Instr::Constant(ConstValue::U64(self.0.size_of() / 8))) | ||||||
|  |             .unwrap(); | ||||||
|  |         let bytes = scope.block.build(Instr::Mul(sizeof, amount.instr())).unwrap(); | ||||||
|  |         let instr = scope.block.build(Instr::FunctionCall(function, vec![bytes])).unwrap(); | ||||||
|         Ok(StackValue(StackValueKind::Literal(instr), self.0.clone())) |         Ok(StackValue(StackValueKind::Literal(instr), self.0.clone())) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -188,10 +188,6 @@ impl mir::Module { | |||||||
| 
 | 
 | ||||||
|             let is_main = self.is_main && function.name == "main"; |             let is_main = self.is_main && function.name == "main"; | ||||||
| 
 | 
 | ||||||
|             let is_true_extern = match function.kind { |  | ||||||
|                 FunctionDefinitionKind::Extern(i) => !i, |  | ||||||
|                 _ => false, |  | ||||||
|             }; |  | ||||||
|             let module_prefix = if let Some(module) = function.source { |             let module_prefix = if let Some(module) = function.source { | ||||||
|                 if module == self.module_id { |                 if module == self.module_id { | ||||||
|                     format!("reid.{}.", self.name) |                     format!("reid.{}.", self.name) | ||||||
| @ -222,11 +218,7 @@ impl mir::Module { | |||||||
|                     }, |                     }, | ||||||
|                 )), |                 )), | ||||||
|                 mir::FunctionDefinitionKind::Extern(imported) => Some(module.function( |                 mir::FunctionDefinitionKind::Extern(imported) => Some(module.function( | ||||||
|                     if function.source == None { |                     &full_name, | ||||||
|                         &function.name |  | ||||||
|                     } else { |  | ||||||
|                         &full_name |  | ||||||
|                     }, |  | ||||||
|                     if function.source == None { |                     if function.source == None { | ||||||
|                         Some(function.linkage_name.clone().unwrap()) |                         Some(function.linkage_name.clone().unwrap()) | ||||||
|                     } else { |                     } else { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user