Skip to content

Commit

Permalink
Change how -O works and create -optsize / -optlevel. Update --safe / …
Browse files Browse the repository at this point in the history
…--fast.
  • Loading branch information
lerno committed Sep 16, 2023
1 parent 03345be commit d49365b
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 100 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
- name: Compile run unit tests
run: |
cd test
..\build\${{ matrix.build_type }}\c3c.exe compile-test unit -g1 -O1 --safe
..\build\${{ matrix.build_type }}\c3c.exe compile-test unit -O1
- name: upload artifacts
uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -263,7 +263,7 @@ jobs:
- name: Compile run unit tests
run: |
cd test
../build/c3c compile-test unit -g1 --safe
../build/c3c compile-test unit
- name: Build testproject
run: |
Expand Down Expand Up @@ -368,7 +368,7 @@ jobs:
- name: Compile run unit tests
run: |
cd test
../build/c3c compile-test unit -g1 --safe
../build/c3c compile-test unit
- name: Build testproject
run: |
Expand Down Expand Up @@ -440,7 +440,7 @@ jobs:
- name: Compile run unit tests
run: |
cd test
../build/c3c compile-test unit -g1 --safe
../build/c3c compile-test unit
- name: Build testproject
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,6 @@ Editor plugins can be found at https://github.com/c3lang/editor-plugins.
1. Write the test, either adding to existing test files in `/test/unit/` or add
a new file. (If testing the standard library, put it in the `/test/unit/stdlib/` subdirectory).
2. Make sure that the test functions have the `@test` attribute.
3. Run tests and see that they pass. (Recommended settings: `c3c compile-test --safe -g1 -O0 test/unit`.
3. Run tests and see that they pass. (Recommended settings: `c3c compile-test -O0 test/unit`.
- in this example `test/unit/` is the relative path to the test directory, so adjust as required)
4. Make a pull request for the new tests.
97 changes: 57 additions & 40 deletions src/build/build_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,20 @@ static void usage(void)
OUTPUT(" -C - Only lex, parse and check.");
OUTPUT(" - - Read code from standard in.");
OUTPUT(" -o <file> - Write output to <file>.");
OUTPUT(" -O0 - Optimizations off.");
OUTPUT(" -O1 - Simple optimizations only.");
OUTPUT(" -O2 - Default optimization level.");
OUTPUT(" -O3 - Aggressive optimization.");
OUTPUT(" -Os - Optimize for size.");
OUTPUT(" -Oz - Optimize for tiny size.");
OUTPUT(" -O0+ - No optimization, single module.");
OUTPUT(" -O1+ - Simple optimizations, single module.");
OUTPUT(" -O2+ - Default optimization level, single module");
OUTPUT(" -O3+ - Aggressive optimization, single module.");
OUTPUT(" -Os+ - Optimize for size, single module.");
OUTPUT(" -Oz+ - Optimize for tiny size, single module.");
OUTPUT(" -O0 - Safe, no optimizations, emit debug info.");
OUTPUT(" -O1 - Safe, high optimization, emit debug info.");
OUTPUT(" -O2 - Unsafe, high optimization, emit debug info.");
OUTPUT(" -O3 - Unsafe, highest optimization, relaxed maths, emit debug info.");
OUTPUT(" -O4 - Unsafe, highest optimization, fast maths, emit debug info.");
OUTPUT(" -Os - Unsafe, high optimization, small code, no debug info.");
OUTPUT(" -Oz - Unsafe, high optimization, tiny code, no debug info.");
OUTPUT(" -O0+ - O0, single module.");
OUTPUT(" -O1+ - O1, single module.");
OUTPUT(" -O2+ - O2, single module.");
OUTPUT(" -O3+ - O3, single module.");
OUTPUT(" -O4+ - O4, single module.");
OUTPUT(" -Os+ - Os, single module.");
OUTPUT(" -Oz+ - Oz, single module.");
OUTPUT(" -t1 - Trust level 1 - don't allow $include nor $exec (default).");
OUTPUT(" -t2 - Trust level 2 - allow $include but not $exec / exec directives.");
OUTPUT(" -t3 - Trust level 3 - full trust, allow both include and exec.");
Expand All @@ -127,12 +129,12 @@ static void usage(void)
OUTPUT(" --no-emit-stdlib - Do not output object files (nor asm or ir) for the standard library.");
OUTPUT(" --target <target> - Compile for a particular architecture + OS target.");
OUTPUT(" --threads <number> - Set the number of threads to use for compilation.");
OUTPUT(" --safe - Set mode to 'safe', generating runtime traps on overflows and contract violations.");
OUTPUT(" --fast - Set mode to 'fast', removes runtime traps.");
OUTPUT(" --safe=<yes|no> - Turn safety (contracts, runtime bounds checking, null pointer checks etc) on or off.");
OUTPUT(" --optlevel=<option> - Code optimization level: none, less, more, max.");
OUTPUT(" --optsize=<option> - Code size optimization: none, small, tiny.");
OUTPUT("");
OUTPUT(" -g - Emit full debug info.");
OUTPUT(" -g - Emit debug info.");
OUTPUT(" -g0 - Emit no debug info.");
OUTPUT(" -gline-tables-only - Only emit line tables for debugging.");
OUTPUT("");
OUTPUT("");
OUTPUT(" -l <library> - Link with the library provided.");
Expand Down Expand Up @@ -533,51 +535,59 @@ static void parse_option(BuildOptions *options)
case 'O':
if (match_shortopt("O0+"))
{
options->optimization_setting_override = OPT_SETTING_O0_PLUS;
options->optsetting = OPT_SETTING_O0_PLUS;
}
else if (match_shortopt("O0"))
{
options->optimization_setting_override = OPT_SETTING_O0;
options->optsetting = OPT_SETTING_O0;
}
else if (match_shortopt("O1+"))
{
options->optimization_setting_override = OPT_SETTING_O1_PLUS;
options->optsetting = OPT_SETTING_O1_PLUS;
}
else if (match_shortopt("O1"))
{
options->optimization_setting_override = OPT_SETTING_O1;
options->optsetting = OPT_SETTING_O1;
}
else if (match_shortopt("O2+"))
{
options->optimization_setting_override = OPT_SETTING_O2_PLUS;
options->optsetting = OPT_SETTING_O2_PLUS;
}
else if (match_shortopt("O2"))
{
options->optimization_setting_override = OPT_SETTING_O2;
options->optsetting = OPT_SETTING_O2;
}
else if (match_shortopt("O3+"))
{
options->optimization_setting_override = OPT_SETTING_O3_PLUS;
options->optsetting = OPT_SETTING_O3_PLUS;
}
else if (match_shortopt("O3"))
{
options->optimization_setting_override = OPT_SETTING_O3;
options->optsetting = OPT_SETTING_O3;
}
else if (match_shortopt("O4+"))
{
options->optsetting = OPT_SETTING_O4_PLUS;
}
else if (match_shortopt("O4"))
{
options->optsetting = OPT_SETTING_O4;
}
else if (match_shortopt("Os+"))
{
options->optimization_setting_override = OPT_SETTING_OSMALL_PLUS;
options->optsetting = OPT_SETTING_OSMALL_PLUS;
}
else if (match_shortopt("Os"))
{
options->optimization_setting_override = OPT_SETTING_OSMALL;
options->optsetting = OPT_SETTING_OSMALL;
}
else if (match_shortopt("Oz+"))
{
options->optimization_setting_override = OPT_SETTING_OTINY_PLUS;
options->optsetting = OPT_SETTING_OTINY_PLUS;
}
else if (match_shortopt("Oz"))
{
options->optimization_setting_override = OPT_SETTING_OTINY;
options->optsetting = OPT_SETTING_OTINY;
}
else
{
Expand Down Expand Up @@ -668,6 +678,21 @@ static void parse_option(BuildOptions *options)
options->fp_math = (FpOpt)parse_multi_option(argopt, 3, fp_math);
return;
}
if ((argopt = match_argopt("optsize")))
{
options->optsize = (SizeOptimizationLevel)parse_multi_option(argopt, 3, optsizes);
return;
}
if ((argopt = match_argopt("optlevel")))
{
options->optlevel = (OptimizationLevel)parse_multi_option(argopt, 4, optlevels);
return;
}
if ((argopt = match_argopt("safe")))
{
options->safety_level = (SafetyLevel)parse_multi_option(argopt, 2, safety_levels);
return;
}
if (match_longopt("strip-unused"))
{
options->no_strip_unused = false;
Expand Down Expand Up @@ -967,16 +992,6 @@ static void parse_option(BuildOptions *options)
options->linuxpaths.crtbegin = check_dir(next_arg());
return;
}
if (match_longopt("safe"))
{
options->safe_mode = 1;
return;
}
if (match_longopt("fast"))
{
options->safe_mode = 0;
return;
}
if (match_longopt("benchmarking"))
{
options->benchmarking = true;
Expand Down Expand Up @@ -1015,9 +1030,11 @@ BuildOptions parse_arguments(int argc, const char *argv[])
BuildOptions build_options = {
.path = ".",
.emit_llvm = false,
.optimization_setting_override = OPT_SETTING_NOT_SET,
.optsetting = OPT_SETTING_NOT_SET,
.debug_info_override = DEBUG_INFO_NOT_SET,
.safe_mode = -1,
.safety_level = SAFETY_NOT_SET,
.optlevel = OPTIMIZATION_NOT_SET,
.optsize = SIZE_OPTIMIZATION_NOT_SET,
.build_threads = cpus(),
.command = COMMAND_MISSING,
.reloc_model = RELOC_DEFAULT,
Expand Down
42 changes: 36 additions & 6 deletions src/build/build_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ typedef enum
DIAG_ERROR,
} DiagnosticsSeverity;

typedef enum
{
SAFETY_NOT_SET = -1,
SAFETY_OFF = 0,
SAFETY_ON = 1,
} SafetyLevel;

typedef enum
{
TRUST_NONE,
Expand Down Expand Up @@ -103,6 +110,8 @@ typedef enum
OPT_SETTING_O2_PLUS,
OPT_SETTING_O3,
OPT_SETTING_O3_PLUS,
OPT_SETTING_O4,
OPT_SETTING_O4_PLUS,
OPT_SETTING_OSMALL,
OPT_SETTING_OSMALL_PLUS,
OPT_SETTING_OTINY,
Expand Down Expand Up @@ -207,6 +216,24 @@ static const char *fp_math[3] = {
[FP_FAST] = "fast",
};

static const char *optlevels[4] = {
[OPTIMIZATION_NONE] = "none",
[OPTIMIZATION_LESS] = "less",
[OPTIMIZATION_MORE] = "more",
[OPTIMIZATION_AGGRESSIVE] = "max",
};

static const char *optsizes[3] = {
[SIZE_OPTIMIZATION_NONE] = "none",
[SIZE_OPTIMIZATION_SMALL] = "small",
[SIZE_OPTIMIZATION_TINY] = "more",
};

static const char *safety_levels[2] = {
[SAFETY_OFF] = "no",
[SAFETY_ON] = "yes",
};

static const char *riscv_capability[3] = {
[RISCVFLOAT_NONE] = "none",
[RISCVFLOAT_FLOAT] = "float",
Expand Down Expand Up @@ -342,10 +369,10 @@ typedef struct BuildOptions_
CompileOption compile_option;
TrustLevel trust_level;
DiagnosticsSeverity severity[DIAG_END_SENTINEL];
OptimizationSetting optimization_setting_override;
OptimizationSetting optsetting;
DebugInfo debug_info_override;
ArchOsTarget arch_os_target_override;
int safe_mode;
SafetyLevel safety_level;
bool emit_llvm;
bool emit_asm;
bool benchmark_mode;
Expand All @@ -368,6 +395,8 @@ typedef struct BuildOptions_
X86VectorCapability x86_vector_capability;
X86CpuSet x86_cpu_set;
FpOpt fp_math;
OptimizationLevel optlevel;
SizeOptimizationLevel optsize;
RiscvFloatCapability riscv_float_capability;
MemoryEnvironment memory_environment;
bool no_strip_unused;
Expand Down Expand Up @@ -460,9 +489,10 @@ typedef struct
bool no_emit_stdlib;
int build_threads;
TrustLevel trust_level;
OptimizationLevel optimization_level;
OptimizationSetting optsetting;
OptimizationLevel optlevel;
MemoryEnvironment memory_environment;
SizeOptimizationLevel size_optimization_level;
SizeOptimizationLevel optsize;
bool single_module;
DebugInfo debug_info;
RelocModel reloc_model;
Expand All @@ -482,9 +512,9 @@ typedef struct
StructReturn x86_struct_return : 3;
X86VectorCapability x86_vector_capability : 4;
RiscvFloatCapability riscv_float_capability : 4;
FpOpt fp_math : 4;
bool trap_on_wrap : 1;
bool safe_mode : 1;
FpOpt fp_math;
SafetyLevel safe_mode;
X86CpuSet x86_cpu_set;
} feature;
struct
Expand Down
Loading

0 comments on commit d49365b

Please sign in to comment.