Skip to content

Commit 06a5daa

Browse files
committed
Use enum_with_order!{} to define Targets
1 parent 4bbc719 commit 06a5daa

File tree

4 files changed

+78
-86
lines changed

4 files changed

+78
-86
lines changed

src/b.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ pub unsafe fn compile_primary_expression(l: *mut Lexer, c: *mut Compiler) -> Opt
487487
get_and_expect_token_but_continue(l, c, Token::CBracket)?;
488488

489489
let result = allocate_auto_var(&mut (*c).auto_vars_ator);
490-
let word_size = Arg::Literal(target_word_size((*c).target));
490+
let word_size = Arg::Literal((*c).target.word_size());
491491
// TODO: Introduce Op::Index instruction that indices values without explicitly emit Binop::Mult and uses efficient multiplication by the size of the word at the codegen level.
492492
push_opcode(Op::Binop {binop: Binop::Mult, index: result, lhs: offset, rhs: word_size}, (*l).loc, c);
493493
push_opcode(Op::Binop {binop: Binop::Plus, index: result, lhs: arg, rhs: Arg::AutoVar(result)}, (*l).loc, c);
@@ -1158,7 +1158,7 @@ pub unsafe fn main(mut argc: i32, mut argv: *mut*mut c_char) -> Option<()> {
11581158
}
11591159

11601160
let default_target_name = if let Some(default_target) = default_target {
1161-
name_of_target(default_target).expect("default target name not found")
1161+
default_target.name()
11621162
} else {
11631163
ptr::null()
11641164
};
@@ -1205,13 +1205,13 @@ pub unsafe fn main(mut argc: i32, mut argv: *mut*mut c_char) -> Option<()> {
12051205

12061206
if strcmp(*target_name, c!("list")) == 0 {
12071207
fprintf(stderr(), c!("Compilation targets:\n"));
1208-
for i in 0..TARGET_NAMES.len() {
1209-
fprintf(stderr(), c!(" %s\n"), (*TARGET_NAMES)[i].name);
1208+
for i in 0..TARGET_ORDER.len() {
1209+
fprintf(stderr(), c!(" %s\n"), (*TARGET_ORDER)[i].name());
12101210
}
12111211
return Some(());
12121212
}
12131213

1214-
let Some(target) = target_by_name(*target_name) else {
1214+
let Some(target) = Target::by_name(*target_name) else {
12151215
usage();
12161216
fprintf(stderr(), c!("ERROR: unknown target `%s`\n"), *target_name);
12171217
return None;

src/btest.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,8 @@ pub enum Outcome {
102102
RunSuccess{stdout: *const c_char},
103103
}
104104

105-
macro_rules! enum_with_order {
106-
(
107-
enum $name:ident in $order_name:ident {
108-
$($items:tt)*
109-
}
110-
) => {
111-
#[derive(Copy, Clone)]
112-
pub enum $name {
113-
$($items)*
114-
}
115-
pub const $order_name: *const [$name] = {
116-
use $name::*;
117-
&[$($items)*]
118-
};
119-
}
120-
}
121-
122105
enum_with_order! {
106+
#[derive(Copy, Clone)]
123107
enum ReportStatus in REPORT_STATUS_ORDER {
124108
OK,
125109
NeverRecorded,
@@ -188,12 +172,12 @@ pub unsafe fn execute_test(
188172
Target::Uxn => c!("rom"),
189173
Target::Mos6502 => c!("6502"),
190174
});
191-
let stdout_path = temp_sprintf(c!("%s/%s.%s.stdout.txt"), GARBAGE_FOLDER, name, name_of_target(target).unwrap());
175+
let stdout_path = temp_sprintf(c!("%s/%s.%s.stdout.txt"), GARBAGE_FOLDER, name, target.name());
192176
cmd_append! {
193177
cmd,
194178
c!("./build/b"),
195179
input_path,
196-
c!("-t"), name_of_target(target).unwrap(),
180+
c!("-t"), target.name(),
197181
c!("-o"), program_path,
198182
}
199183
if !cmd_run_sync_and_reset(cmd) {
@@ -270,7 +254,7 @@ pub unsafe fn print_top_labels(targets: *const [Target], stats_by_target: *const
270254
printf(c!("│ "));
271255
}
272256
// TODO: these fancy unicode characters don't work well on mingw32 build via wine
273-
printf(c!("┌─%-*s"), col_width - 2*j, name_of_target(target).unwrap());
257+
printf(c!("┌─%-*s"), col_width - 2*j, target.name());
274258
print_report_stats(stats)
275259
}
276260
}
@@ -284,7 +268,7 @@ pub unsafe fn print_bottom_labels(targets: *const [Target], stats_by_target: *co
284268
for _ in 0..j {
285269
printf(c!("│ "));
286270
}
287-
printf(c!("└─%-*s"), col_width - 2*j, name_of_target(target).unwrap());
271+
printf(c!("└─%-*s"), col_width - 2*j, target.name());
288272
print_report_stats(stats)
289273
}
290274
}
@@ -403,7 +387,7 @@ pub unsafe fn generate_report(reports: *const [Report], stats_by_target: *const
403387
let mut col_width = 0;
404388
for j in 0..targets.len() {
405389
let target = (*targets)[j];
406-
let width = 2*(j + 1) + strlen(name_of_target(target).unwrap());
390+
let width = 2*(j + 1) + strlen(target.name());
407391
col_width = cmp::max(col_width, width);
408392
}
409393

@@ -447,7 +431,7 @@ pub unsafe fn load_bat_from_json_file_if_exists(
447431
let mut target_test_config_table: Array<(Target, TestConfig)> = zeroed();
448432
jimp_object_begin(jimp)?;
449433
while let Some(()) = jimp_object_member(jimp) {
450-
if let Some(target) = target_by_name((*jimp).string) {
434+
if let Some(target) = Target::by_name((*jimp).string) {
451435
let test_config: TestConfig = test_config_deserialize(jimp)?;
452436
da_append(&mut target_test_config_table, (target, test_config));
453437
} else {
@@ -475,7 +459,7 @@ pub unsafe fn save_bat_to_json_file(
475459
jim_object_begin(jim);
476460
for j in 0..target_test_config_table.count {
477461
let (target, outcome) = *target_test_config_table.items.add(j);
478-
jim_member_key(jim, name_of_target(target).unwrap());
462+
jim_member_key(jim, target.name());
479463
test_config_serialize(jim, outcome);
480464
}
481465
jim_object_end(jim);
@@ -599,14 +583,11 @@ pub unsafe fn main(argc: i32, argv: *mut*mut c_char) -> Option<()> {
599583

600584
let mut targets: Array<Target> = zeroed();
601585
if *list_targets || (*target_flags).count == 0 {
602-
for j in 0..TARGET_NAMES.len() {
603-
let Target_Name { name: _, target } = (*TARGET_NAMES)[j];
604-
da_append(&mut targets, target);
605-
}
586+
da_append_many(&mut targets, TARGET_ORDER);
606587
} else {
607588
for j in 0..(*target_flags).count {
608589
let target_name = *(*target_flags).items.add(j);
609-
if let Some(target) = target_by_name(target_name) {
590+
if let Some(target) = Target::by_name(target_name) {
610591
da_append(&mut targets, target);
611592
} else {
612593
fprintf(stderr(), c!("ERROR: unknown target `%s`\n"), target_name);
@@ -619,7 +600,7 @@ pub unsafe fn main(argc: i32, argv: *mut*mut c_char) -> Option<()> {
619600
fprintf(stderr(), c!("Compilation targets:\n"));
620601
for i in 0..targets.count {
621602
let target = *targets.items.add(i);
622-
fprintf(stderr(), c!(" %s\n"), name_of_target(target).unwrap());
603+
fprintf(stderr(), c!(" %s\n"), target.name());
623604
}
624605
return Some(());
625606
}

src/crust.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ macro_rules! c {
1010
}
1111
}
1212

13+
#[macro_export]
14+
macro_rules! enum_with_order {
15+
(
16+
#[derive($($traits:tt)*)]
17+
enum $name:ident in $order_name:ident {
18+
$($items:tt)*
19+
}
20+
) => {
21+
#[derive($($traits)*)]
22+
pub enum $name {
23+
$($items)*
24+
}
25+
pub const $order_name: *const [$name] = {
26+
use $name::*;
27+
&[$($items)*]
28+
};
29+
}
30+
}
31+
1332
pub unsafe fn assoc_lookup_cstr_mut<Value>(assoc: *mut [(*const c_char, Value)], needle: *const c_char) -> Option<*mut Value> {
1433
for i in 0..assoc.len() {
1534
if strcmp((*assoc)[i].0, needle) == 0 {

src/targets.rs

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,57 @@ use crate::strcmp;
33

44
// TODO: add wasm target
55
// Don't touch this TODO! @rexim wants to stream it!
6-
#[derive(Clone, Copy, PartialEq, Eq)]
7-
pub enum Target {
8-
Fasm_x86_64_Windows,
9-
Fasm_x86_64_Linux,
10-
Gas_x86_64_Windows,
11-
Gas_x86_64_Linux,
12-
Gas_AArch64_Linux,
13-
Uxn,
14-
Mos6502,
15-
}
16-
17-
#[derive(Clone, Copy)]
18-
pub enum Os {
19-
Linux,
20-
Windows,
21-
}
22-
23-
#[derive(Clone, Copy)]
24-
pub struct Target_Name {
25-
pub name: *const c_char,
26-
pub target: Target,
6+
enum_with_order! {
7+
#[derive(Clone, Copy, PartialEq, Eq)]
8+
enum Target in TARGET_ORDER {
9+
Fasm_x86_64_Windows,
10+
Fasm_x86_64_Linux,
11+
Gas_x86_64_Windows,
12+
Gas_x86_64_Linux,
13+
Gas_AArch64_Linux,
14+
Uxn,
15+
Mos6502,
16+
}
2717
}
2818

29-
pub const TARGET_NAMES: *const [Target_Name] = &[
30-
Target_Name { name: c!("fasm-x86_64-windows"), target: Target::Fasm_x86_64_Windows },
31-
Target_Name { name: c!("fasm-x86_64-linux"), target: Target::Fasm_x86_64_Linux },
32-
Target_Name { name: c!("gas-x86_64-windows"), target: Target::Gas_x86_64_Windows },
33-
Target_Name { name: c!("gas-x86_64-linux"), target: Target::Gas_x86_64_Linux },
34-
Target_Name { name: c!("gas-aarch64-linux"), target: Target::Gas_AArch64_Linux },
35-
Target_Name { name: c!("uxn"), target: Target::Uxn },
36-
Target_Name { name: c!("6502"), target: Target::Mos6502 },
37-
];
19+
impl Target {
20+
pub unsafe fn name(self) -> *const c_char {
21+
match self {
22+
Self::Fasm_x86_64_Windows => c!("fasm-x86_64-windows"),
23+
Self::Fasm_x86_64_Linux => c!("fasm-x86_64-linux"),
24+
Self::Gas_x86_64_Windows => c!("gas-x86_64-windows"),
25+
Self::Gas_x86_64_Linux => c!("gas-x86_64-linux"),
26+
Self::Gas_AArch64_Linux => c!("gas-aarch64-linux"),
27+
Self::Uxn => c!("uxn"),
28+
Self::Mos6502 => c!("6502"),
29+
}
30+
}
3831

39-
pub unsafe fn name_of_target(target: Target) -> Option<*const c_char> {
40-
for i in 0..TARGET_NAMES.len() {
41-
if target == (*TARGET_NAMES)[i].target {
42-
return Some((*TARGET_NAMES)[i].name);
32+
pub unsafe fn by_name(name: *const c_char) -> Option<Self> {
33+
for i in 0..TARGET_ORDER.len() {
34+
let target = (*TARGET_ORDER)[i];
35+
if strcmp(target.name(), name) == 0 {
36+
return Some(target);
37+
}
4338
}
39+
None
4440
}
45-
None
46-
}
4741

48-
pub unsafe fn target_by_name(name: *const c_char) -> Option<Target> {
49-
for i in 0..TARGET_NAMES.len() {
50-
if strcmp(name, (*TARGET_NAMES)[i].name) == 0 {
51-
return Some((*TARGET_NAMES)[i].target);
42+
pub unsafe fn word_size(self) -> u64 {
43+
match self {
44+
Self::Fasm_x86_64_Windows => 8,
45+
Self::Fasm_x86_64_Linux => 8,
46+
Self::Gas_x86_64_Windows => 8,
47+
Self::Gas_x86_64_Linux => 8,
48+
Self::Gas_AArch64_Linux => 8,
49+
Self::Uxn => 2,
50+
Self::Mos6502 => 2,
5251
}
5352
}
54-
None
5553
}
5654

57-
pub unsafe fn target_word_size(target: Target) -> u64 {
58-
match target {
59-
Target::Fasm_x86_64_Windows => 8,
60-
Target::Fasm_x86_64_Linux => 8,
61-
Target::Gas_x86_64_Windows => 8,
62-
Target::Gas_x86_64_Linux => 8,
63-
Target::Gas_AArch64_Linux => 8,
64-
Target::Uxn => 2,
65-
Target::Mos6502 => 2,
66-
}
55+
#[derive(Clone, Copy)]
56+
pub enum Os {
57+
Linux,
58+
Windows,
6759
}

0 commit comments

Comments
 (0)