Skip to content

Commit b90ab93

Browse files
ghaithriederm
andauthored
Allow multiple object files as input (#419)
* Allow multiple object files as input * Integration tests for the linker Co-authored-by: Mathias Rieder <mathias.rieder@gmail.com>
1 parent 0ba48e3 commit b90ab93

File tree

10 files changed

+600
-160
lines changed

10 files changed

+600
-160
lines changed

.vscode/launch.json

+19-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@
2424
"args": [],
2525
"cwd": "${workspaceFolder}"
2626
},
27+
{
28+
"type": "lldb",
29+
"request": "launch",
30+
"name": "Debug executable 'rustyc' - oscat",
31+
"cargo": {
32+
"args": [
33+
"build",
34+
"--bin=rustyc",
35+
"--package=rusty"
36+
],
37+
"filter": {
38+
"name": "rustyc",
39+
"kind": "bin"
40+
}
41+
},
42+
"args": ["--ir", "/home/ghaith/git/oscat/oscat_single_file.st","/home/ghaith/git/oscat/std_stubs.st", "-o", "/tmp/comp.ll"],
43+
"cwd": "${workspaceFolder}"
44+
},
2745
{
2846
"type": "lldb",
2947
"request": "launch",
@@ -39,7 +57,7 @@
3957
"kind": "bin"
4058
}
4159
},
42-
"args": ["--ir", "examples/oscat/*.st"],
60+
"args": ["-c", "/home/ghaith/git/oscat/test.st", "-o", "/tmp/comp.ll"],
4361
"cwd": "${workspaceFolder}"
4462
},
4563
{

src/cli.rs

+32-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@ use encoding_rs::Encoding;
33
use std::{ffi::OsStr, path::Path};
44
use structopt::{clap::ArgGroup, StructOpt};
55

6-
#[derive(PartialEq, Debug)]
7-
pub enum FormatOption {
8-
Static,
9-
PIC,
10-
Shared,
11-
Bitcode,
12-
IR,
13-
}
6+
use crate::FormatOption;
147

158
// => Set the default output format here:
169
const DEFAULT_FORMAT: FormatOption = FormatOption::Static;
@@ -56,6 +49,13 @@ pub struct CompileParameters {
5649
#[structopt(long = "static", group = "format", help = "Emit an object as output")]
5750
pub output_obj_code: bool,
5851

52+
#[structopt(
53+
long = "relocatable",
54+
group = "format",
55+
help = "Emit an object as output"
56+
)]
57+
pub output_reloc_code: bool,
58+
5959
#[structopt(
6060
long = "bc",
6161
group = "format",
@@ -126,6 +126,8 @@ impl CompileParameters {
126126
Some(FormatOption::Shared)
127127
} else if self.output_obj_code {
128128
Some(FormatOption::Static)
129+
} else if self.output_reloc_code {
130+
Some(FormatOption::Relocatable)
129131
} else {
130132
None
131133
}
@@ -146,6 +148,7 @@ impl CompileParameters {
146148
} else {
147149
let ending = match out_format {
148150
FormatOption::Bitcode => ".bc",
151+
FormatOption::Relocatable => ".o",
149152
FormatOption::Static if self.skip_linking => ".o",
150153
FormatOption::Static => "",
151154
FormatOption::Shared | FormatOption::PIC => ".so",
@@ -164,7 +167,8 @@ impl CompileParameters {
164167

165168
#[cfg(test)]
166169
mod cli_tests {
167-
use super::{CompileParameters, FormatOption, ParameterError};
170+
use super::{CompileParameters, ParameterError};
171+
use crate::FormatOption;
168172
use pretty_assertions::assert_eq;
169173
use structopt::clap::ErrorKind;
170174

@@ -206,6 +210,10 @@ mod cli_tests {
206210
vec_of_strings!["input.st", "--ir", "--shared", "--pic", "--bc"],
207211
ErrorKind::ArgumentConflict,
208212
);
213+
expect_argument_error(
214+
vec_of_strings!["input.st", "--ir", "--relocatable"],
215+
ErrorKind::ArgumentConflict,
216+
);
209217
}
210218

211219
#[test]
@@ -336,41 +344,56 @@ mod cli_tests {
336344
assert_eq!(parameters.output_obj_code, false);
337345
assert_eq!(parameters.output_pic_obj, false);
338346
assert_eq!(parameters.output_shared_obj, false);
347+
assert_eq!(parameters.output_reloc_code, false);
339348

340349
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--bc")).unwrap();
341350
assert_eq!(parameters.output_ir, false);
342351
assert_eq!(parameters.output_bit_code, true);
343352
assert_eq!(parameters.output_obj_code, false);
344353
assert_eq!(parameters.output_pic_obj, false);
345354
assert_eq!(parameters.output_shared_obj, false);
355+
assert_eq!(parameters.output_reloc_code, false);
346356

347357
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--static")).unwrap();
348358
assert_eq!(parameters.output_ir, false);
349359
assert_eq!(parameters.output_bit_code, false);
350360
assert_eq!(parameters.output_obj_code, true);
351361
assert_eq!(parameters.output_pic_obj, false);
352362
assert_eq!(parameters.output_shared_obj, false);
363+
assert_eq!(parameters.output_reloc_code, false);
353364

354365
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--pic")).unwrap();
355366
assert_eq!(parameters.output_ir, false);
356367
assert_eq!(parameters.output_bit_code, false);
357368
assert_eq!(parameters.output_obj_code, false);
358369
assert_eq!(parameters.output_pic_obj, true);
359370
assert_eq!(parameters.output_shared_obj, false);
371+
assert_eq!(parameters.output_reloc_code, false);
360372

361373
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--shared")).unwrap();
362374
assert_eq!(parameters.output_ir, false);
363375
assert_eq!(parameters.output_bit_code, false);
364376
assert_eq!(parameters.output_obj_code, false);
365377
assert_eq!(parameters.output_pic_obj, false);
366378
assert_eq!(parameters.output_shared_obj, true);
379+
assert_eq!(parameters.output_reloc_code, false);
380+
381+
let parameters =
382+
CompileParameters::parse(vec_of_strings!("input.st", "--relocatable")).unwrap();
383+
assert_eq!(parameters.output_ir, false);
384+
assert_eq!(parameters.output_bit_code, false);
385+
assert_eq!(parameters.output_obj_code, false);
386+
assert_eq!(parameters.output_pic_obj, false);
387+
assert_eq!(parameters.output_shared_obj, false);
388+
assert_eq!(parameters.output_reloc_code, true);
367389

368390
let parameters = CompileParameters::parse(vec_of_strings!("input.st")).unwrap();
369391
assert_eq!(parameters.output_ir, false);
370392
assert_eq!(parameters.output_bit_code, false);
371393
assert_eq!(parameters.output_obj_code, false);
372394
assert_eq!(parameters.output_pic_obj, false);
373395
assert_eq!(parameters.output_shared_obj, false);
396+
assert_eq!(parameters.output_reloc_code, false);
374397
}
375398

376399
#[test]

src/diagnostics.rs

+18
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub enum ErrNo {
3535

3636
//general
3737
general__io_err,
38+
general__param_err,
3839

3940
//syntax
4041
syntax__generic_error,
@@ -76,6 +77,9 @@ pub enum ErrNo {
7677
codegen__general,
7778
codegen__missing_function,
7879
codegen__missing_compare_function,
80+
81+
//linker
82+
linker__generic_error,
7983
}
8084

8185
impl Diagnostic {
@@ -376,6 +380,13 @@ impl Diagnostic {
376380
}
377381
}
378382

383+
pub fn param_error(reason: &str) -> Diagnostic {
384+
Diagnostic::GeneralError {
385+
message: reason.to_string(),
386+
err_no: ErrNo::general__param_err,
387+
}
388+
}
389+
379390
pub fn llvm_error(file: &str, llvm_error: &LLVMString) -> Diagnostic {
380391
Diagnostic::GeneralError {
381392
message: format!(
@@ -438,6 +449,13 @@ impl Diagnostic {
438449
}
439450
}
440451

452+
pub fn link_error(error: &str) -> Diagnostic {
453+
Diagnostic::GeneralError {
454+
err_no: ErrNo::linker__generic_error,
455+
message: error.to_string(),
456+
}
457+
}
458+
441459
pub fn get_message(&self) -> &str {
442460
match self {
443461
Diagnostic::SyntaxError { message, .. }

0 commit comments

Comments
 (0)