Skip to content

Commit

Permalink
try method #1
Browse files Browse the repository at this point in the history
  • Loading branch information
AlicanC committed Oct 11, 2022
1 parent 58624f9 commit 090a4d1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
40 changes: 40 additions & 0 deletions sway-core/src/asm_generation/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::asm_lang::*;
use crate::error::*;

use sway_error::error::CompileError;
use sway_ir::*;

/// Checks for disallowed opcodes in non-contract code.
/// i.e., if this is a script or predicate, we can't use certain contract opcodes.
Expand Down Expand Up @@ -74,3 +75,42 @@ fn check_for_contract_opcodes(ops: &[AllocatedOp]) -> CompileResult<()> {
err(vec![], errors)
}
}

pub fn check_invalid_return(ir: &Context, module: &Module) -> CompileResult<()> {
let default_span = sway_types::span::Span::new("no span found".into(), 0, 1, None).unwrap();
match module.get_kind(ir) {
Kind::Contract | Kind::Library => ok((), vec![], vec![]),
Kind::Predicate | Kind::Script => {
let entry_point_function = module
.function_iter(ir)
.find(|func| {
func.get_name(ir) == sway_types::constants::DEFAULT_ENTRY_POINT_FN_NAME
})
.unwrap();

let ret_ty = entry_point_function.get_return_type(ir);

if type_has_ptr(ir, &ret_ty) {
err(
vec![],
vec![CompileError::PointerReturnNotAllowedInMain { span: default_span }],
)
} else {
ok((), vec![], vec![])
}
}
}
}

fn type_has_ptr(ir: &Context, ty: &Type) -> bool {
match ty {
Type::Pointer(_) => true,
Type::Array(aggregate) | Type::Union(aggregate) | Type::Struct(aggregate) => {
match aggregate.get_content(ir) {
AggregateContent::ArrayType(ty, _) => type_has_ptr(ir, ty),
AggregateContent::FieldTypes(tys) => tys.iter().any(|ty| type_has_ptr(ir, ty)),
}
}
_ => false,
}
}
10 changes: 9 additions & 1 deletion sway-core/src/asm_generation/from_ir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
asm_builder::AsmBuilder,
checks::check_invalid_opcodes,
checks::{check_invalid_opcodes, check_invalid_return},
finalized_asm::FinalizedAsm,
programs::{AbstractProgram, ProgramKind},
register_sequencer::RegisterSequencer,
Expand All @@ -26,6 +26,14 @@ pub fn compile_ir_to_asm(
let mut errors: Vec<CompileError> = Vec::new();

let module = ir.module_iter().next().unwrap();

check!(
check_invalid_return(ir, &module),
return err(warnings, errors),
warnings,
errors
);

let abstract_program = check!(
compile_module_to_asm(RegisterSequencer::new(), ir, module),
return err(warnings, errors),
Expand Down

0 comments on commit 090a4d1

Please sign in to comment.