Skip to content

Commit

Permalink
Let IRgen compile predicate IR modules specifically. (FuelLabs#3125)
Browse files Browse the repository at this point in the history
Previously we were just compiling scripts and predicates as the same and
so predicates were compiled to `script` modules.
  • Loading branch information
otrho authored Oct 25, 2022
1 parent 37dbe4b commit 1d0b1af
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
3 changes: 2 additions & 1 deletion sway-core/src/asm_generation/from_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ fn compile_module_to_asm(
let kind = match module.get_kind(context) {
Kind::Contract => ProgramKind::Contract,
Kind::Script => ProgramKind::Script,
Kind::Library | Kind::Predicate => todo!("libraries and predicates coming soon!"),
Kind::Predicate => ProgramKind::Predicate,
Kind::Library => todo!("libraries coming soon!"),
};

ok(
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/asm_generation/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::asm_lang::Label;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum ProgramKind {
Script,
Predicate,
Contract,
}

Expand Down
4 changes: 4 additions & 0 deletions sway-core/src/asm_generation/programs/final.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ impl FinalProgram {
data_section: self.data_section,
program_section: self.ops,
},
ProgramKind::Predicate => FinalizedAsm::PredicateMain {
data_section: self.data_section,
program_section: self.ops,
},
ProgramKind::Contract => FinalizedAsm::ContractAbi {
data_section: self.data_section,
program_section: self.ops,
Expand Down
11 changes: 4 additions & 7 deletions sway-core/src/ir_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ pub fn compile_program(program: ty::TyProgram) -> Result<Context, CompileError>
ty::TyProgramKind::Script {
main_function,
declarations,
}
| ty::TyProgramKind::Predicate {
main_function,
declarations,
// predicates and scripts have the same codegen, their only difference is static
// type-check time checks.
// Predicates are not allowed to use logs so no need to pass in `logged_types` here
} => compile::compile_script(
&mut ctx,
main_function,
Expand All @@ -45,6 +38,10 @@ pub fn compile_program(program: ty::TyProgram) -> Result<Context, CompileError>
.map(|(log_id, type_id)| (type_id, log_id))
.collect(),
),
ty::TyProgramKind::Predicate {
main_function,
declarations,
} => compile::compile_predicate(&mut ctx, main_function, &root.namespace, declarations),
ty::TyProgramKind::Contract {
abi_entries,
declarations,
Expand Down
16 changes: 16 additions & 0 deletions sway-core/src/ir_generation/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ pub(super) fn compile_script(
Ok(module)
}

pub(super) fn compile_predicate(
context: &mut Context,
main_function: ty::TyFunctionDeclaration,
namespace: &namespace::Module,
declarations: Vec<ty::TyDeclaration>,
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Predicate);
let mut md_mgr = MetadataManager::default();

compile_constants(context, &mut md_mgr, module, namespace)?;
compile_declarations(context, &mut md_mgr, module, namespace, declarations)?;
compile_function(context, &mut md_mgr, module, main_function, &HashMap::new())?;

Ok(module)
}

pub(super) fn compile_contract(
context: &mut Context,
abi_entries: Vec<ty::TyFunctionDeclaration>,
Expand Down
14 changes: 9 additions & 5 deletions sway-ir/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,26 @@ mod ir_builder {
peg::parser! {
pub(in crate::parser) grammar parser() for str {
pub(in crate::parser) rule ir_descrs() -> IrAstModule
= _ s:script() eoi() {
s
= _ sop:script_or_predicate() eoi() {
sop
}
/ _ c:contract() eoi() {
c
}

rule script() -> IrAstModule
= "script" _ "{" _ fn_decls:fn_decl()* "}" _ metadata:metadata_decls() {
rule script_or_predicate() -> IrAstModule
= kind:module_kind() "{" _ fn_decls:fn_decl()* "}" _ metadata:metadata_decls() {
IrAstModule {
kind: crate::module::Kind::Script,
kind,
fn_decls,
metadata
}
}

rule module_kind() -> Kind
= "script" _ { Kind::Script }
/ "predicate" _ { Kind::Predicate }

rule contract() -> IrAstModule
= "contract" _ "{" _ fn_decls:fn_decl()* "}" _ metadata:metadata_decls() {
IrAstModule {
Expand Down
2 changes: 1 addition & 1 deletion sway-ir/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn module_to_doc<'a>(
match module.kind {
Kind::Contract => "contract",
Kind::Library => "library",
Kind::Predicate => "predicate ",
Kind::Predicate => "predicate",
Kind::Script => "script",
}
)))
Expand Down
12 changes: 12 additions & 0 deletions test/src/ir_generation/tests/predicate.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
predicate;

fn main() -> bool {
// Nope.
false
}


// not: script {
// check: predicate {

// check: fn main() -> bool

0 comments on commit 1d0b1af

Please sign in to comment.