Skip to content

Commit

Permalink
Parsing stack_limit
Browse files Browse the repository at this point in the history
  • Loading branch information
pepyakin authored and sunfishcode committed Jun 9, 2018
1 parent 0bc30ea commit 8aac784
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions filetests/parser/preamble.cton
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test cat

; Verify parsing of stack_limit.
function %minimal(i64 vmctx) {
gv0 = vmctx
; Stack limit
stack_limit = gv0

ebb0:
trap user0
}
; sameln: function %minimal(i64 vmctx) fast {
; nextln: gv0 = vmctx
; nextln: stack_limit = gv0
; nextln:
; nextln: ebb0:
; nextln: trap user0
; nextln: }
9 changes: 9 additions & 0 deletions lib/codegen/src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ impl Function {
self.stack_slots.push(data)
}

/// Sets the stack limit for the function.
///
/// Returns previous one if any.
pub fn set_stack_limit(&mut self, stack_limit: Option<GlobalVar>) -> Option<GlobalVar> {
let prev = self.stack_limit.take();
self.stack_limit = stack_limit;
prev
}

/// Adds a signature which can later be used to declare an external function import.
pub fn import_signature(&mut self, signature: Signature) -> SigRef {
self.dfg.signatures.push(signature)
Expand Down
21 changes: 21 additions & 0 deletions lib/reader/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ impl<'a> Context<'a> {
}
}

// Assign the global for the stack limit.
fn set_stack_limit(&mut self, gv: GlobalVar, loc: &Location) -> Result<()> {
if let Some(_) = self.function.set_stack_limit(Some(gv)) {
err!(loc, "multiple stack_limit declarations")
} else {
Ok(())
}
}

// Allocate a new EBB.
fn add_ebb(&mut self, ebb: Ebb, loc: &Location) -> Result<Ebb> {
while self.function.dfg.num_ebbs() <= ebb.index() {
Expand Down Expand Up @@ -973,6 +982,7 @@ impl<'a> Parser<'a> {
// * function-decl
// * signature-decl
// * jump-table-decl
// * stack-limit-decl
//
// The parsed decls are added to `ctx` rather than returned.
fn parse_preamble(&mut self, ctx: &mut Context) -> Result<()> {
Expand Down Expand Up @@ -1009,6 +1019,8 @@ impl<'a> Parser<'a> {
self.parse_jump_table_decl()
.and_then(|(jt, dat)| ctx.add_jt(jt, dat, &self.loc))
}
Some(Token::Identifier("stack_limit")) => self.parse_stack_limit_decl()
.and_then(|gv| ctx.set_stack_limit(gv, &self.loc)),
// More to come..
_ => return Ok(()),
}?;
Expand Down Expand Up @@ -1291,6 +1303,15 @@ impl<'a> Parser<'a> {
}
}

/// stack-limit-decl ::= "stack_limit" "=" GlobalVar(gv)
fn parse_stack_limit_decl(&mut self) -> Result<GlobalVar> {
self.consume();
self.match_token(Token::Equal, "expected '=' in stack limit declaration")?;
let gv = self.match_gv("expected global variable")?;

Ok(gv)
}

// Parse a function body, add contents to `ctx`.
//
// function-body ::= * { extended-basic-block }
Expand Down

0 comments on commit 8aac784

Please sign in to comment.