-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support builtin functions * Add correctness tests The builtinin index is now created first in the lib, and not always by visiting an ast. Also fixed the index import to use not skip enum and hardware constants. As a side effect of the index being imported, some index numbers changed in some tests
- Loading branch information
Showing
22 changed files
with
458 additions
and
56 deletions.
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 { | ||
let src = BUILTIN | ||
.iter() | ||
.map(|(_, it)| it.decl) | ||
.collect::<Vec<&str>>() | ||
.join(" "); | ||
parser::parse(lexer::lex_with_ids(&src, id_provider), LinkageType::BuiltIn).0 | ||
} | ||
|
||
/// 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.