Skip to content

Commit

Permalink
Merge branch 'master' into builtin_functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaith authored Mar 30, 2022
2 parents 84eddfc + fd520d7 commit f207776
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 33 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion book/src/using_rusty.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ for that and link with libc when we're done. This program can also be found at
done. Otherwise, the program will most likely crash (because it tries to return to a function that never
existed).

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

### Optimization
`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`).

To use an optimization, the flag `-O` or `--optimization` is required:

- `rustyc -c "**/*.st" -O none`
- `rustyc -c "**/*.st" -O less`
- `rustyc -c "**/*.st" -O default`
- `rustyc -c "**/*.st" -O aggressive`

By default `rustyc` will use `default` which corresponds to clang's `-O2`.

### Linking an executable
Instead, you can also compile this into an executable and run it:
```bash
Expand Down
22 changes: 20 additions & 2 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ release=0
debug=0
container=0
assume_linux=0
junit=0

CONTAINER_NAME='rust-llvm'

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

function generate_sources() {
Expand Down Expand Up @@ -149,6 +161,9 @@ function run_in_container() {
if [[ $test -ne 0 ]]; then
params="$params --test"
fi
if [[ $junit -ne 0 ]]; then
params="$params --junit"
fi
if [[ $doc -ne 0 ]]; then
params="$params --doc"
fi
Expand Down Expand Up @@ -179,7 +194,7 @@ function run_in_container() {
set -o errexit -o pipefail -o noclobber -o nounset

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

check_env
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
Expand Down Expand Up @@ -231,6 +246,9 @@ while true; do
--test)
test=1
;;
--junit)
junit=1
;;
--coverage)
coverage=1
;;
Expand Down
50 changes: 49 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ pub struct CompileParameters {
parse(try_from_str = validate_config)
) ]
pub hardware_config: Option<String>,

#[clap(
name = "optimization",
long,
short = 'O',
help = "Optimization level",
arg_enum,
default_value = "default"
)]
pub optimization: crate::OptimizationLevel,
}

fn parse_encoding(encoding: &str) -> Result<&'static Encoding, String> {
Expand Down Expand Up @@ -211,7 +221,7 @@ impl CompileParameters {
#[cfg(test)]
mod cli_tests {
use super::CompileParameters;
use crate::{ConfigFormat, FormatOption};
use crate::{ConfigFormat, FormatOption, OptimizationLevel};
use clap::ErrorKind;
use pretty_assertions::assert_eq;

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

#[test]
fn test_optimization_levels() {
let parameters = CompileParameters::parse(vec_of_strings!("alpha.st")).unwrap();

assert_eq!(parameters.optimization, OptimizationLevel::Default);
let parameters = CompileParameters::parse(vec_of_strings!("alpha.st", "-Onone")).unwrap();

assert_eq!(parameters.optimization, OptimizationLevel::None);
let parameters =
CompileParameters::parse(vec_of_strings!("alpha.st", "--optimization", "none"))
.unwrap();
assert_eq!(parameters.optimization, OptimizationLevel::None);

let parameters = CompileParameters::parse(vec_of_strings!("alpha.st", "-Oless")).unwrap();

assert_eq!(parameters.optimization, OptimizationLevel::Less);
let parameters =
CompileParameters::parse(vec_of_strings!("alpha.st", "--optimization", "less"))
.unwrap();
assert_eq!(parameters.optimization, OptimizationLevel::Less);
let parameters =
CompileParameters::parse(vec_of_strings!("alpha.st", "-Odefault")).unwrap();

assert_eq!(parameters.optimization, OptimizationLevel::Default);
let parameters =
CompileParameters::parse(vec_of_strings!("alpha.st", "--optimization", "default"))
.unwrap();
assert_eq!(parameters.optimization, OptimizationLevel::Default);
let parameters =
CompileParameters::parse(vec_of_strings!("alpha.st", "-Oaggressive")).unwrap();

assert_eq!(parameters.optimization, OptimizationLevel::Aggressive);
let parameters =
CompileParameters::parse(vec_of_strings!("alpha.st", "--optimization", "aggressive"))
.unwrap();
assert_eq!(parameters.optimization, OptimizationLevel::Aggressive);
}

#[test]
fn test_default_format() {
let parameters = CompileParameters::parse(vec_of_strings!("alpha.st", "--ir")).unwrap();
Expand Down
Loading

0 comments on commit f207776

Please sign in to comment.