Skip to content

Commit 051c9ed

Browse files
committed
review comments, add codegen tests that trigger online change
1 parent 3ad7cf4 commit 051c9ed

File tree

9 files changed

+240
-34
lines changed

9 files changed

+240
-34
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ tempfile = "3"
5656
encoding_rs.workspace = true
5757
encoding_rs_io.workspace = true
5858

59-
6059
[lib]
6160
name = "rusty"
6261
path = "src/lib.rs"

compiler/plc_driver/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ pub fn get_compilation_context<T: AsRef<str> + AsRef<OsStr> + Debug>(
179179
debug_level: compile_parameters.debug_level(),
180180
single_module: compile_parameters.single_module,
181181
online_change: if compile_parameters.online_change {
182-
OnlineChange::Enabled((
183-
compile_parameters.got_layout_file.clone(),
184-
compile_parameters.got_layout_format(),
185-
))
182+
OnlineChange::Enabled {
183+
file_name: compile_parameters.got_layout_file.clone(),
184+
format: compile_parameters.got_layout_format(),
185+
}
186186
} else {
187187
OnlineChange::Disabled
188188
},

compiler/plc_driver/src/pipelines.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
309309
}
310310

311311
pub fn codegen_to_string(&self, compile_options: &CompileOptions) -> Result<Vec<String>, Diagnostic> {
312-
let got_layout = if let OnlineChange::Enabled((file, format)) = &compile_options.online_change {
313-
read_got_layout(file, *format)?
312+
let got_layout = if let OnlineChange::Enabled { file_name, format } = &compile_options.online_change {
313+
read_got_layout(file_name, *format)?
314314
} else {
315315
HashMap::default()
316316
};
@@ -331,8 +331,8 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
331331
context: &'ctx CodegenContext,
332332
compile_options: &CompileOptions,
333333
) -> Result<Option<GeneratedModule<'ctx>>, Diagnostic> {
334-
let got_layout = if let OnlineChange::Enabled((file, format)) = &compile_options.online_change {
335-
read_got_layout(file, *format)?
334+
let got_layout = if let OnlineChange::Enabled { file_name, format } = &compile_options.online_change {
335+
read_got_layout(file_name, *format)?
336336
} else {
337337
HashMap::default()
338338
};
@@ -429,8 +429,8 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
429429
ensure_compile_dirs(targets, &compile_directory)?;
430430
let targets = if targets.is_empty() { &[Target::System] } else { targets };
431431

432-
let got_layout = if let OnlineChange::Enabled((file, format)) = &compile_options.online_change {
433-
read_got_layout(file, *format)?
432+
let got_layout = if let OnlineChange::Enabled { file_name, format } = &compile_options.online_change {
433+
read_got_layout(file_name, *format)?
434434
} else {
435435
HashMap::default()
436436
};
@@ -500,8 +500,8 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
500500
})
501501
.collect::<Result<Vec<_>, Diagnostic>>()?;
502502

503-
if let OnlineChange::Enabled((file, format)) = &compile_options.online_change {
504-
write_got_layout(got_layout.into_inner().unwrap(), file, *format)?;
503+
if let OnlineChange::Enabled { file_name, format } = &compile_options.online_change {
504+
write_got_layout(got_layout.into_inner().unwrap(), file_name, *format)?;
505505
}
506506

507507
Ok(res)

compiler/section_mangler/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ mod tests {
318318
];
319319

320320
inputs.into_iter().for_each(|input| {
321-
let _ = SectionMangler::from(dbg!(input));
321+
let _ = SectionMangler::from(input);
322322
});
323323
}
324324
}

src/codegen/llvm_index.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub struct LlvmTypedIndex<'ink> {
1414
type_associations: FxHashMap<String, BasicTypeEnum<'ink>>,
1515
pou_type_associations: FxHashMap<String, BasicTypeEnum<'ink>>,
1616
global_values: FxHashMap<String, GlobalValue<'ink>>,
17-
// TODO: Should this be an Option?
1817
got_indices: FxHashMap<String, u64>,
1918
initial_value_associations: FxHashMap<String, BasicValueEnum<'ink>>,
2019
loaded_variable_associations: FxHashMap<String, PointerValue<'ink>>,

src/codegen/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod multifile_codegen_tests;
1414
mod parameters_tests;
1515
mod statement_codegen_test;
1616
mod string_tests;
17+
mod online_change_tests;
1718
#[cfg(feature = "verify")]
1819
mod switch_case_tests;
1920
mod typesystem_test;
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
use insta::assert_snapshot;
2+
3+
use crate::test_utils::tests::codegen_with_online_change as codegen;
4+
5+
#[test]
6+
fn generate_function_with_online_change() {
7+
let src = codegen("
8+
FUNCTION foo : DINT
9+
VAR
10+
x : DINT;
11+
END_VAR
12+
END_FUNCTION
13+
");
14+
assert_snapshot!(src, @r###"
15+
; ModuleID = '<internal>'
16+
source_filename = "<internal>"
17+
18+
@__custom_got = weak_odr global [4 x i8*] zeroinitializer
19+
20+
define i32 @foo() section "$RUSTY$fn-foo:i32[]" {
21+
entry:
22+
%foo = alloca i32, align 4
23+
%x = alloca i32, align 4
24+
store i32 0, i32* %x, align 4
25+
store i32 0, i32* %foo, align 4
26+
%foo_ret = load i32, i32* %foo, align 4
27+
ret i32 %foo_ret
28+
}
29+
; ModuleID = '__init___testproject'
30+
source_filename = "__init___testproject"
31+
32+
@__custom_got = weak_odr global [4 x i8*] zeroinitializer
33+
34+
define void @__init___testproject() section "$RUSTY$fn-__init___testproject:v[]" {
35+
entry:
36+
ret void
37+
}
38+
"###)
39+
}
40+
41+
#[test]
42+
fn generate_program_with_online_change() {
43+
let src = codegen("
44+
PROGRAM prg
45+
VAR
46+
x : DINT;
47+
END_VAR
48+
END_PROGRAM
49+
");
50+
assert_snapshot!(src, @r###"
51+
; ModuleID = '<internal>'
52+
source_filename = "<internal>"
53+
54+
%prg = type { i32 }
55+
56+
@prg_instance = global %prg zeroinitializer, section "$RUSTY$var-prg_instance:r1i32"
57+
@__custom_got = weak_odr global [10 x i8*] zeroinitializer
58+
59+
define void @prg(%prg* %0) section "$RUSTY$fn-prg:v[]" {
60+
entry:
61+
%x = getelementptr inbounds %prg, %prg* %0, i32 0, i32 0
62+
ret void
63+
}
64+
; ModuleID = '__initializers'
65+
source_filename = "__initializers"
66+
67+
%prg = type { i32 }
68+
69+
@prg_instance = external global %prg, section "$RUSTY$var-prg_instance:r1i32"
70+
@__custom_got = weak_odr global [10 x i8*] zeroinitializer
71+
72+
define void @__init_prg(%prg* %0) section "$RUSTY$fn-__init_prg:v[pr1i32]" {
73+
entry:
74+
%self = alloca %prg*, align 8
75+
store %prg* %0, %prg** %self, align 8
76+
ret void
77+
}
78+
79+
declare void @prg(%prg*) section "$RUSTY$fn-prg:v[]"
80+
; ModuleID = '__init___testproject'
81+
source_filename = "__init___testproject"
82+
83+
%prg = type { i32 }
84+
85+
@prg_instance = external global %prg, section "$RUSTY$var-prg_instance:r1i32"
86+
@__custom_got = weak_odr global [10 x i8*] zeroinitializer
87+
88+
define void @__init___testproject() section "$RUSTY$fn-__init___testproject:v[]" {
89+
entry:
90+
%0 = load void (%prg*)*, void (%prg*)** getelementptr inbounds (void (%prg*)*, void (%prg*)** inttoptr (i64 -2401053092612145152 to void (%prg*)**), i32 7), align 8
91+
call void %0(%prg* @prg_instance)
92+
ret void
93+
}
94+
95+
declare void @__init_prg(%prg*) section "$RUSTY$fn-__init_prg:v[pr1i32]"
96+
97+
declare void @prg(%prg*) section "$RUSTY$fn-prg:v[]"
98+
"###)
99+
}
100+
101+
#[test]
102+
fn generate_program_and_var_with_online_change() {
103+
let src = codegen("
104+
PROGRAM prg
105+
VAR
106+
x : DINT;
107+
END_VAR
108+
gV := x;
109+
END_PROGRAM
110+
VAR_GLOBAL
111+
gV : DINT;
112+
END_VAR
113+
");
114+
assert_snapshot!(src, @r###"
115+
; ModuleID = '<internal>'
116+
source_filename = "<internal>"
117+
118+
%prg = type { i32 }
119+
120+
@gV = global i32 0, section "$RUSTY$var-gv:i32"
121+
@prg_instance = global %prg zeroinitializer, section "$RUSTY$var-prg_instance:r1i32"
122+
@__custom_got = weak_odr global [12 x i8*] zeroinitializer
123+
124+
define void @prg(%prg* %0) section "$RUSTY$fn-prg:v[]" {
125+
entry:
126+
%x = getelementptr inbounds %prg, %prg* %0, i32 0, i32 0
127+
%1 = load i32*, i32** getelementptr inbounds (i32*, i32** inttoptr (i64 -2401053092612145152 to i32**), i32 1), align 8
128+
%load_x = load i32, i32* %x, align 4
129+
store i32 %load_x, i32* %1, align 4
130+
ret void
131+
}
132+
; ModuleID = '__initializers'
133+
source_filename = "__initializers"
134+
135+
%prg = type { i32 }
136+
137+
@prg_instance = external global %prg, section "$RUSTY$var-prg_instance:r1i32"
138+
@__custom_got = weak_odr global [12 x i8*] zeroinitializer
139+
140+
define void @__init_prg(%prg* %0) section "$RUSTY$fn-__init_prg:v[pr1i32]" {
141+
entry:
142+
%self = alloca %prg*, align 8
143+
store %prg* %0, %prg** %self, align 8
144+
ret void
145+
}
146+
147+
declare void @prg(%prg*) section "$RUSTY$fn-prg:v[]"
148+
; ModuleID = '__init___testproject'
149+
source_filename = "__init___testproject"
150+
151+
%prg = type { i32 }
152+
153+
@prg_instance = external global %prg, section "$RUSTY$var-prg_instance:r1i32"
154+
@__custom_got = weak_odr global [12 x i8*] zeroinitializer
155+
156+
define void @__init___testproject() section "$RUSTY$fn-__init___testproject:v[]" {
157+
entry:
158+
%0 = load void (%prg*)*, void (%prg*)** getelementptr inbounds (void (%prg*)*, void (%prg*)** inttoptr (i64 -2401053092612145152 to void (%prg*)**), i32 9), align 8
159+
call void %0(%prg* @prg_instance)
160+
ret void
161+
}
162+
163+
declare void @__init_prg(%prg*) section "$RUSTY$fn-__init_prg:v[pr1i32]"
164+
165+
declare void @prg(%prg*) section "$RUSTY$fn-prg:v[]"
166+
"###)
167+
}
168+
169+
#[test]
170+
fn generate_function_and_var_with_online_change() {
171+
let src = codegen("
172+
FUNCTION foo : DINT
173+
VAR
174+
x : DINT;
175+
END_VAR
176+
gV := x;
177+
END_FUNCTION
178+
VAR_GLOBAL
179+
gV : DINT;
180+
END_VAR
181+
");
182+
assert_snapshot!(src, @r###"
183+
; ModuleID = '<internal>'
184+
source_filename = "<internal>"
185+
186+
@gV = global i32 0, section "$RUSTY$var-gv:i32"
187+
@__custom_got = weak_odr global [6 x i8*] zeroinitializer
188+
189+
define i32 @foo() section "$RUSTY$fn-foo:i32[]" {
190+
entry:
191+
%foo = alloca i32, align 4
192+
%x = alloca i32, align 4
193+
store i32 0, i32* %x, align 4
194+
store i32 0, i32* %foo, align 4
195+
%0 = load i32*, i32** getelementptr inbounds (i32*, i32** inttoptr (i64 -2401053092612145152 to i32**), i32 1), align 8
196+
%load_x = load i32, i32* %x, align 4
197+
store i32 %load_x, i32* %0, align 4
198+
%foo_ret = load i32, i32* %foo, align 4
199+
ret i32 %foo_ret
200+
}
201+
; ModuleID = '__init___testproject'
202+
source_filename = "__init___testproject"
203+
204+
@__custom_got = weak_odr global [6 x i8*] zeroinitializer
205+
206+
define void @__init___testproject() section "$RUSTY$fn-__init___testproject:v[]" {
207+
entry:
208+
ret void
209+
}
210+
"###)
211+
}

src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub mod validation;
4848
extern crate shell_words;
4949

5050
pub const DEFAULT_DWARF_VERSION: usize = 5;
51+
pub const DEFAULT_GOT_LAYOUT_FILE: &str = "online_change_got.json";
5152

5253
#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
5354
pub enum Target {
@@ -142,8 +143,6 @@ impl FromStr for ConfigFormat {
142143
}
143144
}
144145

145-
pub const DEFAULT_GOT_LAYOUT_FILE: &str = "online_change_got.json";
146-
147146
#[derive(Debug, Copy, Clone, PartialEq, Eq, ArgEnum, Serialize, Deserialize, Default)]
148147
pub enum ErrorFormat {
149148
#[default]
@@ -179,16 +178,13 @@ pub enum DebugLevel {
179178

180179
#[derive(Debug, Clone, PartialEq)]
181180
pub enum OnlineChange {
182-
Enabled((String, ConfigFormat)),
181+
Enabled { file_name: String, format: ConfigFormat },
183182
Disabled,
184183
}
185184

186185
impl OnlineChange {
187186
pub fn is_enabled(&self) -> bool {
188-
match self {
189-
OnlineChange::Enabled(_) => true,
190-
OnlineChange::Disabled => false,
191-
}
187+
matches!(self, OnlineChange::Enabled { .. })
192188
}
193189
}
194190

src/test_utils.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@ pub mod tests {
1414
use plc_source::{source_location::SourceLocationFactory, Compilable, SourceCode, SourceContainer};
1515

1616
use crate::{
17-
builtins,
18-
codegen::{CodegenContext, GeneratedModule},
19-
index::{self, FxIndexSet, Index},
20-
lexer,
21-
lowering::AstLowerer,
22-
parser,
23-
resolver::{
17+
builtins, codegen::{CodegenContext, GeneratedModule}, index::{self, FxIndexSet, Index}, lexer, lowering::AstLowerer, parser, resolver::{
2418
const_evaluator::evaluate_constants, AnnotationMapImpl, AstAnnotations, Dependency,
2519
StringLiterals, TypeAnnotator,
26-
},
27-
typesystem::get_builtin_types,
28-
DebugLevel, Validator,
20+
}, typesystem::get_builtin_types, DebugLevel, OnlineChange, Validator
2921
};
3022

3123
pub fn parse(src: &str) -> (CompilationUnit, Vec<Diagnostic>) {
@@ -195,11 +187,19 @@ pub mod tests {
195187
codegen_debug_without_unwrap(src, DebugLevel::None)
196188
}
197189

190+
pub fn codegen_with_online_change(src: &str) -> String {
191+
codegen_debug_without_unwrap_oc(src, DebugLevel::None, OnlineChange::Enabled{ file_name: "test".into(), format: crate::ConfigFormat::JSON }).unwrap()
192+
}
193+
194+
pub fn codegen_debug_without_unwrap(src: &str, debug_level: DebugLevel) -> Result<String, String> {
195+
codegen_debug_without_unwrap_oc(src, debug_level, OnlineChange::Disabled)
196+
}
197+
198198
/// Returns either a string or an error, in addition it always returns
199199
/// reported diagnostics. Therefor the return value of this method is always a tuple.
200200
/// TODO: This should not be so, we should have a diagnostic type that holds multiple new
201201
/// issues.
202-
pub fn codegen_debug_without_unwrap(src: &str, debug_level: DebugLevel) -> Result<String, String> {
202+
pub fn codegen_debug_without_unwrap_oc(src: &str, debug_level: DebugLevel, online_change: OnlineChange) -> Result<String, String> {
203203
let mut reporter = Diagnostician::buffered();
204204
reporter.register_file("<internal>".to_string(), src.to_string());
205205
let mut id_provider = IdProvider::default();
@@ -224,7 +224,7 @@ pub mod tests {
224224
&unit.file_name,
225225
crate::OptimizationLevel::None,
226226
debug_level,
227-
crate::OnlineChange::Disabled,
227+
online_change.clone(),
228228
);
229229
let llvm_index = code_generator
230230
.generate_llvm_index(

0 commit comments

Comments
 (0)