Add a bunch of operations to lib
This commit is contained in:
		
							parent
							
								
									f0f828d1eb
								
							
						
					
					
						commit
						feac7163f2
					
				| @ -355,8 +355,17 @@ impl Builder { | |||||||
|                 Instr::Param(_) => Ok(()), |                 Instr::Param(_) => Ok(()), | ||||||
|                 Instr::Constant(_) => Ok(()), |                 Instr::Constant(_) => Ok(()), | ||||||
|                 Instr::Add(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), |                 Instr::Add(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|  |                 Instr::FAdd(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|                 Instr::Sub(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), |                 Instr::Sub(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|                 Instr::Mult(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), |                 Instr::FSub(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|  |                 Instr::Mul(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|  |                 Instr::FMul(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|  |                 Instr::UDiv(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|  |                 Instr::SDiv(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|  |                 Instr::FDiv(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|  |                 Instr::URem(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|  |                 Instr::SRem(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|  |                 Instr::FRem(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|                 Instr::And(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), |                 Instr::And(lhs, rhs) => match_types(&lhs, &rhs, &self).map(|_| ()), | ||||||
|                 Instr::ICmp(_, lhs, rhs) => { |                 Instr::ICmp(_, lhs, rhs) => { | ||||||
|                     let t = match_types(&lhs, &rhs, self)?; |                     let t = match_types(&lhs, &rhs, self)?; | ||||||
|  | |||||||
| @ -754,16 +754,62 @@ impl InstructionHolder { | |||||||
|                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|                     LLVMBuildAdd(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) |                     LLVMBuildAdd(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|                 } |                 } | ||||||
|  |                 FAdd(lhs, rhs) => { | ||||||
|  |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|  |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|  |                     LLVMBuildFAdd(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|  |                 } | ||||||
|                 Sub(lhs, rhs) => { |                 Sub(lhs, rhs) => { | ||||||
|                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|                     LLVMBuildSub(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) |                     LLVMBuildSub(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|                 } |                 } | ||||||
|                 Mult(lhs, rhs) => { |                 FSub(lhs, rhs) => { | ||||||
|  |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|  |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|  |                     LLVMBuildFSub(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|  |                 } | ||||||
|  |                 Mul(lhs, rhs) => { | ||||||
|                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|                     LLVMBuildMul(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) |                     LLVMBuildMul(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|                 } |                 } | ||||||
|  | 
 | ||||||
|  |                 FMul(lhs, rhs) => { | ||||||
|  |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|  |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|  |                     LLVMBuildFMul(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|  |                 } | ||||||
|  |                 UDiv(lhs, rhs) => { | ||||||
|  |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|  |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|  |                     LLVMBuildUDiv(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|  |                 } | ||||||
|  |                 SDiv(lhs, rhs) => { | ||||||
|  |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|  |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|  |                     LLVMBuildSDiv(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|  |                 } | ||||||
|  |                 FDiv(lhs, rhs) => { | ||||||
|  |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|  |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|  |                     LLVMBuildFDiv(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|  |                 } | ||||||
|  |                 URem(lhs, rhs) => { | ||||||
|  |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|  |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|  |                     LLVMBuildURem(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|  |                 } | ||||||
|  |                 SRem(lhs, rhs) => { | ||||||
|  |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|  |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|  |                     LLVMBuildSRem(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|  |                 } | ||||||
|  |                 FRem(lhs, rhs) => { | ||||||
|  |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|  |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|  |                     LLVMBuildFRem(module.builder_ref, lhs_val, rhs_val, name.as_ptr()) | ||||||
|  |                 } | ||||||
|                 And(lhs, rhs) => { |                 And(lhs, rhs) => { | ||||||
|                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; |                     let lhs_val = module.values.get(&lhs).unwrap().value_ref; | ||||||
|                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; |                     let rhs_val = module.values.get(&rhs).unwrap().value_ref; | ||||||
|  | |||||||
| @ -303,8 +303,17 @@ impl Debug for Instr { | |||||||
|             Instr::Param(nth) => fmt_call(f, &"Param", &nth), |             Instr::Param(nth) => fmt_call(f, &"Param", &nth), | ||||||
|             Instr::Constant(c) => c.fmt(f), |             Instr::Constant(c) => c.fmt(f), | ||||||
|             Instr::Add(lhs, rhs) => fmt_binop(f, lhs, &"+", rhs), |             Instr::Add(lhs, rhs) => fmt_binop(f, lhs, &"+", rhs), | ||||||
|  |             Instr::FAdd(lhs, rhs) => fmt_binop(f, lhs, &"+", rhs), | ||||||
|             Instr::Sub(lhs, rhs) => fmt_binop(f, lhs, &"-", rhs), |             Instr::Sub(lhs, rhs) => fmt_binop(f, lhs, &"-", rhs), | ||||||
|             Instr::Mult(lhs, rhs) => fmt_binop(f, lhs, &"*", rhs), |             Instr::FSub(lhs, rhs) => fmt_binop(f, lhs, &"-", rhs), | ||||||
|  |             Instr::Mul(lhs, rhs) => fmt_binop(f, lhs, &"*", rhs), | ||||||
|  |             Instr::FMul(lhs, rhs) => fmt_binop(f, lhs, &"*", rhs), | ||||||
|  |             Instr::UDiv(lhs, rhs) => fmt_binop(f, lhs, &"/", rhs), | ||||||
|  |             Instr::SDiv(lhs, rhs) => fmt_binop(f, lhs, &"/", rhs), | ||||||
|  |             Instr::FDiv(lhs, rhs) => fmt_binop(f, lhs, &"/", rhs), | ||||||
|  |             Instr::URem(lhs, rhs) => fmt_binop(f, lhs, &"%", rhs), | ||||||
|  |             Instr::SRem(lhs, rhs) => fmt_binop(f, lhs, &"%", rhs), | ||||||
|  |             Instr::FRem(lhs, rhs) => fmt_binop(f, lhs, &"%", rhs), | ||||||
|             Instr::And(lhs, rhs) => fmt_binop(f, lhs, &"&&", rhs), |             Instr::And(lhs, rhs) => fmt_binop(f, lhs, &"&&", rhs), | ||||||
|             Instr::Phi(val) => fmt_call(f, &"Phi", &val), |             Instr::Phi(val) => fmt_call(f, &"Phi", &val), | ||||||
|             Instr::ICmp(cmp, lhs, rhs) => fmt_binop(f, lhs, cmp, rhs), |             Instr::ICmp(cmp, lhs, rhs) => fmt_binop(f, lhs, cmp, rhs), | ||||||
|  | |||||||
| @ -312,9 +312,30 @@ pub enum Instr { | |||||||
|     Param(usize), |     Param(usize), | ||||||
|     Constant(ConstValue), |     Constant(ConstValue), | ||||||
| 
 | 
 | ||||||
|  |     /// Add two integers
 | ||||||
|     Add(InstructionValue, InstructionValue), |     Add(InstructionValue, InstructionValue), | ||||||
|  |     /// Add two floats
 | ||||||
|  |     FAdd(InstructionValue, InstructionValue), | ||||||
|  |     /// Subtract two integers
 | ||||||
|     Sub(InstructionValue, InstructionValue), |     Sub(InstructionValue, InstructionValue), | ||||||
|     Mult(InstructionValue, InstructionValue), |     /// Subtract two floats
 | ||||||
|  |     FSub(InstructionValue, InstructionValue), | ||||||
|  |     /// Multiply two integers
 | ||||||
|  |     Mul(InstructionValue, InstructionValue), | ||||||
|  |     /// Multiply two floats
 | ||||||
|  |     FMul(InstructionValue, InstructionValue), | ||||||
|  |     /// Divide two unsigned integers
 | ||||||
|  |     UDiv(InstructionValue, InstructionValue), | ||||||
|  |     /// Divide two signed integers
 | ||||||
|  |     SDiv(InstructionValue, InstructionValue), | ||||||
|  |     /// Divide two floats
 | ||||||
|  |     FDiv(InstructionValue, InstructionValue), | ||||||
|  |     /// Get the remainder from two unsigned integers
 | ||||||
|  |     URem(InstructionValue, InstructionValue), | ||||||
|  |     /// Get the remainder from two signed integers
 | ||||||
|  |     SRem(InstructionValue, InstructionValue), | ||||||
|  |     /// Get the remainder from two floats
 | ||||||
|  |     FRem(InstructionValue, InstructionValue), | ||||||
|     And(InstructionValue, InstructionValue), |     And(InstructionValue, InstructionValue), | ||||||
|     Phi(Vec<InstructionValue>), |     Phi(Vec<InstructionValue>), | ||||||
| 
 | 
 | ||||||
| @ -416,8 +437,17 @@ impl InstructionValue { | |||||||
|                     .ok_or(()), |                     .ok_or(()), | ||||||
|                 Constant(c) => Ok(c.get_type()), |                 Constant(c) => Ok(c.get_type()), | ||||||
|                 Add(lhs, rhs) => match_types(lhs, rhs, &builder), |                 Add(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|  |                 FAdd(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|                 Sub(lhs, rhs) => match_types(lhs, rhs, &builder), |                 Sub(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|                 Mult(lhs, rhs) => match_types(lhs, rhs, &builder), |                 FSub(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|  |                 Mul(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|  |                 FMul(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|  |                 UDiv(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|  |                 SDiv(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|  |                 FDiv(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|  |                 URem(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|  |                 SRem(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|  |                 FRem(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|                 And(lhs, rhs) => match_types(lhs, rhs, &builder), |                 And(lhs, rhs) => match_types(lhs, rhs, &builder), | ||||||
|                 ICmp(_, _, _) => Ok(Type::Bool), |                 ICmp(_, _, _) => Ok(Type::Bool), | ||||||
|                 FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret), |                 FunctionCall(function_value, _) => Ok(builder.function_data(function_value).ret), | ||||||
|  | |||||||
| @ -641,7 +641,7 @@ impl mir::Expression { | |||||||
|                             scope.block.build("sub", Instr::Sub(lhs, rhs)).unwrap() |                             scope.block.build("sub", Instr::Sub(lhs, rhs)).unwrap() | ||||||
|                         } |                         } | ||||||
|                         mir::BinaryOperator::Mult => { |                         mir::BinaryOperator::Mult => { | ||||||
|                             scope.block.build("mul", Instr::Mult(lhs, rhs)).unwrap() |                             scope.block.build("mul", Instr::Mul(lhs, rhs)).unwrap() | ||||||
|                         } |                         } | ||||||
|                         mir::BinaryOperator::And => { |                         mir::BinaryOperator::And => { | ||||||
|                             scope.block.build("and", Instr::And(lhs, rhs)).unwrap() |                             scope.block.build("and", Instr::And(lhs, rhs)).unwrap() | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user