Skip to content

Commit f207776

Browse files
authored
Merge branch 'master' into builtin_functions
2 parents 84eddfc + fd520d7 commit f207776

File tree

7 files changed

+176
-33
lines changed

7 files changed

+176
-33
lines changed

Cargo.lock

+20-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

book/src/using_rusty.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ for that and link with libc when we're done. This program can also be found at
3434
done. Otherwise, the program will most likely crash (because it tries to return to a function that never
3535
existed).
3636

37-
```st
37+
```iecst
3838
@EXTERNAL FUNCTION puts : DINT
3939
VAR_INPUT
4040
text : STRING;
@@ -59,6 +59,18 @@ Compiling with rusty is very easy. If you just want to build an object file, the
5959
rustyc -c hello_world.st -o hello_world.o
6060
```
6161

62+
### Optimization
63+
`rustyc` offers 4 levels of optimization which correspond to the levels established by llvm respectively [clang](https://clang.llvm.org/docs/CommandGuide/clang.html#code-generation-options) (`none` to `aggressive`, respectively `-O0` to `-O3`).
64+
65+
To use an optimization, the flag `-O` or `--optimization` is required:
66+
67+
- `rustyc -c "**/*.st" -O none`
68+
- `rustyc -c "**/*.st" -O less`
69+
- `rustyc -c "**/*.st" -O default`
70+
- `rustyc -c "**/*.st" -O aggressive`
71+
72+
By default `rustyc` will use `default` which corresponds to clang's `-O2`.
73+
6274
### Linking an executable
6375
Instead, you can also compile this into an executable and run it:
6476
```bash

scripts/build.sh

+20-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ release=0
1313
debug=0
1414
container=0
1515
assume_linux=0
16+
junit=0
1617

1718
CONTAINER_NAME='rust-llvm'
1819

@@ -101,7 +102,18 @@ function run_check_style() {
101102
function run_test() {
102103
CARGO_OPTIONS=$(set_cargo_options)
103104
log "Running cargo test"
104-
cargo test $CARGO_OPTIONS
105+
if [[ $junit -ne 0 ]]; then
106+
#Delete the test results if they exist
107+
rm -rf "$project_location/test_results"
108+
make_dir "$project_location/test_results"
109+
#Passing through tail here will remove the first line which is currently empty.
110+
cargo test $CARGO_OPTIONS --lib -- --format=junit -Zunstable-options | tail -n +2 > "$project_location"/test_results/rusty_unit_tests.xml
111+
# Run only the integration tests
112+
#https://stackoverflow.com/questions/62447864/how-can-i-run-only-integration-tests
113+
cargo test $CARGO_OPTIONS --test '*' -- --format=junit -Zunstable-options | tail -n +2 > "$project_location"/test_results/rusty_integration_tests.xml
114+
else
115+
cargo test $CARGO_OPTIONS
116+
fi
105117
}
106118

107119
function generate_sources() {
@@ -149,6 +161,9 @@ function run_in_container() {
149161
if [[ $test -ne 0 ]]; then
150162
params="$params --test"
151163
fi
164+
if [[ $junit -ne 0 ]]; then
165+
params="$params --junit"
166+
fi
152167
if [[ $doc -ne 0 ]]; then
153168
params="$params --doc"
154169
fi
@@ -179,7 +194,7 @@ function run_in_container() {
179194
set -o errexit -o pipefail -o noclobber -o nounset
180195

181196
OPTIONS=sorbvc
182-
LONGOPTS=sources,offline,release,check,check-style,build,doc,test,verbose,container,linux,container-name:,coverage
197+
LONGOPTS=sources,offline,release,check,check-style,build,doc,test,junit,verbose,container,linux,container-name:,coverage
183198

184199
check_env
185200
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
@@ -231,6 +246,9 @@ while true; do
231246
--test)
232247
test=1
233248
;;
249+
--junit)
250+
junit=1
251+
;;
234252
--coverage)
235253
coverage=1
236254
;;

src/cli.rs

+49-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ pub struct CompileParameters {
122122
parse(try_from_str = validate_config)
123123
) ]
124124
pub hardware_config: Option<String>,
125+
126+
#[clap(
127+
name = "optimization",
128+
long,
129+
short = 'O',
130+
help = "Optimization level",
131+
arg_enum,
132+
default_value = "default"
133+
)]
134+
pub optimization: crate::OptimizationLevel,
125135
}
126136

127137
fn parse_encoding(encoding: &str) -> Result<&'static Encoding, String> {
@@ -211,7 +221,7 @@ impl CompileParameters {
211221
#[cfg(test)]
212222
mod cli_tests {
213223
use super::CompileParameters;
214-
use crate::{ConfigFormat, FormatOption};
224+
use crate::{ConfigFormat, FormatOption, OptimizationLevel};
215225
use clap::ErrorKind;
216226
use pretty_assertions::assert_eq;
217227

@@ -329,6 +339,44 @@ mod cli_tests {
329339
assert_eq!(parameters.target, Some("x86_64-linux-gnu".to_string()));
330340
}
331341

342+
#[test]
343+
fn test_optimization_levels() {
344+
let parameters = CompileParameters::parse(vec_of_strings!("alpha.st")).unwrap();
345+
346+
assert_eq!(parameters.optimization, OptimizationLevel::Default);
347+
let parameters = CompileParameters::parse(vec_of_strings!("alpha.st", "-Onone")).unwrap();
348+
349+
assert_eq!(parameters.optimization, OptimizationLevel::None);
350+
let parameters =
351+
CompileParameters::parse(vec_of_strings!("alpha.st", "--optimization", "none"))
352+
.unwrap();
353+
assert_eq!(parameters.optimization, OptimizationLevel::None);
354+
355+
let parameters = CompileParameters::parse(vec_of_strings!("alpha.st", "-Oless")).unwrap();
356+
357+
assert_eq!(parameters.optimization, OptimizationLevel::Less);
358+
let parameters =
359+
CompileParameters::parse(vec_of_strings!("alpha.st", "--optimization", "less"))
360+
.unwrap();
361+
assert_eq!(parameters.optimization, OptimizationLevel::Less);
362+
let parameters =
363+
CompileParameters::parse(vec_of_strings!("alpha.st", "-Odefault")).unwrap();
364+
365+
assert_eq!(parameters.optimization, OptimizationLevel::Default);
366+
let parameters =
367+
CompileParameters::parse(vec_of_strings!("alpha.st", "--optimization", "default"))
368+
.unwrap();
369+
assert_eq!(parameters.optimization, OptimizationLevel::Default);
370+
let parameters =
371+
CompileParameters::parse(vec_of_strings!("alpha.st", "-Oaggressive")).unwrap();
372+
373+
assert_eq!(parameters.optimization, OptimizationLevel::Aggressive);
374+
let parameters =
375+
CompileParameters::parse(vec_of_strings!("alpha.st", "--optimization", "aggressive"))
376+
.unwrap();
377+
assert_eq!(parameters.optimization, OptimizationLevel::Aggressive);
378+
}
379+
332380
#[test]
333381
fn test_default_format() {
334382
let parameters = CompileParameters::parse(vec_of_strings!("alpha.st", "--ir")).unwrap();

0 commit comments

Comments
 (0)