-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Introduce __eq intrinsic
#2100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce __eq intrinsic
#2100
Changes from 12 commits
15a740c
0c9af62
9b154e1
ee35b5d
d10e152
69173ac
cc60855
3c8aa03
b6a9b66
fc96a5d
1886acb
febfe3e
bf4db7a
0e50977
54541f4
a675f18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ use crate::{ | |
| type_engine::{insert_type, resolve_type, TypeId, TypeInfo}, | ||
| }; | ||
| use sway_ir::{Context, *}; | ||
| use sway_parse::intrinsics::Intrinsic; | ||
| use sway_types::{ | ||
| ident::Ident, | ||
| span::{Span, Spanned}, | ||
|
|
@@ -318,41 +319,63 @@ impl FnCompiler { | |
| fn compile_intrinsic_function( | ||
| &mut self, | ||
| context: &mut Context, | ||
| kind: TypedIntrinsicFunctionKind, | ||
| TypedIntrinsicFunctionKind { | ||
| kind, | ||
| arguments, | ||
| type_arguments, | ||
| span: _, | ||
| }: TypedIntrinsicFunctionKind, | ||
| span: Span, | ||
| ) -> Result<Value, CompileError> { | ||
| // We safely index into arguments and type_arguments arrays below | ||
| // because the type-checker ensures that the arguments are all there. | ||
| match kind { | ||
| TypedIntrinsicFunctionKind::SizeOfVal { exp } => { | ||
| Intrinsic::SizeOfVal => { | ||
| let exp = arguments[0].clone(); | ||
| // Compile the expression in case of side-effects but ignore its value. | ||
| let ir_type = convert_resolved_typeid(context, &exp.return_type, &exp.span)?; | ||
| self.compile_expression(context, *exp)?; | ||
| self.compile_expression(context, exp)?; | ||
| Ok(Constant::get_uint( | ||
| context, | ||
| 64, | ||
| ir_type_size_in_bytes(context, &ir_type), | ||
| None, | ||
| )) | ||
| } | ||
| TypedIntrinsicFunctionKind::SizeOfType { type_id, type_span } => { | ||
| let ir_type = convert_resolved_typeid(context, &type_id, &type_span)?; | ||
| Intrinsic::SizeOfType => { | ||
| let targ = type_arguments[0].clone(); | ||
| let ir_type = convert_resolved_typeid(context, &targ.type_id, &targ.span)?; | ||
| Ok(Constant::get_uint( | ||
| context, | ||
| 64, | ||
| ir_type_size_in_bytes(context, &ir_type), | ||
| None, | ||
| )) | ||
| } | ||
| TypedIntrinsicFunctionKind::IsRefType { type_id, type_span } => { | ||
| let ir_type = convert_resolved_typeid(context, &type_id, &type_span)?; | ||
| Intrinsic::IsReferenceType => { | ||
| let targ = type_arguments[0].clone(); | ||
| let ir_type = convert_resolved_typeid(context, &targ.type_id, &targ.span)?; | ||
| Ok(Constant::get_bool(context, !ir_type.is_copy_type(), None)) | ||
| } | ||
| TypedIntrinsicFunctionKind::GetStorageKey => { | ||
| Intrinsic::GetStorageKey => { | ||
| let span_md_idx = MetadataIndex::from_span(context, &span); | ||
| Ok(self | ||
| .current_block | ||
| .ins(context) | ||
| .get_storage_key(span_md_idx, None)) | ||
| } | ||
| Intrinsic::Eq => { | ||
| let lhs = arguments[0].clone(); | ||
| let rhs = arguments[1].clone(); | ||
|
Comment on lines
+369
to
+370
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing we could do is codify the arguments into the enum itself, so we don't have a panic risk here. e.g. enum Intrinsic {
Eq { lhs: TypedExpression, rhs: TypedExpression },
SizeOf { type_argument: TypeInfo }
}etc... I'm just suggesting this, but I wouldn't block this PR on it, as it could be a more major refactor. It would give us type safety here, though. |
||
| let lhs_value = self.compile_expression(context, lhs)?; | ||
| let rhs_value = self.compile_expression(context, rhs)?; | ||
| Ok(self.current_block.ins(context).cmp( | ||
| Predicate::Equal, | ||
| lhs_value, | ||
| rhs_value, | ||
| None, | ||
| )) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you specify what you mean by "the type checker did its job"? The phrase "did its job" doesn't convey exactly what we are assuming here.