-
Notifications
You must be signed in to change notification settings - Fork 55
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
Support builtin functions #469
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c5a5c28
Support builtin functions
ghaith cfc603f
Add correctness tests
ghaith f9b9dc4
Review comments
ghaith a4cbbe4
Review Comments
ghaith 159fa90
Merge branch 'master' into builtin_functions
ghaith 84eddfc
Run clippy and fmt
ghaith f207776
Merge branch 'master' into builtin_functions
ghaith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::collections::HashMap; | ||
|
||
use inkwell::values::{BasicValue, BasicValueEnum}; | ||
use lazy_static::lazy_static; | ||
|
||
use crate::{ | ||
ast::{AstStatement, CompilationUnit, LinkageType, SourceRange}, | ||
codegen::generators::expression_generator::ExpressionCodeGenerator, | ||
diagnostics::Diagnostic, | ||
lexer::{self, IdProvider}, | ||
parser, | ||
}; | ||
|
||
// Defines a set of functions that are always included in a compiled application | ||
lazy_static! { | ||
static ref BUILTIN: HashMap<&'static str, BuiltIn> = HashMap::from([ | ||
( | ||
"ADR", | ||
BuiltIn { | ||
decl: "FUNCTION ADR<T: ANY> : LWORD | ||
VAR_INPUT | ||
in : T; | ||
END_VAR | ||
END_FUNCTION | ||
", | ||
code: |generator, params, location| { | ||
if let [reference] = params { | ||
generator | ||
.generate_element_pointer(reference) | ||
.map(|it| generator.ptr_as_value(it)) | ||
} else { | ||
Err(Diagnostic::codegen_error( | ||
"Expected exadtly one parameter for REF", | ||
location, | ||
)) | ||
} | ||
} | ||
}, | ||
), | ||
( | ||
"REF", | ||
BuiltIn { | ||
decl: "FUNCTION REF<T: ANY> : REF_TO T | ||
VAR_INPUT | ||
in : T; | ||
END_VAR | ||
END_FUNCTION | ||
", | ||
code: |generator, params, location| { | ||
if let [reference] = params { | ||
generator | ||
.generate_element_pointer(reference) | ||
.map(|it| it.as_basic_value_enum()) | ||
} else { | ||
Err(Diagnostic::codegen_error( | ||
"Expected exadtly one parameter for REF", | ||
location, | ||
)) | ||
} | ||
} | ||
}, | ||
) | ||
]); | ||
} | ||
|
||
pub struct BuiltIn { | ||
decl: &'static str, | ||
code: for<'ink, 'b> fn( | ||
&'b ExpressionCodeGenerator<'ink, 'b>, | ||
&[&AstStatement], | ||
SourceRange, | ||
) -> Result<BasicValueEnum<'ink>, Diagnostic>, | ||
} | ||
|
||
impl BuiltIn { | ||
pub fn codegen<'ink, 'b>( | ||
&self, | ||
generator: &'b ExpressionCodeGenerator<'ink, 'b>, | ||
params: &[&AstStatement], | ||
location: SourceRange, | ||
) -> Result<BasicValueEnum<'ink>, Diagnostic> { | ||
(self.code)(generator, params, location) | ||
} | ||
} | ||
|
||
pub fn parse_built_ins(id_provider: IdProvider) -> (CompilationUnit, Vec<Diagnostic>) { | ||
let src = BUILTIN | ||
.iter() | ||
.map(|(_, it)| it.decl) | ||
.collect::<Vec<&str>>() | ||
.join(" "); | ||
parser::parse(lexer::lex_with_ids(&src, id_provider), LinkageType::BuiltIn) | ||
} | ||
|
||
/// Returns the requested functio from the builtin index or None | ||
pub fn get_builtin(name: &str) -> Option<&'static BuiltIn> { | ||
BUILTIN.get(name.to_uppercase().as_str()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...n/tests/snapshots/rusty__codegen__tests__expression_tests__builtin_function_call_adr.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: src/codegen/tests/expression_tests.rs | ||
assertion_line: 479 | ||
expression: result | ||
|
||
--- | ||
; ModuleID = 'main' | ||
source_filename = "main" | ||
|
||
%main_interface = type { i32*, i32 } | ||
|
||
@main_instance = global %main_interface zeroinitializer | ||
|
||
define void @main(%main_interface* %0) { | ||
entry: | ||
%a = getelementptr inbounds %main_interface, %main_interface* %0, i32 0, i32 0 | ||
%b = getelementptr inbounds %main_interface, %main_interface* %0, i32 0, i32 1 | ||
%1 = ptrtoint i32* %b to i64 | ||
%2 = inttoptr i64 %1 to i32* | ||
store i32* %2, i32** %a, align 8 | ||
ret void | ||
} | ||
|
21 changes: 21 additions & 0 deletions
21
...n/tests/snapshots/rusty__codegen__tests__expression_tests__builtin_function_call_ref.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
source: src/codegen/tests/expression_tests.rs | ||
assertion_line: 499 | ||
expression: result | ||
|
||
--- | ||
; ModuleID = 'main' | ||
source_filename = "main" | ||
|
||
%main_interface = type { i32*, i32 } | ||
|
||
@main_instance = global %main_interface zeroinitializer | ||
|
||
define void @main(%main_interface* %0) { | ||
entry: | ||
%a = getelementptr inbounds %main_interface, %main_interface* %0, i32 0, i32 0 | ||
%b = getelementptr inbounds %main_interface, %main_interface* %0, i32 0, i32 1 | ||
store i32* %b, i32** %a, align 8 | ||
ret void | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
so far we avoided &'static str
is there a benefit over String?
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.
Honestly, I don't remember, it feels like less code (not having .to_string() in each entry)
I think initially this came when I was using a static array of declarations instead of the lazy_static map