.\" Made by website/plain/build.mjs from website/words/sg-emit-c.md; change that file. .TH SG-EMIT-C 1 2026-11-28 "SG" "SG commands" .SH NAME sg emit-c \- prints the C that sg build compiles, for reading, for a sanitizer, or for a build of your own .SH SYNOPSIS .TP \fBsg emit\-c file.sg, sg emit\-c folder\fR prints the C of a debug build of the program .TP \fBsg emit\-c path \-\-release\fR the C of a release build: overflow wraps, and no check of a debug build is in it .TP \fBsg emit\-c path \-\-test\fR the C of the program that runs the tests, in place of main .TP \fBsg emit\-c path \-\-library\fR the C of a library: the program's export functions, with no main .TP \fBsg emit\-c path \-\-shared\fR the same C as --library, which a static and a shared library share .TP \fBsg emit\-c path \-\-freestanding\fR the C of an object for a machine without an operating system .TP \fBsg emit\-c path \-L folder, \-Lfolder\fR looks for the libraries of extern blocks in that folder too; given once for each folder .TP \fBsg emit\-c \-\-help\fR prints this page .SH DESCRIPTION \fBsg emit\-c\fR checks the program as \fBsg\fR does and prints, on the standard output, the C that \fBsg build\fR would compile with the same flags. Nothing is compiled and no file is written: the C goes where the command line sends it, \fB> main.c\fR or a pipe. A reader that closes early, such as \fBhead\fR, ends it quietly. .PP The C is plain C11 and the whole program: the runtime of SG first, then the program's types and functions. The lines at its top (\fB#define SG_THREADS 0\fR) choose the parts of the runtime the program uses. A function of SG keeps its name with \fBf_\fR before it, a variable with \fBv_\fR, so the C can be read beside the source. Before each function and statement a \fB#line\fR names its line of the \fB\&.sg\fR file, by the file's absolute path, so a debugger and the C compiler's own messages point at SG; a C file named by an \fBextern "./native.c"\fR block is included by its absolute path too. The C is therefore for this machine and these files. .PP It is the input of a build by hand: with flags of your own, a sanitizer, or another C compiler. A program needs \fB\-std=c11\fR and \fB\-ffp\-contract=off\fR, which keeps a multiply and an add apart so fractional results stay the interpreter's, and on Linux and macOS \fB\-lm\fR and \fB\-pthread\fR\&. \fBsg build\fR also gives a debug build \fB\-g\fR, and on Linux \fB\-fstack\-clash\-protection\fR\&. When an \fBextern\fR block names a library, the first line of the C says what to link: \fB/* sg\-link: \-lz */\fR, the name found first among those the block gives. To find it, the C compiler is asked where it looks, with the \fB\-L\fR folders before. .PP \fB\-\-release\fR, \fB\-\-test\fR, \fB\-\-library\fR, \fB\-\-shared\fR and \fB\-\-freestanding\fR choose what is printed as they choose what \fBsg build\fR makes. \fB\-\-release\fR goes with any of them; the others exclude each other. \fB\-\-library\fR prints the C of the library alone, and \fB\-\-shared\fR the same C, since a static and a shared library differ only in how the C compiler links them: the header is written by \fBsg build \-\-library\fR\&. .PP The exit code is 0 when the C was printed, 1 when the checker refuses the program, and 2 for a command line that is not a usage and for a path that does not lead anywhere. .SH EXAMPLES .SS "The C of a program" The file twice.sg: .PP .RS 4 .nf int twice(int x) { return x * 2 } void main() { print(twice(21)) } .fi .RE .PP .RS 4 .nf $ sg emit-c twice.sg > twice.c $ grep -E '^static .* f_[a-z]+\e(.*\e);$' twice.c static int32_t f_twice(int32_t v_x); static void f_main(void); $ cc -std=c11 -ffp-contract=off twice.c -o twice -lm -pthread $ ./twice 42 .fi .RE .PP The two lines \fBgrep\fR finds are the program's own functions, declared before the rest; everything else in \fBtwice.c\fR is the runtime. Compiled by hand with those flags, it is the same program \fBsg build\fR makes. .SS "What a debug build checks" .RS 4 .nf $ sg emit-c twice.sg | grep 'v_x,' return sg_mul_int(v_x, ((int32_t)2LL), sg_loc_0); $ sg emit-c twice.sg | grep '^static const char sg_loc_0' static const char sg_loc_0[] = " --> twice.sg:2:12\en 2 | return x * 2\en | ^^^^^\en" "\e0\e0"; $ sg emit-c twice.sg --release | grep 'v_x,' return sg_wmul_int(v_x, ((int32_t)2LL)); $ sg emit-c twice.sg --release > fast.c $ cc -std=c11 -O3 -ffp-contract=off fast.c -o fast -lm -pthread $ ./fast 42 .fi .RE .PP In a debug build \fBx * 2\fR is a call that checks for overflow, and is given the place to name when it panics: the line, written out as the message shows it. In a release build the same multiplication wraps, and the place is not there. \fB\-O3\fR is a flag of your own; \fBsg build \-\-release\fR compiles with \fB\-O2\fR, or takes it after \fB\-\-\fR\&. .SS "The C of the tests" The file fines.sg: .PP .RS 4 .nf int fineCents(int daysLate) { return daysLate * 25 } test "a day late costs 25 cents" { assert(fineCents(1) == 25) } test "on time costs nothing" { assert(fineCents(0) == 0) } .fi .RE .PP .RS 4 .nf $ sg emit-c fines.sg --test | grep '", t_' { "a day late costs 25 cents", t_0 }, { "on time costs nothing", t_1 }, $ sg emit-c fines.sg error: there is no `main` function --> fines.sg:11:2 11 | } | ^ hint: this file holds tests; they are run by the test command: `sg test ` .fi .RE .PP With \fB\-\-test\fR the C holds each test as a function and a table of their names, which its \fBmain\fR runs as \fBsg test \-\-compiled\fR does. Without it the tests are left out, as \fBsg build\fR leaves them out, and this file has nothing else to run. .SS "A library and an object without an operating system" The file temps/temps.sg: .PP .RS 4 .nf export double toFahrenheit(double celsius) { return celsius * 9.0 / 5.0 + 32.0 } export bool isFreezing(double celsius) { return celsius <= 0.0 } .fi .RE .PP The file device/device.sg: .PP .RS 4 .nf void onPanic(read string message) { } export ulong sumTo(ulong n) { ulong total = 0 for i in 1..n + 1 { total += i } return total } .fi .RE .PP .RS 4 .nf $ sg emit-c temps --library | grep '^SG_EXPORT' SG_EXPORT double toFahrenheit(double a0); SG_EXPORT bool isFreezing(double a0); $ sg emit-c device --freestanding | grep -m1 '^#define SG_FREESTANDING' #define SG_FREESTANDING 1 $ sg emit-c device --freestanding | grep '^SG_EXPORT' SG_EXPORT uint64_t sumTo(uint64_t a0); .fi .RE .PP An \fBexport\fR function keeps its own name in the C, the one C calls, beside the \fBf_\fR function of SG it calls. \fBSG_FREESTANDING\fR leaves out of the runtime what needs a system, so the object takes nothing of C's library. .SS "A library of C, and -L" The file maths/pasha.c: .PP .RS 4 .nf #include int32_t pasha_twice(int32_t value) { return value * 2; } .fi .RE .PP The file maths/main.sg: .PP .RS 4 .nf extern "pashacalc", "pasha" { int pasha_twice(int value) } void main() { print(unsafe pasha_twice(21)) } .fi .RE .PP .RS 4 .nf $ cd maths $ mkdir -p vendor $ cc -c pasha.c -o pasha.o $ ar rcs vendor/libpasha.a pasha.o $ sg emit-c main.sg 2>&1 | head -1 error: none of the libraries this `extern` block names was found: `pashacalc`, `pasha` $ sg emit-c main.sg -L vendor > main.c $ head -1 main.c /* sg-link: -lpasha */ $ cc -std=c11 -ffp-contract=off main.c -o main -Lvendor -lpasha -lm -pthread $ ./main 42 .fi .RE .PP The block names a library by two names; \fBlibpasha.a\fR in \fBvendor\fR is the one found, and the first line of the C says to link it. Without \fB\-L\fR the library is not where the C compiler looks, and the C is not printed: the message goes on to say where it looked, and gives the hint of \fB\-L\fR\&. .SH "MISTAKES IT TELLS" .SS "-o and flags of C" .RS 4 .nf $ sg emit-c twice.sg -o twice.c 2> usage.txt; echo $? 2 $ grep 'sg emit-c' usage.txt sg emit-c [--release] [--test | --library | --shared | --freestanding] [-L folder]... $ sg emit-c twice.sg -- -O3 > /dev/null 2>&1; echo $? 2 .fi .RE .PP \fBsg emit\-c\fR writes to its output, so \fB\-o\fR is not one of its flags, and it compiles nothing, so neither are flags of C after \fB\-\-\fR\&. Both are a usage error: the usage of every command is printed, and nothing else. The same goes for a flag given twice, a second program, and two of \fB\-\-test\fR, \fB\-\-library\fR, \fB\-\-shared\fR and \fB\-\-freestanding\fR\&. .SS "A library without --library" .RS 4 .nf $ sg emit-c temps error: there is no `main` function --> temps/temps.sg:7:2 7 | } | ^ hint: a library that C calls through its `export` functions is printed with `sg emit-c --library` .fi .RE .PP Without \fB\-\-library\fR the folder is read as a program, which starts at \fBmain\fR\&. \fBsg emit\-c temps \-\-library\fR prints its C; \fBsg temps\fR and \fBsg build temps\fR give the hint of \fBsg build temps \-\-library\fR\&. .SS "A program the checker refuses" The file late.sg: .PP .RS 4 .nf void main() { int days = 3 days = "three" print(days) } .fi .RE .PP .RS 4 .nf $ sg emit-c late.sg; echo $? error: expected `int`, found `string` --> late.sg:3:12 3 | days = "three" | ^^^^^^^ 1 .fi .RE .PP The error is the one \fBsg late.sg\fR gives, and no C is printed, not even a part of it. .SS "A path that leads nowhere" .RS 4 .nf $ sg emit-c nothere.sg; echo $? error: cannot read nothere.sg: No such file or directory (os error 2) 2 $ sg emit-c twice.sg -L nowhere; echo $? error: -L nowhere: no such folder 2 .fi .RE .SH "SEE ALSO" .BR sg-build (1), .BR sg (1), .BR sg-test (1), .BR sg-extern (7), .BR sg-export (7), .BR sg-test (7) .PP https://sg\-lang.org/reference/sg-emit-c.html