Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate definitions in generated C #203

Open
rmn30 opened this issue Jan 27, 2023 · 12 comments
Open

Duplicate definitions in generated C #203

rmn30 opened this issue Jan 27, 2023 · 12 comments

Comments

@rmn30
Copy link
Contributor

rmn30 commented Jan 27, 2023

When attempting to compile the current version of this PR: riscv/sail-riscv#197 I get an error when compiling the C file generated by Sail:

make csim
gcc -g  -I /mnt/rust/.opam/4.14.0/share/sail/lib -I c_emulator   -I c_emulator/SoftFloat-3e/source/include -fcommon -O3 -flto generated_definitions/c/riscv_model_RV64.c c_emulator/riscv_prelude.c c_emulator/riscv_platform_impl.c c_emulator/riscv_platform.c c_emulator/riscv_softfloat.c c_emulator/riscv_sim.c /mnt/rust/.opam/4.14.0/share/sail/lib/*.c -lgmp -lz c_emulator/SoftFloat-3e/build/Linux-RISCV-GCC/softfloat.a -o c_emulator/riscv_sim_RV64
generated_definitions/c/riscv_model_RV64.c:64842:6: error: redefinition of ‘zinternal_errorzIUMemoryOpResultzIozKzK’
 void zinternal_errorzIUMemoryOpResultzIozKzK(struct zMemoryOpResultzIozK *zcbz355, sail_string zfile, sail_int zline, sail_string zs)
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
generated_definitions/c/riscv_model_RV64.c:64623:6: note: previous definition of ‘zinternal_errorzIUMemoryOpResultzIozKzK’ was here
 void zinternal_errorzIUMemoryOpResultzIozKzK(struct zMemoryOpResultzIozK *zcbz350, sail_string zfile, sail_int zline, sail_string zs)
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
generated_definitions/c/riscv_model_RV64.c:64885:15: error: redefinition of ‘zinternal_errorzIERetiredz5zK’
 enum zRetired zinternal_errorzIERetiredz5zK(sail_string zfile, sail_int zline, sail_string zs)
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
generated_definitions/c/riscv_model_RV64.c:64710:15: note: previous definition of ‘zinternal_errorzIERetiredz5zK’ was here
 enum zRetired zinternal_errorzIERetiredz5zK(sail_string zfile, sail_int zline, sail_string zs)

I've not been able to reproduce in a smaller example yet. I suspect it is something to do with the interesting type of internal_error:

val internal_error : forall ('a : Type). (string, int, string) -> 'a effect {escape}
function internal_error(file, line, s) = {
    assert (false, file ^ ":" ^ string_of_int(line) ^ ": " ^ s);
    throw Error_internal_error()
}
@rmn30
Copy link
Contributor Author

rmn30 commented Jan 27, 2023

Also the function I introduced that calls internal_error:


/*!
 * Raise an internal error reporting that width w is invalid for access kind, k,
 * and current xlen. The file name and line number should be passed in as the
 * first two arguments using the __FILE__ and __LINE__ built-in macros.
 * This is mainly used to supress Sail warnings about incomplete matches and
 * should be unreachable. See https://github.com/riscv/sail-riscv/issues/194
 * and https://github.com/riscv/sail-riscv/pull/197 .
 */
val report_invalid_width : forall ('a : Type). (string, int, word_width, string) -> 'a effect {escape}
function report_invalid_width(f , l, w, k) -> 'a = {
  internal_error(f, l, "Invalid width, " ^ size_mnemonic(w) ^ ", for " ^ k ^
    " with xlen=" ^ string_of_int(sizeof(xlen)));
}

@Alasdair
Copy link
Collaborator

I guess it probably has something to do with the return type. Do you mean to use throw right after an assert(false) though? The throw will never actually occur.

@Alasdair
Copy link
Collaborator

default Order dec

$include <prelude.sail>

infixr 5 ^^

overload operator ^^ = {concat_str}

union exception = {
    Error_internal_error : unit
}

val internal_error : forall ('a : Type). (string, int, string) -> 'a

function internal_error(file, line, s) = {
    assert (false, file ^^ ":" ^^ dec_str(line) ^^ ": " ^^ s);
    throw Error_internal_error()
}

val internal_error_caller : forall ('a : Type). (string, int, string) -> 'a

function internal_error_caller(f , l, k) -> 'a = {
    internal_error(f, l, k);
}

val main : unit -> unit

function main() = {
    internal_error_caller(__FILE__, __LINE__, "message 1");
    internal_error(__FILE__, __LINE__, "message 2");
}

Should be a fairly minimal example

@Alasdair
Copy link
Collaborator

What it's doing is it does one round of monomorphisation, specialising internal_error and internal_error_caller, then it does a second round because the call to internal_error within internal_error_caller can now be monomorphised within the body of the specialised internal_error_caller producing the duplicate definition.

@Alasdair
Copy link
Collaborator

Should be fixed by 21314bc

@rmn30
Copy link
Contributor Author

rmn30 commented Jan 27, 2023

Thanks! Re. the throw at the end of internal_error I think it is required to get it to type check as otherwise Sail is not aware that it never returns? Perhaps it should be rewritten to just throw Internal_Error(string).

@rmn30
Copy link
Contributor Author

rmn30 commented Jan 27, 2023

I can confirm it now compiles!

@Alasdair
Copy link
Collaborator

If this is needed for the RISC-V spec would it make sense to do a 0.15.1 point release?

@rmn30
Copy link
Contributor Author

rmn30 commented Jan 27, 2023

It is blocking the merge of the above PR so yes please. There are also some fixes to SMT I'd like to have in a release :-)

@arichardson
Copy link
Contributor

If this is needed for the RISC-V spec would it make sense to do a 0.15.1 point release?

FYI 444132c causes lots of warnings in the RISC-V model. I'm not sure how to fix those so it would be good to check that they are indeed valid warnings before releasing the fix.

@Alasdair
Copy link
Collaborator

I made that warning one that only prints at most once so it prints less often. I think most of the issues in the RISC-V spec are valid warnings though. I fixed an issue with spurious warnings for mappings, but the definitions in https://github.com/riscv/sail-riscv/blob/master/model/prelude_mapping.sail are in the wrong order to do what I think is intended so the warnings for those are valid.

@Alasdair
Copy link
Collaborator

I will see about making a pull request to fix that file

avsm pushed a commit to ocaml/opam-repository that referenced this issue Aug 30, 2023
…_manifest, sail_lem_backend, sail_latex_backend, sail_doc_backend, sail_coq_backend, sail_c_backend, sail and libsail (0.16)

CHANGES:

##### New documentation backend

A new documentation backend for integrating with Asciidoctor has been
added.

##### Automatic formatting (EXPERIMENTAL)

The `sail -fmt` option can be used to automatically format Sail
source. This currently misses some features and can produce ugly
output in some known cases, so is not ready for serious usage yet.

##### Fixes

Various bugfixes including:

* Issue 203: rems-project/sail#203
* Issue 202: rems-project/sail#202
* Issue 188: rems-project/sail#188
* Issue 187: rems-project/sail#187
* Issue 277: rems-project/sail#277

Various mapping issues such as:

* Issue 244: rems-project/sail#244

As well as other minor issues

The `val cast` syntax and support for implict casts is now entirely
removed, as mentioned in the previous release changes. The flags are
still allowed (to avoid breaking Makefiles) but no longer do anything.

The pattern completeness checker has been improved and is now context
sensitive in some cases.
nberth pushed a commit to nberth/opam-repository that referenced this issue Jun 18, 2024
…_manifest, sail_lem_backend, sail_latex_backend, sail_doc_backend, sail_coq_backend, sail_c_backend, sail and libsail (0.16)

CHANGES:

##### New documentation backend

A new documentation backend for integrating with Asciidoctor has been
added.

##### Automatic formatting (EXPERIMENTAL)

The `sail -fmt` option can be used to automatically format Sail
source. This currently misses some features and can produce ugly
output in some known cases, so is not ready for serious usage yet.

##### Fixes

Various bugfixes including:

* Issue 203: rems-project/sail#203
* Issue 202: rems-project/sail#202
* Issue 188: rems-project/sail#188
* Issue 187: rems-project/sail#187
* Issue 277: rems-project/sail#277

Various mapping issues such as:

* Issue 244: rems-project/sail#244

As well as other minor issues

The `val cast` syntax and support for implict casts is now entirely
removed, as mentioned in the previous release changes. The flags are
still allowed (to avoid breaking Makefiles) but no longer do anything.

The pattern completeness checker has been improved and is now context
sensitive in some cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants