Skip to content

Commit

Permalink
feat: Add a compiler option for controlling whether to emit debug inf…
Browse files Browse the repository at this point in the history
…ormation
  • Loading branch information
Marwes committed Jan 22, 2017
1 parent 8195324 commit 6c55280
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/compiler_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ impl<E, Extra> Compileable<Extra> for TypecheckValue<E>
thread.global_env(),
symbols,
&source,
filename.to_string());
filename.to_string(),
compiler.emit_debug_info);
compiler.compile_expr(self.expr.borrow())?
};
function.id = Symbol::from(filename);
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub type Result<T> = StdResult<T, Error>;
pub struct Compiler {
symbols: Symbols,
implicit_prelude: bool,
emit_debug_info: bool,
}

impl Compiler {
Expand All @@ -127,6 +128,7 @@ impl Compiler {
Compiler {
symbols: Symbols::new(),
implicit_prelude: true,
emit_debug_info: true,
}
}

Expand All @@ -137,6 +139,13 @@ impl Compiler {
self
}

/// Sets wheter the compiler should emit debug information such as source maps and variable names.
/// (default: true)
pub fn emit_debug_info(mut self, emit_debug_info: bool) -> Compiler {
self.emit_debug_info = emit_debug_info;
self
}

/// Parse `expr_str`, returning an expression if successful
pub fn parse_expr(&mut self,
file: &str,
Expand Down
39 changes: 29 additions & 10 deletions vm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct FunctionEnv {
free_vars: Vec<Symbol>,
/// The line where instructions are currently being emitted
current_line: Line,
emit_debug_info: bool,
function: CompiledFunction,
}

Expand Down Expand Up @@ -121,15 +122,19 @@ impl FunctionEnvs {
typ: ArcType) {
compiler.stack_types.enter_scope();
compiler.stack_constructors.enter_scope();
self.envs.push(FunctionEnv::new(args, id, typ, compiler.source_name.clone()));
self.envs.push(FunctionEnv::new(args,
id,
typ,
compiler.source_name.clone(),
compiler.emit_debug_info));
}

fn end_function(&mut self, compiler: &mut Compiler, current_line: Line) -> FunctionEnv {
compiler.stack_types.exit_scope();
compiler.stack_constructors.exit_scope();
let instructions = self.function.instructions.len();
self.function.debug_info.source_map.close(instructions, current_line);
{
if compiler.emit_debug_info {
self.function.debug_info.source_map.close(instructions, current_line);
let function = &mut **self;
function.function
.debug_info
Expand All @@ -141,13 +146,19 @@ impl FunctionEnvs {
}

impl FunctionEnv {
fn new(args: VmIndex, id: Symbol, typ: ArcType, source_name: String) -> FunctionEnv {
fn new(args: VmIndex,
id: Symbol,
typ: ArcType,
source_name: String,
emit_debug_info: bool)
-> FunctionEnv {
FunctionEnv {
free_vars: Vec::new(),
stack: ScopedMap::new(),
stack_size: 0,
function: CompiledFunction::new(args, id, typ, source_name),
current_line: Line::from(0),
emit_debug_info: emit_debug_info,
}
}

Expand All @@ -165,10 +176,13 @@ impl FunctionEnv {
}

self.function.instructions.push(instruction);
self.function
.debug_info
.source_map
.emit(self.function.instructions.len() - 1, self.current_line);

if self.emit_debug_info {
self.function
.debug_info
.source_map
.emit(self.function.instructions.len() - 1, self.current_line);
}
}

fn increase_stack(&mut self, adjustment: VmIndex) {
Expand Down Expand Up @@ -248,7 +262,9 @@ impl FunctionEnv {

fn new_stack_var(&mut self, _compiler: &Compiler, s: Symbol) {
debug!("Push var: {:?} at {}", s, self.stack_size - 1);
self.function.debug_info.local_map.emit(self.function.instructions.len(), s.clone());
if self.emit_debug_info {
self.function.debug_info.local_map.emit(self.function.instructions.len(), s.clone());
}
self.stack.insert(s, self.stack_size - 1);
}

Expand Down Expand Up @@ -302,6 +318,7 @@ pub struct Compiler<'a> {
stack_types: ScopedMap<Symbol, Alias<Symbol, ArcType>>,
source: &'a Source<'a>,
source_name: String,
emit_debug_info: bool,
empty_symbol: Symbol,
}

Expand Down Expand Up @@ -337,7 +354,8 @@ impl<'a> Compiler<'a> {
vm: &'a GlobalVmState,
mut symbols: SymbolModule<'a>,
source: &'a Source<'a>,
source_name: String)
source_name: String,
emit_debug_info: bool)
-> Compiler<'a> {
Compiler {
globals: globals,
Expand All @@ -348,6 +366,7 @@ impl<'a> Compiler<'a> {
stack_types: ScopedMap::new(),
source: source,
source_name: source_name,
emit_debug_info: emit_debug_info,
}
}

Expand Down

0 comments on commit 6c55280

Please sign in to comment.