mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-22 12:53:47 +00:00
Estimated hours taken: 8
Various changes to get things to work with the DEC Alpha OSF1 C
compiler in grade hlc.gc.
compiler/equiv_type.m:
Change the code for equiv_type__replace_item_in_item_list
so that it is tail recursive, by using an accumulator and
calling list__reverse at the end. This is needed to avoid
a stack overflow (with the default 2M stack limit) when
bootstrapping with the DEC Alpha OSF1 C compiler in grade hlc.gc.
The dec-alpha-osf3.2 `cc' was generating code that used
280 bytes of space per stack frame for this procedure.
compiler/fact_table.m:
Ensure that there is a statement after every label, since
ANSI/ISO C doesn't allow labels at the end of a block without
an empty statement following them.
library/exception.m:
Use a different name for the local typedef in different functions.
This allows the code to work with the DEC Alpha OSF1 C in
`-std' ("ANSI C with popular extensions") mode, rather than
the `-std1' (strict ANSI/ISO C). The supposedly popular
extension here is treating local typedefs as if they were
global. It's probably not worth expending much effort to
cater for non-ANSI modes of compilers, but in this case the
effort is small and arguably the different names are more
descriptive anyway.
runtime/mercury.c:
Avoid the use of a GNU C extension (returning void values).
runtime/mercury_deep_copy.c:
Change `#undef FOO(ARGS)' to `#undef FOO', since the former
is not allowed in ANSI/ISO C.
trace/mercury_trace_internal.c:
Change `#endif FOO' to `#endif /* FOO */', since the former
is not allowed in ANSI/ISO C.
tests/hard_coded/pragma_c_code.m:
tests/hard_coded/type_tables.m:
Use `\\n' rather than `\n' for newlines in strings inside
`pragma c_code'. This is needed because the first level of
slashes gets processed when converting from Mercury to C, and
ANSI/ISO C doesn't allow literal newlines in strings.
63 lines
1.5 KiB
Mathematica
63 lines
1.5 KiB
Mathematica
% A test of the C interface.
|
|
|
|
:- module pragma_c_code.
|
|
:- interface.
|
|
:- import_module io.
|
|
|
|
:- pred main(io__state::di, io__state::uo) is det.
|
|
|
|
:- implementation.
|
|
|
|
main -->
|
|
c_write_string("Hello, world\n"),
|
|
{ c_incr_and_decr(42, Y1, Y2) }, % test integer and multiple
|
|
% output args handling
|
|
c_write_integer(Y1),
|
|
c_write_integer(Y2),
|
|
{ c_get_meaning_of_life(Z) }, % test floats
|
|
c_write_float(Z),
|
|
c_write_cosine(Z). % test c_header_code
|
|
|
|
:- pragma(c_header_code, "#include <stdio.h>").
|
|
|
|
:- pred c_write_string(string::in, io__state::di, io__state::uo) is det.
|
|
|
|
:- pragma(c_code, c_write_string(Message::in, IO0::di, IO::uo), "
|
|
printf(""%s"", Message);
|
|
IO = IO0;
|
|
").
|
|
|
|
:- pred c_incr_and_decr(int::in, int::out, int::out) is det.
|
|
|
|
:- pragma(c_code, c_incr_and_decr(Int0::in, Int1::out, Int2::out), "
|
|
Int1 = Int0 + 1;
|
|
Int2 = Int0 - 1;
|
|
").
|
|
|
|
:- pred c_write_integer(int::in, io__state::di, io__state::uo) is det.
|
|
|
|
:- pragma(c_code, c_write_integer(Int::in, IO0::di, IO::uo), "
|
|
printf(""%ld\\n"", (long) Int);
|
|
IO = IO0;
|
|
").
|
|
|
|
:- pred c_get_meaning_of_life(float::out) is det.
|
|
|
|
:- pragma(c_code, c_get_meaning_of_life(X::out), "X = 42.0;").
|
|
|
|
:- pred c_write_float(float::in, io__state::di, io__state::uo) is det.
|
|
|
|
:- pragma(c_code, c_write_float(X::in, IO0::di, IO::uo), "
|
|
printf(""%.1f\\n"", X);
|
|
IO = IO0;
|
|
").
|
|
|
|
:- pragma(c_header_code, "#include <math.h>").
|
|
|
|
:- pred c_write_cosine(float::in, io__state::di, io__state::uo) is det.
|
|
|
|
:- pragma(c_code, c_write_cosine(X::in, IO0::di, IO::uo), "
|
|
printf(""%.3f\\n"", cos(X));
|
|
IO = IO0;
|
|
").
|