Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Make target configurable
Browse files Browse the repository at this point in the history
It still does nothing even if you configure it lol
  • Loading branch information
jyn514 committed May 2, 2020
1 parent fa858f7 commit c869bb9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/analyze/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<T: Lexer> Iterator for Analyzer<T> {
}

impl<I: Lexer> Analyzer<I> {
pub fn new(parser: Parser<I>) -> Self {
pub fn new(parser: Parser<I>, target: Triple) -> Self {
Self {
declarations: parser,
error_handler: ErrorHandler::new(),
Expand All @@ -95,7 +95,7 @@ impl<I: Lexer> Analyzer<I> {
pending: VecDeque::new(),
initialized: HashSet::new(),
// TODO: allow cross-compilation
target: Triple::host(),
target,
decl_side_channel: Vec::new(),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl TryFrom<u32> for Radix {
mod tests {
use crate::analyze::test::analyze;
use crate::Parser;
use target_lexicon::Triple;

#[test]
fn type_display() {
Expand Down
7 changes: 4 additions & 3 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ struct Compiler<T: Backend> {
pub(crate) fn compile<B: Backend>(
module: Module<B>,
program: Vec<Locatable<Declaration>>,
target: Triple,
debug: bool,
) -> (Result<Module<B>, CompileError>, VecDeque<CompileWarning>) {
// really we'd like to have all errors but that requires a refactor
let mut err = None;
let mut compiler = Compiler::new(module, debug);
let mut compiler = Compiler::new(module, dbg!(target), debug);
for decl in program {
let meta = decl.data.symbol.get();
let current = match &meta.ctype {
Expand Down Expand Up @@ -138,7 +139,7 @@ pub(crate) fn compile<B: Backend>(
}

impl<B: Backend> Compiler<B> {
fn new(module: Module<B>, debug: bool) -> Compiler<B> {
fn new(module: Module<B>, target: Triple, debug: bool) -> Compiler<B> {
Compiler {
module,
declarations: HashMap::new(),
Expand All @@ -148,7 +149,7 @@ impl<B: Backend> Compiler<B> {
// the initial value doesn't really matter
last_saw_loop: true,
strings: Default::default(),
target: Triple::host(),
target,
error_handler: Default::default(),
debug,
}
Expand Down
5 changes: 3 additions & 2 deletions src/lex/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::path::{Path, PathBuf};
use std::rc::Rc;

use codespan::FileId;
use target_lexicon::Triple;

use super::{Lexer, Token};
use crate::arch::TARGET;
Expand Down Expand Up @@ -736,7 +737,7 @@ impl<'a> PreProcessor<'a> {
fn boolean_expr(&mut self) -> Result<bool, CompileError> {
// TODO: is this unwrap safe? there should only be scalar types in a cpp directive...
// TODO: should this use the target arch or the host arch?
let target = target_lexicon::Triple::host();
let target = target_lexicon::HOST;
match self.cpp_expr()?.truthy(&mut self.error_handler).constexpr(&target)?.data {
(Literal::Int(i), Type::Bool) => Ok(i != 0),
_ => unreachable!("bug in const_fold or parser: cpp cond should be boolean"),
Expand Down Expand Up @@ -873,7 +874,7 @@ impl<'a> PreProcessor<'a> {
// TODO: catch expressions that aren't allowed
// (see https://github.com/jyn514/rcc/issues/5#issuecomment-575339427)
// TODO: can semantic errors happen here? should we check?
Ok(Analyzer::new(parser).parse_expr(expr))
Ok(Analyzer::new(parser, Triple::host()).parse_expr(expr))
}
/// We saw an `#if`, `#ifdef`, or `#ifndef` token at the start of the line
/// and want to either take the branch or ignore the tokens within the directive.
Expand Down
29 changes: 26 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use cranelift_module::{Backend, Module};

#[cfg(feature = "codegen")]
pub use ir::initialize_aot_module;
use target_lexicon::Triple;

/// The `Source` type for `codespan::Files`.
///
Expand Down Expand Up @@ -93,7 +94,7 @@ impl From<VecDeque<CompileError>> for Error {
}
}

#[derive(Debug, Default)]
#[derive(Debug)]
pub struct Opt {
/// If set, print all tokens found by the lexer in addition to compiling.
pub debug_lex: bool,
Expand All @@ -117,6 +118,28 @@ pub struct Opt {

/// The directories to consider as part of the search path.
pub search_path: Vec<PathBuf>,

/// The target triple to compile to.
///
/// Defaults to the host target.
pub target: Triple,
}

// We can't derive(Default) for Opt because Triple::default() is Triple::Unknown :(
impl Default for Opt {
fn default() -> Self {
Opt {
debug_lex: false,
debug_ast: false,
debug_asm: false,
no_link: false,
#[cfg(feature = "jit")]
jit: false,
max_errors: None,
search_path: Vec::new(),
target: Triple::host(),
}
}
}

/// Preprocess the source and return the tokens.
Expand Down Expand Up @@ -189,7 +212,7 @@ pub fn check_semantics(
};

let mut hir = vec![];
let mut parser = Analyzer::new(Parser::new(first, &mut cpp, opt.debug_ast));
let mut parser = Analyzer::new(Parser::new(first, &mut cpp, opt.debug_ast), opt.target.clone());
for res in &mut parser {
match res {
Ok(decl) => hir.push(decl),
Expand Down Expand Up @@ -219,7 +242,7 @@ pub fn compile<B: Backend>(
(Err(errs), warnings) => return (Err(Error::Source(errs)), warnings),
(Ok(hir), warnings) => (hir, warnings),
};
let (result, ir_warnings) = ir::compile(module, hir, opt.debug_asm);
let (result, ir_warnings) = ir::compile(module, hir, opt.target.clone(), opt.debug_asm);
warnings.extend(ir_warnings);
(result.map_err(Error::from), warnings)
}
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use rcc::{
link, preprocess, Error, Files, Opt,
};
use std::ffi::OsStr;
use target_lexicon::Triple;
use tempfile::NamedTempFile;

static ERRORS: AtomicUsize = AtomicUsize::new(0);
Expand Down Expand Up @@ -55,6 +56,7 @@ OPTIONS:
--max-errors <max> The maximum number of errors to allow before giving up.
Use 0 to allow unlimited errors. [default: 10]
-I, --include <dir> Add a directory to the local include path (`#include \"file.h\"`)
--target <target> The target platform to compile to. Allows cross-compilation.
ARGS:
<file> The file to read C source from. \"-\" means stdin (use ./- to read a file called '-').
Expand Down Expand Up @@ -263,6 +265,9 @@ fn parse_args() -> Result<(BinOpt, PathBuf), pico_args::Error> {
debug_asm: input.contains("--debug-asm"),
debug_ast: input.contains(["-a", "--debug-ast"]),
no_link: input.contains(["-c", "--no-link"]),
target: dbg!(input
.opt_value_from_str("--target")?
.unwrap_or_else(Triple::host)),
#[cfg(feature = "jit")]
jit: input.contains("--jit"),
max_errors,
Expand Down
4 changes: 2 additions & 2 deletions src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ pub(crate) mod test {
first in any::<Token>(),
tokens in arb_vec_result_locatable_token()
) {
let mut parser = Parser::new(Locatable { data: first, location: Location::default() }, tokens.into_iter(), false);
let mut parser = Parser::new(Locatable { data: first, location: Location::default() }, tokens.into_iter(), Triple::host(), false);

let peek = parser.peek_token().cloned();
let next = parser.next_token().map(|l| l.data);
Expand All @@ -416,7 +416,7 @@ pub(crate) mod test {
first in any::<Token>(),
tokens in arb_vec_result_locatable_token()
) {
let mut parser = Parser::new(Locatable { data: first, location: Location::default() }, tokens.into_iter(), false);
let mut parser = Parser::new(Locatable { data: first, location: Location::default() }, tokens.into_iter(), Triple::host(), false);

let peek = parser.peek_next_token().cloned();
parser.next_token();
Expand Down

0 comments on commit c869bb9

Please sign in to comment.