- 所有 tcc 文件/内容/宏改名为 pcc (tcc.c->pcc.c, tccelf.c->pccelf.c, libtcc.c->libpcc.c 等) - 支持架构: i386, x86_64, ARM, ARM64, RISC-V, C67 - 支持格式: PE, ELF, Mach-O, COFF - 完整预处理、代码生成、链接器、调试信息 - 构建成功: pcc.exe (x86_64 Windows) - 功能测试通过: 递归/结构体/浮点/switch/循环 - 自编译测试通过: pcc 可以编译自身
1987 行
64 KiB
Plaintext
1987 行
64 KiB
Plaintext
\input texinfo @c -*- texinfo -*-
|
|
@c %**start of header
|
|
@setfilename pcc-doc.info
|
|
@settitle Paze C Compiler Reference Documentation
|
|
@dircategory Software development
|
|
@direntry
|
|
* PCC: (pcc-doc). The Paze C Compiler.
|
|
@end direntry
|
|
@c %**end of header
|
|
|
|
@include config.texi
|
|
|
|
@iftex
|
|
@titlepage
|
|
@afourpaper
|
|
@sp 7
|
|
@center @titlefont{Paze C Compiler Reference Documentation}
|
|
@sp 3
|
|
@end titlepage
|
|
@headings double
|
|
@end iftex
|
|
|
|
@contents
|
|
|
|
@node Top, Introduction, (dir), (dir)
|
|
@top Paze C Compiler Reference Documentation
|
|
|
|
This manual documents version @value{VERSION} of the Paze C Compiler.
|
|
|
|
@menu
|
|
* Introduction:: Introduction to pcc.
|
|
* Invoke:: Invocation of pcc (command line, options).
|
|
* Clang:: ANSI C and extensions.
|
|
* asm:: Assembler syntax.
|
|
* linker:: Output file generation and supported targets.
|
|
* Bounds:: Automatic bounds-checking of C code.
|
|
* Libtcc:: The libpcc library.
|
|
* devel:: Guide for Developers.
|
|
@end menu
|
|
|
|
|
|
@node Introduction
|
|
@chapter Introduction
|
|
|
|
PazeCC (aka PCC) is a small but hyper fast C compiler. Unlike other C
|
|
compilers, it is meant to be self-relying: you do not need an
|
|
external assembler or linker because PCC does that for you.
|
|
|
|
PCC compiles so @emph{fast} that even for big projects @code{Makefile}s may
|
|
not be necessary.
|
|
|
|
PCC not only supports ANSI C, but also most of the ISO C99 standard,
|
|
many ISO C11 features, and many GNUC extensions including inline assembly.
|
|
|
|
PCC can also be used to make @emph{C scripts}, i.e. pieces of C source
|
|
that you run as a Perl or Python script. Compilation is so fast that
|
|
your script will be as fast as if it was an executable.
|
|
|
|
PCC can also automatically generate memory and bound checks
|
|
(@pxref{Bounds}) while allowing all C pointers operations. PCC can do
|
|
these checks even if non patched libraries are used.
|
|
|
|
With @code{libpcc}, you can use PCC as a backend for dynamic code
|
|
generation (@pxref{Libtcc}).
|
|
|
|
PCC supports the following target and platform combinations:
|
|
|
|
@multitable @columnfractions .15 .10 .12 .11 .11 .18 .09
|
|
@item @strong{OS} @tab @strong{i386} @tab @strong{x86-64} @tab @strong{ARM} @tab @strong{ARM64} @tab @strong{RISC-V 64} @tab @strong{C67}
|
|
@item Linux @tab yes @tab yes @tab yes @tab yes @tab yes @tab yes
|
|
@item macOS @tab @tab yes @tab @tab yes @tab @tab
|
|
@item Windows @tab yes @tab yes @tab WinCE @tab yes @tab @tab
|
|
@item Android @tab yes @tab yes @tab yes @tab yes @tab @tab
|
|
@item FreeBSD @tab @tab yes @tab @tab yes @tab @tab
|
|
@item NetBSD @tab @tab yes @tab yes @tab yes @tab @tab
|
|
@item OpenBSD @tab yes @tab yes @tab @tab yes @tab yes @tab
|
|
@item DragonFly @tab @tab yes @tab @tab @tab @tab
|
|
@end multitable
|
|
|
|
The TMS320C67xx (C67) target is a cross compiler for the digital signal
|
|
processors of that family. It outputs COFF instead of ELF and has neither
|
|
@file{libpcc1.a} nor bound checking.
|
|
|
|
For usage on Windows, see also @url{pcc-win32.txt}.
|
|
|
|
@node Invoke
|
|
@chapter Command line invocation
|
|
|
|
@section Quick start
|
|
|
|
@example
|
|
@c man begin SYNOPSIS
|
|
usage: pcc [options] [@var{infile1} @var{infile2}@dots{}] [@option{-run} @var{infile} @var{args}@dots{}]
|
|
@c man end
|
|
@end example
|
|
|
|
@noindent
|
|
@c man begin DESCRIPTION
|
|
PCC options are very much like gcc options. The main difference is that PCC
|
|
can also execute directly the resulting program and give it runtime
|
|
arguments.
|
|
|
|
Here are some examples to understand the logic:
|
|
|
|
@table @code
|
|
@item @samp{pcc -run a.c}
|
|
Compile @file{a.c} and execute it directly
|
|
|
|
@item @samp{pcc -run a.c arg1}
|
|
Compile a.c and execute it directly. arg1 is given as first argument to
|
|
the @code{main()} of a.c.
|
|
|
|
@item @samp{pcc a.c -run b.c arg1}
|
|
Compile @file{a.c} and @file{b.c}, link them together and execute them. arg1 is given
|
|
as first argument to the @code{main()} of the resulting program.
|
|
@ignore
|
|
Because multiple C files are specified, @option{--} are necessary to clearly
|
|
separate the program arguments from the PCC options.
|
|
@end ignore
|
|
|
|
@item @samp{pcc -o myprog a.c b.c}
|
|
Compile @file{a.c} and @file{b.c}, link them and generate the executable @file{myprog}.
|
|
|
|
@item @samp{pcc -o myprog a.o b.o}
|
|
link @file{a.o} and @file{b.o} together and generate the executable @file{myprog}.
|
|
|
|
@item @samp{pcc -c a.c}
|
|
Compile @file{a.c} and generate object file @file{a.o}.
|
|
|
|
@item @samp{pcc -c asmfile.S}
|
|
Preprocess with C preprocess and assemble @file{asmfile.S} and generate
|
|
object file @file{asmfile.o}.
|
|
|
|
@item @samp{pcc -c asmfile.s}
|
|
Assemble (but not preprocess) @file{asmfile.s} and generate object file
|
|
@file{asmfile.o}.
|
|
|
|
@item @samp{pcc -r -o ab.o a.c b.c}
|
|
Compile @file{a.c} and @file{b.c}, link them together and generate the object file @file{ab.o}.
|
|
|
|
@end table
|
|
|
|
Scripting:
|
|
|
|
PCC can be invoked from @emph{scripts}, just as shell scripts. You just
|
|
need to add @code{#!/usr/local/bin/pcc -run} at the start of your C source:
|
|
|
|
@example
|
|
#!/usr/local/bin/pcc -run
|
|
#include <stdio.h>
|
|
|
|
int main()
|
|
@{
|
|
printf("Hello World\n");
|
|
return 0;
|
|
@}
|
|
@end example
|
|
|
|
PCC can read C source code from @emph{standard input} when @option{-} is used in
|
|
place of @option{infile}. Example:
|
|
|
|
@example
|
|
echo 'main()@{puts("hello");@}' | pcc -run -
|
|
@end example
|
|
@c man end
|
|
|
|
@section Option summary
|
|
|
|
@c man begin OPTIONS
|
|
@subheading General Options
|
|
|
|
@table @option
|
|
@item -c
|
|
Generate an object file.
|
|
|
|
@item -o outfile
|
|
Put object file, executable, or dll into output file @file{outfile}.
|
|
|
|
@item -run source [args...]
|
|
Compile file @var{source} and run it with the command line arguments
|
|
@var{args}. In order to be able to give more than one argument to a
|
|
script, several PCC options can be given @emph{after} the
|
|
@option{-run} option, separated by spaces:
|
|
@example
|
|
pcc "-run -L/usr/X11R6/lib -lX11" ex4.c
|
|
@end example
|
|
In a script, it gives the following header:
|
|
@example
|
|
#!/usr/local/bin/pcc -run -L/usr/X11R6/lib -lX11
|
|
@end example
|
|
|
|
@item -rstdin file
|
|
With @option{-run}: reopen standard input from @file{file} before executing
|
|
the program. This is mainly useful when the C source itself was read from
|
|
standard input (see @option{-} above).
|
|
|
|
@item -v
|
|
Display PCC version.
|
|
|
|
@item -vv
|
|
Show included files. As sole argument, print search dirs. -vvv shows tries too.
|
|
|
|
@item -bench
|
|
Display compilation statistics.
|
|
|
|
@item -dumpmachine
|
|
Print target machine architecture triplet.
|
|
|
|
@item -dumpversion
|
|
Print PCC compiler version.
|
|
|
|
@end table
|
|
|
|
@subheading Preprocessor Options
|
|
|
|
@table @option
|
|
@item -Idir
|
|
Specify an additional include path. Include paths are searched in the
|
|
order they are specified.
|
|
|
|
System include paths are always searched after. The defaults are
|
|
@file{@var{tccdir}/include} and then @file{/usr/include}, where
|
|
@var{tccdir} is the pcc private directory (@file{PREFIX/lib/pcc}, see
|
|
@option{-B}; @file{PREFIX} is usually @file{/usr} or @file{/usr/local}).
|
|
On Windows the defaults are @file{@var{tccdir}/include} and
|
|
@file{@var{tccdir}/include/winapi}. Use @option{-vv} as sole argument to
|
|
print the paths of the current build.
|
|
|
|
@item -isystem dir
|
|
Specify a system include path to be added to the defaults.
|
|
|
|
@item -nostdinc
|
|
Do not search the default system include paths; only search include paths
|
|
provided on the command line.
|
|
|
|
@item -include file
|
|
Include @option{file} above each input file.
|
|
|
|
@item -Dsym[=val]
|
|
Define preprocessor symbol @samp{sym} to
|
|
val. If val is not present, its value is @samp{1}. Function-like macros can
|
|
also be defined: @option{-DF(a)=a+1}
|
|
|
|
@item -Usym
|
|
Undefine preprocessor symbol @samp{sym}.
|
|
|
|
@item -E
|
|
Preprocess only, to stdout or file (with -o).
|
|
|
|
@item -P
|
|
Do not output @code{#line} directives.
|
|
|
|
@item -P1
|
|
Output alternative @code{#line} directives.
|
|
|
|
@item -dD, -dM
|
|
Output @code{#define} directives.
|
|
|
|
@item -Wp,-opt
|
|
Same as @option{-opt}.
|
|
|
|
@end table
|
|
|
|
@subheading Compilation Flags
|
|
|
|
Note: each of the following options has a negative form beginning with
|
|
@option{-fno-}.
|
|
|
|
@table @option
|
|
@item -funsigned-char
|
|
Let the @code{char} type be unsigned.
|
|
|
|
@item -fsigned-char
|
|
Let the @code{char} type be signed.
|
|
|
|
@item -fcommon
|
|
Generate common symbols for uninitialized data. The default is
|
|
@option{-fno-common}, which puts tentative definitions directly in the
|
|
bss section.
|
|
|
|
@item -fleading-underscore
|
|
Add a leading underscore at the beginning of each C symbol.
|
|
|
|
@item -fms-extensions
|
|
Allow a MS C compiler extensions to the language. Currently this
|
|
assumes a nested named structure declaration without an identifier
|
|
behaves like an unnamed one.
|
|
|
|
@item -fdollars-in-identifiers
|
|
Allow dollar signs in identifiers
|
|
|
|
@item -freverse-funcargs
|
|
Evaluate function arguments right to left.
|
|
|
|
@item -fgnu89-inline
|
|
@code{extern inline} is like @code{static inline}.
|
|
|
|
@item -fasynchronous-unwind-tables
|
|
Create eh_frame section [on]
|
|
|
|
@item -ftest-coverage
|
|
Create code coverage code. After running the resulting code an executable.tcov
|
|
or sofile.tcov file is generated with code coverage.
|
|
|
|
@end table
|
|
|
|
@subheading Warning Options
|
|
|
|
@table @option
|
|
@item -w
|
|
Disable all warnings.
|
|
|
|
@end table
|
|
|
|
Note: each of the following warning options has a negative form beginning with
|
|
@option{-Wno-}.
|
|
|
|
@table @option
|
|
@item -Wimplicit-function-declaration
|
|
Warn about implicit function declaration (missing prototype).
|
|
|
|
@item -Wdiscarded-qualifiers
|
|
Warn when const is dropped.
|
|
|
|
@item -Wunsupported
|
|
Warn about unsupported GCC features that are ignored by PCC.
|
|
|
|
@item -Wwrite-strings
|
|
Make string constants be of type @code{const char *} instead of @code{char
|
|
*}.
|
|
|
|
@item -Werror
|
|
Abort compilation if a warning is issued. Can be given an option to enable
|
|
the specified warning and turn it into an error, for example
|
|
@option{-Werror=unsupported}.
|
|
|
|
@item -Wall
|
|
Activate some useful warnings (@option{-Wimplicit-function-declaration},
|
|
@option{-Wdiscarded-qualifiers}).
|
|
|
|
@end table
|
|
|
|
@subheading Linker Options
|
|
|
|
@table @option
|
|
@item -Ldir
|
|
Specify an additional static library path for the @option{-l} option. The
|
|
defaults are the pcc private directory (see @option{-B}) and
|
|
@file{/usr/lib}, which the build may replace by the library directory of
|
|
the host (@file{/usr/lib64} or a multiarch triplet directory). On Windows
|
|
the default is @file{@var{tccdir}/lib}. Use @option{-vv} as sole argument
|
|
to print the paths of the current build.
|
|
|
|
@item -lxxx
|
|
Link your program with dynamic library libxxx.so or static library
|
|
libxxx.a. The library is searched in the paths specified by the
|
|
@option{-L} option and @env{LIBRARY_PATH} variable.
|
|
|
|
@item -Bdir
|
|
Set the path where the pcc internal libraries (and include files) can be
|
|
found (default is @file{PREFIX/lib/pcc}).
|
|
|
|
@item -shared
|
|
Generate a shared library instead of an executable.
|
|
|
|
@item -soname name
|
|
set name for shared library to be used at runtime
|
|
|
|
@item -static
|
|
Generate a statically linked executable (default is a shared linked
|
|
executable).
|
|
|
|
@item -rdynamic
|
|
Export global symbols to the dynamic linker. It is useful when a library
|
|
opened with @code{dlopen()} needs to access executable symbols.
|
|
|
|
@item -pthread
|
|
Preprocess with @option{-D_REENTRANT} and link with @option{-lpthread}.
|
|
|
|
@item -r
|
|
Generate an object file combining all input files.
|
|
|
|
@item -nostdlib
|
|
Don't implicitly link with libc, the C runtime files, and libpcc1.
|
|
|
|
@item -Wl,-nostdlib
|
|
Don't search the default library paths (see @option{-L}). Only the paths
|
|
specified with @option{-L} and @env{LIBRARY_PATH} are searched.
|
|
|
|
@item -Wl,-rpath=path
|
|
Put custom search path for dynamic libraries into executable.
|
|
|
|
@item -Wl,-Ipath
|
|
@item -Wl,--dynamic-linker=path
|
|
Set the ELF interpreter (dynamic linker). This defaults to the value of the
|
|
environment variable @env{LD_SO} if set, or a compiled-in default.
|
|
|
|
@item -Wl,--enable-new-dtags
|
|
When putting a custom search path for dynamic libraries into the executable,
|
|
create the new ELF dynamic tag DT_RUNPATH instead of the old legacy DT_RPATH.
|
|
|
|
@item -Wl,--oformat=fmt
|
|
Use @var{fmt} as output format. The supported output formats are:
|
|
@table @code
|
|
@item elf32-i386
|
|
ELF output format (default)
|
|
@item binary
|
|
Binary image (only for executable output)
|
|
@item coff
|
|
COFF output format (only for executable output for TMS320C67xx target)
|
|
@end table
|
|
|
|
@item -Wl,--export-all-symbols
|
|
@item -Wl,--export-dynamic
|
|
Export global symbols to the dynamic linker. It is useful when a library
|
|
opened with @code{dlopen()} needs to access executable symbols.
|
|
|
|
@item -Wl,-subsystem=console/gui/wince/...
|
|
Set type for PE (Windows) executables.
|
|
|
|
@item -Wl,-[Ttext=# | section-alignment=# | file-alignment=# | image-base=# | stack=#]
|
|
Modify executable layout.
|
|
|
|
@item -Wl,-(no-|disable-)[dynamicbase | nxcompat | high-entropy-va | tsaware]
|
|
Set or clear PE (Windows) executable header hardening flags. The
|
|
@option{-Wl,-high-entropy-va} option is supported on x86-64 and ARM64 PE
|
|
targets and implies @option{-Wl,-dynamicbase}. Clearing dynamicbase also
|
|
clears high-entropy-va. When @option{-Wl,-dynamicbase} is used for an
|
|
executable, PCC also enables base relocation emission for Windows ASLR.
|
|
|
|
@item -Wl,-Bsymbolic
|
|
Set DT_SYMBOLIC tag.
|
|
|
|
@item -Wl,-(no-)whole-archive
|
|
Turn on/off linking of all objects in archives.
|
|
|
|
@end table
|
|
|
|
@subheading Debugger Options
|
|
|
|
@table @option
|
|
@item -g
|
|
Generate run time stab debug information so that you get clear run time
|
|
error messages: @code{ test.c:68: in function 'test5()': dereferencing
|
|
invalid pointer} instead of the laconic @code{Segmentation
|
|
fault}.
|
|
|
|
@item -gdwarf[-x]
|
|
Generate run time dwarf debug information instead of stab debug information.
|
|
|
|
@item -b
|
|
Generate additional support code to check memory allocations and array/pointer
|
|
bounds (@pxref{Bounds}). @option{-g} is implied.
|
|
|
|
@item -bt[N]
|
|
Display N callers in stack traces. This is useful with @option{-g} or @option{-b}.
|
|
When activated, @code{__TCC_BACKTRACE__} is defined.
|
|
|
|
With executables, additional support for stack traces is included. A function
|
|
@code{ int pcc_backtrace(const char *fmt, ...); }
|
|
is provided to trigger a stack trace with a message on demand.
|
|
|
|
@end table
|
|
|
|
@subheading Misc Options
|
|
|
|
@table @option
|
|
|
|
@item -std=version
|
|
Define @code{__STDC_VERSION__} to @code{201112} if @option{version} is
|
|
c11 or gnu11; @code{199901} otherwise.
|
|
|
|
@item -x[c|a|b|n]
|
|
Specify content of next input file: respectively C, assembly, binary, or none.
|
|
|
|
@item -O[n]
|
|
Same as @option{-D__OPTIMIZE__} except for -O0. @option{-Os} is treated
|
|
the same as @option{-O1}.
|
|
|
|
@item -M
|
|
Just output makefile fragment with dependencies
|
|
|
|
@item -MM
|
|
Like -M except mention only user header files, not system header files.
|
|
|
|
@item -MD
|
|
Generate makefile fragment with dependencies.
|
|
|
|
@item -MMD
|
|
Like -MD except mention only user header files, not system header files.
|
|
|
|
@item -MF depfile
|
|
Use @file{depfile} as output for -MD.
|
|
|
|
@item -MP
|
|
Mention all dependencies as targets too.
|
|
|
|
@item -print-search-dirs
|
|
Print the configured installation directory and a list of library
|
|
and include directories pcc will search.
|
|
|
|
@item -dt
|
|
With @option{-run}/@option{-E}: auto-define 'test_...' macros
|
|
|
|
@end table
|
|
|
|
@subheading Target Specific Options
|
|
|
|
@table @option
|
|
@item -mms-bitfields
|
|
Use an algorithm for bitfield alignment consistent with MSVC. Default is
|
|
gcc's algorithm.
|
|
|
|
@item -mfloat-abi (ARM only)
|
|
Select the float ABI. Possible values: @code{softfp} and @code{hard}
|
|
|
|
@item -mno-sse
|
|
Do not use sse registers on x86_64
|
|
|
|
@item -m32, -m64
|
|
Pass command line to the i386/x86_64 cross compiler.
|
|
|
|
@end table
|
|
|
|
@subheading macOS Specific Options (Mach-O Targets Only)
|
|
|
|
@table @option
|
|
@item -dynamiclib
|
|
Generate a dynamic library instead of an executable.
|
|
|
|
@item -install_name name
|
|
Set the install name for a dynamic library.
|
|
|
|
@item -flat_namespace
|
|
Use a flat namespace (ignored, accepted for compatibility).
|
|
|
|
@item -two_levelnamespace
|
|
Use a two-level namespace (default, accepted for compatibility).
|
|
|
|
@item -undefined @var{treatment}
|
|
Specify how undefined symbols are treated (accepted for compatibility).
|
|
|
|
@item -compatibility_version version
|
|
Set the compatibility version for a dynamic library.
|
|
|
|
@item -current_version version
|
|
Set the current version for a dynamic library.
|
|
|
|
@end table
|
|
|
|
@subheading Tool Modes
|
|
|
|
@table @option
|
|
@item pcc -ar [crstvx] lib [files]
|
|
Create a static library archive. PCC can function as an @command{ar}
|
|
replacement without requiring an external archiver tool. The
|
|
@code{[abdiopN]} keys are not supported.
|
|
|
|
@item pcc -impdef lib.dll [-v] [-o lib.def] (Windows only)
|
|
Create a @file{.def} definition file from a DLL.
|
|
|
|
@end table
|
|
|
|
Note: GCC options @option{-fx}, @option{-mx}, @option{-arch}, @option{-C},
|
|
@option{--param}, @option{-pedantic}, @option{-pie}, @option{-no-pie},
|
|
@option{-pipe}, @option{-s}, and @option{-traditional} are ignored.
|
|
@option{-Wunsupported} makes PCC warn about them.
|
|
@c man end
|
|
|
|
@c man begin ENVIRONMENT
|
|
Environment variables that affect how pcc operates.
|
|
|
|
@table @option
|
|
|
|
@item CPATH
|
|
@item C_INCLUDE_PATH
|
|
A colon-separated list of directories searched for include files,
|
|
directories given with @option{-I} are searched first.
|
|
|
|
@item LIBRARY_PATH
|
|
A colon-separated list of directories searched for libraries for the
|
|
@option{-l} option, directories given with @option{-L} are searched first.
|
|
|
|
@end table
|
|
|
|
@c man end
|
|
|
|
@ignore
|
|
|
|
@setfilename pcc
|
|
@settitle Paze C Compiler
|
|
|
|
@c man begin SEEALSO
|
|
cpp(1),
|
|
gcc(1)
|
|
@c man end
|
|
|
|
@c man begin AUTHOR
|
|
Fabrice Bellard
|
|
@c man end
|
|
|
|
@end ignore
|
|
|
|
@node Clang
|
|
@chapter C language support
|
|
|
|
@section ANSI C
|
|
|
|
PCC implements all the ANSI C standard, including structure bit fields
|
|
and floating point numbers (@code{long double}, @code{double}, and
|
|
@code{float} fully supported).
|
|
|
|
@section ISOC99 extensions
|
|
|
|
PCC implements many features of the new C standard: ISO C99. Currently
|
|
missing items are: complex and imaginary numbers.
|
|
|
|
Currently implemented ISOC99 features:
|
|
|
|
@itemize
|
|
|
|
@item variable length arrays.
|
|
|
|
@item 64 bit @code{long long} types are fully supported.
|
|
|
|
@item The boolean type @code{_Bool} is supported.
|
|
|
|
@item @code{__func__} is a string variable containing the current
|
|
function name.
|
|
|
|
@item Variadic macros: @code{__VA_ARGS__} can be used for
|
|
function-like macros:
|
|
@example
|
|
#define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)
|
|
@end example
|
|
|
|
@noindent
|
|
@code{dprintf} can then be used with a variable number of parameters.
|
|
|
|
@item Declarations can appear anywhere in a block (as in C++).
|
|
|
|
@item Array and struct/union elements can be initialized in any order by
|
|
using designators:
|
|
@example
|
|
struct @{ int x, y; @} st[10] = @{ [0].x = 1, [0].y = 2 @};
|
|
|
|
int tab[10] = @{ 1, 2, [5] = 5, [9] = 9@};
|
|
@end example
|
|
|
|
@item Compound initializers are supported:
|
|
@example
|
|
int *p = (int [])@{ 1, 2, 3 @};
|
|
@end example
|
|
to initialize a pointer pointing to an initialized array. The same
|
|
works for structures and strings.
|
|
|
|
@item Hexadecimal floating point constants are supported:
|
|
@example
|
|
double d = 0x1234p10;
|
|
@end example
|
|
|
|
@noindent
|
|
is the same as writing
|
|
@example
|
|
double d = 4771840.0;
|
|
@end example
|
|
|
|
@item @code{inline} keyword is supported. Static inline functions are
|
|
emitted at the end of the compilation unit only if used. The behavior
|
|
of @code{extern inline} depends on @option{-fgnu89-inline} (@pxref{Invoke}).
|
|
|
|
@item @code{restrict} keyword is ignored.
|
|
@end itemize
|
|
|
|
@section ISO C11 extensions
|
|
|
|
PCC implements several features of the ISO C11 standard:
|
|
|
|
@itemize
|
|
|
|
@item @code{_Generic} keyword for type-generic expressions:
|
|
@example
|
|
#define print_type(x) _Generic((x), \
|
|
int: "int", \
|
|
float: "float", \
|
|
default: "other")
|
|
@end example
|
|
|
|
@item @code{_Static_assert(expr, msg)} for compile-time assertions.
|
|
The single-argument form @code{_Static_assert(expr)} (C23) is also
|
|
supported.
|
|
|
|
@item @code{_Atomic} type qualifier for atomic types.
|
|
|
|
@item @code{_Thread_local} storage-class specifier for thread-local
|
|
storage. The GCC extension @code{__thread} is also supported.
|
|
|
|
@item @code{_Noreturn} function specifier (equivalent to the
|
|
@code{__attribute__((noreturn))} GNU extension).
|
|
|
|
@item @code{_Alignas(n)} specifier to request alignment of a variable
|
|
or structure field.
|
|
|
|
@item @code{_Alignof} operator to query the alignment of a type or
|
|
expression.
|
|
|
|
@end itemize
|
|
|
|
@section GNU C extensions
|
|
|
|
PCC implements some GNU C extensions:
|
|
|
|
@itemize
|
|
|
|
@item array designators can be used without '=':
|
|
@example
|
|
int a[10] = @{ [0] 1, [5] 2, 3, 4 @};
|
|
@end example
|
|
|
|
@item Structure field designators can be a label:
|
|
@example
|
|
struct @{ int x, y; @} st = @{ x: 1, y: 1@};
|
|
@end example
|
|
instead of
|
|
@example
|
|
struct @{ int x, y; @} st = @{ .x = 1, .y = 1@};
|
|
@end example
|
|
|
|
@item @code{\e} is ASCII character 27.
|
|
|
|
@item case ranges : ranges can be used in @code{case}s:
|
|
@example
|
|
switch(a) @{
|
|
case 1 @dots{} 9:
|
|
printf("range 1 to 9\n");
|
|
break;
|
|
default:
|
|
printf("unexpected\n");
|
|
break;
|
|
@}
|
|
@end example
|
|
|
|
@cindex aligned attribute
|
|
@cindex packed attribute
|
|
@cindex section attribute
|
|
@cindex unused attribute
|
|
@cindex cleanup attribute
|
|
@cindex constructor attribute
|
|
@cindex destructor attribute
|
|
@cindex always_inline attribute
|
|
@cindex alias attribute
|
|
@cindex visibility attribute
|
|
@cindex weak attribute
|
|
@cindex noreturn attribute
|
|
@cindex cdecl attribute
|
|
@cindex stdcall attribute
|
|
@cindex fastcall attribute
|
|
@cindex thiscall attribute
|
|
@cindex regparm attribute
|
|
@cindex mode attribute
|
|
@cindex dllexport attribute
|
|
@cindex dllimport attribute
|
|
@cindex nodecorate attribute
|
|
@cindex format attribute
|
|
@cindex used attribute
|
|
@cindex nodebug attribute
|
|
|
|
@item The keyword @code{__attribute__} is handled to specify variable or
|
|
function attributes. The following attributes are supported:
|
|
@itemize
|
|
|
|
@item @code{aligned(n)}: align a variable or a structure field to n bytes
|
|
(must be a power of two).
|
|
|
|
@item @code{packed}: force alignment of a variable or a structure field to
|
|
1.
|
|
|
|
@item @code{section(name)}: generate function or data in assembly section
|
|
name (name is a string containing the section name) instead of the default
|
|
section.
|
|
|
|
@item @code{unused}: specify that the variable or the function is unused.
|
|
Accepted but currently ignored.
|
|
|
|
@item @code{cleanup(func)}: specify a function to be called automatically
|
|
when the variable goes out of scope. The cleanup function must take one
|
|
parameter, a pointer to a type compatible with the variable, and return
|
|
@code{void}.
|
|
|
|
@item @code{constructor}: specify that the function is to be executed automatically before @code{main()} is called.
|
|
|
|
@item @code{destructor}: specify that the function is to be executed automatically after @code{main()} returns or @code{exit()} is called.
|
|
|
|
@item @code{always_inline}: force inline expansion of the function.
|
|
|
|
@item @code{alias("target")}: declare the function or variable as an alias for symbol @code{target} (a string containing the target symbol name).
|
|
|
|
@item @code{visibility("mode")}: set symbol visibility. Supported modes are @code{"default"}, @code{"hidden"}, @code{"internal"}, and @code{"protected"}.
|
|
|
|
@item @code{weak}: declare the symbol as a weak symbol.
|
|
|
|
@item @code{noreturn}: specify that the function does not return to its caller.
|
|
|
|
@item @code{cdecl}: use standard C calling convention (default).
|
|
|
|
@item @code{stdcall}: use Pascal-like calling convention.
|
|
|
|
@item @code{fastcall}: use fastcall calling convention (i386 only).
|
|
|
|
@item @code{thiscall}: use C++-style thiscall calling convention (i386 only).
|
|
|
|
@item @code{regparm(n)}: use fast i386 calling convention. @var{n} must be
|
|
between 1 and 3. The first @var{n} function parameters are respectively put in
|
|
registers @code{%eax}, @code{%edx} and @code{%ecx}.
|
|
|
|
@item @code{__mode__(mode)}: specify the data type size mode for integer
|
|
variables or typedefs (supported modes: @code{__QI__}, @code{__HI__},
|
|
@code{__SI__}, @code{__DI__}, @code{__word__}). The attribute is only
|
|
taken into account when it is given with the declaration specifiers, that
|
|
is before the declarator:
|
|
@example
|
|
typedef __attribute__ ((__mode__(__QI__))) int int8_type;
|
|
@end example
|
|
|
|
@item @code{dllexport}: export function from dll/executable (win32 only).
|
|
|
|
@item @code{dllimport}: import function from dll/executable (win32 only).
|
|
|
|
@item @code{nodecorate}: do not apply any decorations that would otherwise be applied when exporting function from dll/executable (win32 only).
|
|
|
|
@item @code{nodebug}: suppress debug information for the symbol.
|
|
|
|
@item @code{format(archetype, fmt_idx, chk_idx)}: marks functions that
|
|
take @code{printf}- or @code{scanf}-style format strings. Accepted but
|
|
currently ignored (not checked by PCC).
|
|
|
|
@item @code{used}: indicates that the symbol must be retained even if it
|
|
appears unreferenced. Accepted but currently ignored.
|
|
|
|
@end itemize
|
|
|
|
Here are some examples:
|
|
@example
|
|
int a __attribute__ ((aligned(8), section(".mysection")));
|
|
@end example
|
|
|
|
@noindent
|
|
align variable @code{a} to 8 bytes and put it in section @code{.mysection}.
|
|
|
|
@example
|
|
int my_add(int a, int b) __attribute__ ((section(".mycodesection")))
|
|
@{
|
|
return a + b;
|
|
@}
|
|
@end example
|
|
|
|
@noindent
|
|
generate function @code{my_add} in section @code{.mycodesection}.
|
|
|
|
@example
|
|
void free_ptr(void *p) @{ free(*(void **)p); @}
|
|
void foo(void) @{
|
|
char *buf __attribute__((cleanup(free_ptr))) = malloc(1024);
|
|
/* buf is freed automatically when leaving foo */
|
|
@}
|
|
@end example
|
|
|
|
@noindent
|
|
automatically call @code{free_ptr(&buf)} when variable @code{buf} goes out of scope.
|
|
|
|
@item GNU style variadic macros:
|
|
@example
|
|
#define dprintf(fmt, args@dots{}) printf(fmt, ## args)
|
|
|
|
dprintf("no arg\n");
|
|
dprintf("one arg %d\n", 1);
|
|
@end example
|
|
|
|
@item @code{__FUNCTION__} is interpreted as C99 @code{__func__}
|
|
(so it has not exactly the same semantics as string literal GNUC
|
|
where it is a string literal).
|
|
|
|
@item The @code{__alignof__} keyword can be used as @code{sizeof}
|
|
to get the alignment of a type or an expression.
|
|
|
|
@item The @code{typeof(x)} returns the type of @code{x}.
|
|
@code{x} is an expression or a type.
|
|
|
|
@item Computed gotos: @code{&&label} returns a pointer of type
|
|
@code{void *} on the goto label @code{label}. @code{goto *expr} can be
|
|
used to jump on the pointer resulting from @code{expr}.
|
|
|
|
@item Statement expressions: a compound statement enclosed in
|
|
parentheses may appear as an expression. The value of the expression is
|
|
the value of the last statement. This is commonly used in macros:
|
|
@example
|
|
#define max(a,b) (@{ int _a = (a), _b = (b); _a > _b ? _a : _b; @})
|
|
@end example
|
|
|
|
@item The @code{__label__} keyword declares local labels within a block:
|
|
@example
|
|
@{ __label__ done; /* ... */ done: ; @}
|
|
@end example
|
|
|
|
@item The @code{__extension__} keyword suppresses warnings for GNU
|
|
extensions in strict compilation modes.
|
|
|
|
@item Inline assembly with asm instruction:
|
|
@cindex inline assembly
|
|
@cindex assembly, inline
|
|
@cindex __asm__
|
|
@example
|
|
static inline void * my_memcpy(void * to, const void * from, size_t n)
|
|
@{
|
|
int d0, d1, d2;
|
|
__asm__ __volatile__(
|
|
"rep ; movsl\n\t"
|
|
"testb $2,%b4\n\t"
|
|
"je 1f\n\t"
|
|
"movsw\n"
|
|
"1:\ttestb $1,%b4\n\t"
|
|
"je 2f\n\t"
|
|
"movsb\n"
|
|
"2:"
|
|
: "=&c" (d0), "=&D" (d1), "=&S" (d2)
|
|
:"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
|
|
: "memory");
|
|
return (to);
|
|
@}
|
|
@end example
|
|
|
|
@noindent
|
|
@cindex gas
|
|
PCC includes its own x86 inline assembler with a @code{gas}-like (GNU
|
|
assembler) syntax. No intermediate files are generated. GCC 3.x named
|
|
operands are supported. @code{asm goto} is also supported for
|
|
control-flow transfer to C labels:
|
|
@example
|
|
asm goto ("jmp %l[label]" : : : : label);
|
|
@end example
|
|
|
|
@item The following GCC built-in functions are supported:
|
|
@itemize
|
|
@item @code{__builtin_types_compatible_p(type1, type2)}: returns 1 if @code{type1} is compatible with @code{type2}, 0 otherwise.
|
|
@item @code{__builtin_constant_p(exp)}: returns 1 if @code{exp} evaluates to a compile-time constant, 0 otherwise.
|
|
@item @code{__builtin_choose_expr(const_expr, exp1, exp2)}: evaluates to @code{exp1} if @code{const_expr} is non-zero, otherwise @code{exp2}.
|
|
@item @code{__builtin_expect(expr, expected)}: branch prediction hint (evaluates to @code{expr}).
|
|
@item @code{__builtin_unreachable()}: informs the compiler that the code location cannot be reached.
|
|
@item @code{__builtin_frame_address(level)}: returns the frame pointer address for stack level @code{level}.
|
|
@item @code{__builtin_return_address(level)}: returns the return address for stack level @code{level}.
|
|
@item @code{__builtin_offsetof(type, field)}: returns the byte offset of @code{field} within structure @code{type}.
|
|
@item Bit manipulation built-ins: @code{__builtin_clz},
|
|
@code{__builtin_ctz}, @code{__builtin_ffs}, @code{__builtin_popcount},
|
|
@code{__builtin_parity}, @code{__builtin_clrsb} (and their @code{l} /
|
|
@code{ll} type variants).
|
|
@item Variadic argument handling built-ins: @code{__builtin_va_start}, @code{__builtin_va_arg}, @code{__builtin_va_copy}, @code{__builtin_va_end}.
|
|
@item Memory and string built-ins (mapped to libc):
|
|
@code{__builtin_memcpy}, @code{__builtin_memmove},
|
|
@code{__builtin_memset}, @code{__builtin_memcmp},
|
|
@code{__builtin_strlen}, @code{__builtin_strcpy},
|
|
@code{__builtin_strncpy}, @code{__builtin_strcmp},
|
|
@code{__builtin_strncmp}, @code{__builtin_strcat},
|
|
@code{__builtin_strncat}, @code{__builtin_strchr},
|
|
@code{__builtin_strrchr}, @code{__builtin_strdup},
|
|
@code{__builtin_malloc}, @code{__builtin_realloc},
|
|
@code{__builtin_calloc}, @code{__builtin_memalign},
|
|
@code{__builtin_free}, @code{__builtin_alloca},
|
|
@code{__builtin_abort}.
|
|
@item Atomic built-ins: @code{__atomic_store},
|
|
@code{__atomic_load}, @code{__atomic_exchange},
|
|
@code{__atomic_compare_exchange}, @code{__atomic_fetch_add},
|
|
@code{__atomic_fetch_sub}, @code{__atomic_fetch_or},
|
|
@code{__atomic_fetch_xor}, @code{__atomic_fetch_and},
|
|
@code{__atomic_fetch_nand}, and their reverse
|
|
@code{__atomic_add_fetch} variants.
|
|
@end itemize
|
|
|
|
@item Pragmas supported by PazeCC:
|
|
@itemize
|
|
@item @code{#pragma pack(n)}, @code{#pragma pack()}, @code{#pragma pack(push)}, @code{#pragma pack(push, n)}, @code{#pragma pack(pop)}: set structure alignment or manipulate the packing alignment stack.
|
|
@item @code{#pragma push_macro("MACRO")} and @code{#pragma pop_macro("MACRO")}: push and pop macro definitions to/from a macro stack.
|
|
@item @code{#pragma once}: ensure the header file is included only once during compilation.
|
|
@item @code{#pragma comment(lib, "libname")}: specify a library to link automatically.
|
|
@item @code{#pragma comment(option, "flags")}: pass command line options directly within source code.
|
|
@end itemize
|
|
|
|
@item Preprocessor features:
|
|
@itemize
|
|
@item @code{#include_next <file>}: include the next file of the same name in the header search path order.
|
|
@item @code{#warning "message"}: emit a preprocessor warning message.
|
|
@item GNU empty variadic macro argument pasting: @code{, ##__VA_ARGS__} strips the leading comma when @code{__VA_ARGS__} is empty.
|
|
@item @code{__has_include(<file>)} and @code{__has_include("file")}: test whether a header file exists. Can be used in @code{#if} / @code{#elif} preprocessor conditions.
|
|
@item @code{__has_include_next(<file>)}: like @code{__has_include} but searches in the next include path, for use in wrapper headers.
|
|
@end itemize
|
|
|
|
@end itemize
|
|
|
|
@section PazeCC extensions
|
|
|
|
@itemize
|
|
|
|
@item @code{__TINYC__} is a predefined macro to indicate that you use PCC.
|
|
|
|
@item @code{#!} at the start of a line is ignored to allow scripting.
|
|
|
|
@item Binary digits can be entered (@code{0b101} instead of
|
|
@code{5}).
|
|
|
|
@end itemize
|
|
|
|
@node asm
|
|
@chapter PazeCC Assembler
|
|
|
|
Since version 0.9.16, PazeCC integrates its own assembler. PazeCC
|
|
assembler supports a gas-like syntax (GNU assembler). You can
|
|
deactivate assembler support if you want a smaller PazeCC executable
|
|
(the C compiler does not rely on the assembler).
|
|
|
|
PazeCC Assembler is used to handle files with @file{.S} (C
|
|
preprocessed assembler) and @file{.s} extensions. It is also used to
|
|
handle the GNU inline assembler with the @code{asm} keyword.
|
|
|
|
@section Syntax
|
|
|
|
PazeCC Assembler supports most of the gas syntax. The tokens are the
|
|
same as C.
|
|
|
|
@itemize
|
|
|
|
@item C and C++ comments are supported.
|
|
|
|
@item Identifiers are mostly the same as C. The dot (@code{.}) is
|
|
accepted as an identifier character in assembler code. The dollar
|
|
sign (@code{$}) is accepted with @option{-fdollars-in-identifiers},
|
|
except in inline assembly on targets other than x86-64 and RISC-V 64.
|
|
|
|
@item 64-bit integer numbers are supported.
|
|
|
|
@end itemize
|
|
|
|
@section Expressions
|
|
|
|
@itemize
|
|
|
|
@item Integers in decimal, octal and hexa are supported.
|
|
|
|
@item Unary operators: +, -, ~.
|
|
|
|
@item Binary operators in decreasing priority order:
|
|
|
|
@enumerate
|
|
@item *, /, %, <<, >>
|
|
@item &, |, ^
|
|
@item +, -
|
|
@end enumerate
|
|
|
|
@item A value is either an absolute number or a label plus an offset.
|
|
Only the @code{+} and @code{-} operators can be used with labels (to
|
|
add or subtract an offset). All other operators require absolute
|
|
values. @code{-} supports two labels only if they are the same or if
|
|
they are both defined and in the same section.
|
|
|
|
@end itemize
|
|
|
|
@section Labels
|
|
|
|
@itemize
|
|
|
|
@item All labels are considered as local, except undefined ones.
|
|
|
|
@item Numeric labels can be used as local @code{gas}-like labels.
|
|
They can be defined several times in the same source. Use 'b'
|
|
(backward) or 'f' (forward) as suffix to reference them:
|
|
|
|
@example
|
|
1:
|
|
jmp 1b /* jump to '1' label before */
|
|
jmp 1f /* jump to '1' label after */
|
|
1:
|
|
@end example
|
|
|
|
@end itemize
|
|
|
|
@section Directives
|
|
@cindex assembler directives
|
|
@cindex directives, assembler
|
|
@cindex align directive
|
|
@cindex skip directive
|
|
@cindex space directive
|
|
@cindex byte directive
|
|
@cindex word directive
|
|
@cindex short directive
|
|
@cindex int directive
|
|
@cindex long directive
|
|
@cindex quad directive
|
|
@cindex globl directive
|
|
@cindex global directive
|
|
@cindex section directive
|
|
@cindex text directive
|
|
@cindex data directive
|
|
@cindex bss directive
|
|
@cindex fill directive
|
|
@cindex org directive
|
|
@cindex previous directive
|
|
@cindex string directive
|
|
@cindex asciz directive
|
|
@cindex ascii directive
|
|
@cindex p2align directive
|
|
@cindex balign directive
|
|
@cindex pushsection directive
|
|
@cindex popsection directive
|
|
@cindex set directive
|
|
@cindex weak directive
|
|
@cindex hidden directive
|
|
@cindex type directive
|
|
|
|
All directives are preceded by a '.'. The following directives are
|
|
supported:
|
|
|
|
@itemize
|
|
@item .align n[,value]
|
|
@item .skip n[,value]
|
|
@item .space n[,value]
|
|
@item .byte value1[,...]
|
|
@item .word value1[,...]
|
|
@item .short value1[,...]
|
|
@item .int value1[,...]
|
|
@item .long value1[,...]
|
|
@item .quad immediate_value1[,...]
|
|
@item .globl symbol
|
|
@item .global symbol
|
|
@item .section section
|
|
@item .text
|
|
@item .data
|
|
@item .bss
|
|
@item .fill repeat[,size[,value]]
|
|
@item .org n
|
|
@item .previous
|
|
@item .string string[,...]
|
|
@item .asciz string[,...]
|
|
@item .ascii string[,...]
|
|
@item .p2align n
|
|
@item .balign n[,value]
|
|
@item .pushsection section
|
|
@item .popsection
|
|
@item .set symbol, expr
|
|
@item .weak symbol
|
|
@item .hidden symbol
|
|
@item .type symbol, type
|
|
@end itemize
|
|
|
|
@section X86 Assembler
|
|
@cindex assembler
|
|
|
|
All i386 opcodes are supported. Most common x86_64 opcodes are
|
|
supported, including MMX and a subset of the SSE instruction set; SSE2
|
|
support is limited to a couple of instructions. Only AT&T syntax is
|
|
supported (source then destination operand order). If no size suffix is
|
|
given, PazeCC tries to guess it from the operand sizes. Note that more
|
|
recent extensions such as AVX, AVX2, and AVX-512 are @emph{not}
|
|
supported.
|
|
|
|
x86_64 adds 64-bit operand support (the @code{q} suffix), additional
|
|
registers (@code{%r8}--@code{%r15}), and instructions such as
|
|
@code{cqto}, @code{pushfq}, @code{popfq}, @code{bswapq},
|
|
@code{cmpxchg16b}, @code{movnti}, @code{prefetch*}, @code{lfence},
|
|
@code{mfence}, @code{sfence}, and @code{endbr64} (Control-Flow
|
|
Enforcement).
|
|
|
|
@section ARM, ARM64 and RISC-V Assemblers
|
|
@cindex assembler
|
|
|
|
PCC also includes assemblers for the ARM (32-bit), ARM64 (AArch64) and
|
|
RISC-V 64 targets. These handle the same @file{.S} and @file{.s} file
|
|
extensions and support the same inline assembly mechanism as the x86
|
|
assemblers.
|
|
|
|
@node linker
|
|
@chapter PazeCC Linker
|
|
@cindex linker
|
|
|
|
PCC includes its own linker and can directly output executables,
|
|
shared libraries, and object files without relying on an external
|
|
linker.
|
|
|
|
@section ELF file generation
|
|
@cindex ELF
|
|
|
|
PCC can directly output relocatable ELF files (object files),
|
|
executable ELF files and dynamic ELF libraries without relying on an
|
|
external linker.
|
|
|
|
Dynamic ELF libraries can be output but the C compiler does not generate
|
|
position independent code (PIC). It means that the dynamic library
|
|
code generated by PCC cannot be factorized among processes yet.
|
|
|
|
PCC linker eliminates unreferenced object code in libraries. A single pass is
|
|
done on the object and library list, so the order in which object files and
|
|
libraries are specified is important (same constraint as GNU ld). No grouping
|
|
options (@option{--start-group} and @option{--end-group}) are supported.
|
|
|
|
PCC generates standard ELF sections including @code{.text}, @code{.data},
|
|
@code{.data.ro} (read only data), @code{.bss}, @code{.plt}, @code{.got},
|
|
and @code{.eh_frame}.
|
|
Shared libraries additionally get @code{.gnu.hash}, @code{.gnu.version},
|
|
@code{.dynamic}, and relocation sections (@code{.rela.got},
|
|
@code{.rela.plt}).
|
|
|
|
@cindex section boundary symbols
|
|
@cindex __start_SEC
|
|
@cindex __stop_SEC
|
|
PCC automatically defines @emph{section boundary symbols}
|
|
@code{__start_SEC} and @code{__stop_SEC} for every allocated section
|
|
whose name, without the leading dot, is a valid C identifier. These are
|
|
useful for iterating over section contents from C code:
|
|
|
|
@example
|
|
extern char __start_text[], __stop_text[];
|
|
size_t text_size = __stop_text - __start_text;
|
|
@end example
|
|
|
|
@section ELF file loader
|
|
@cindex ELF loader
|
|
|
|
PCC can load ELF object files, archives (@code{.a} files) and dynamic
|
|
libraries (@code{.so}).
|
|
|
|
@section PE file generation
|
|
@cindex PE
|
|
@cindex PE-i386
|
|
@cindex PE32+
|
|
|
|
PCC for Windows supports the native Win32 executable file format
|
|
(PE-i386). It generates PE32 (i386) and PE32+ (x86-64) executables,
|
|
as well as DLL files. PCC also supports PE with ARM64 machine type,
|
|
including @code{.pdata} unwind information for structured exception
|
|
handling.
|
|
|
|
PE executables can target several subsystems (see @ref{Invoke,, the
|
|
@option{-Wl,-subsystem} option}); which ones are available depends on
|
|
the target. Standard PE sections are emitted (@code{.text},
|
|
@code{.data}, @code{.rdata}, @code{.bss}, @code{.rsrc}, @code{.pdata},
|
|
@code{.idata}, @code{.reloc}); the export directory goes to
|
|
@code{.rdata}.
|
|
|
|
@section Mach-O file generation
|
|
@cindex Mach-O
|
|
@cindex macOS
|
|
|
|
PCC on macOS produces Mach-O executables and dynamic libraries
|
|
(@code{.dylib}) for x86-64 and ARM64 (AArch64). The files PCC writes
|
|
always contain a single architecture.
|
|
|
|
PCC can load Mach-O dynamic libraries and text-based stub files
|
|
(@code{.tbd}) for linking against system frameworks. Fat (universal)
|
|
libraries are accepted, the slice matching the target being selected.
|
|
|
|
Mach-O output uses dyld chained fixups on both x86-64 and ARM64.
|
|
|
|
@section Binary and COFF output
|
|
@cindex binary output
|
|
@cindex COFF
|
|
|
|
In addition to the platform-native container formats, PCC supports:
|
|
|
|
@table @code
|
|
@item Binary image
|
|
Raw binary output with no container headers. Selected via
|
|
@code{-Wl,--oformat=binary} (@pxref{Invoke}). Useful for bootloaders,
|
|
kernels, and embedded targets. Only valid for executable output.
|
|
|
|
@item COFF
|
|
The TMS320C67xx (C67) target outputs COFF executables. Object files are
|
|
always written as ELF.
|
|
@end table
|
|
|
|
@section GNU Linker Scripts
|
|
@cindex scripts, linker
|
|
@cindex linker scripts
|
|
@cindex GROUP, linker command
|
|
@cindex FILE, linker command
|
|
@cindex OUTPUT_FORMAT, linker command
|
|
@cindex TARGET, linker command
|
|
|
|
Because on many Linux systems some dynamic libraries (such as
|
|
@file{/usr/lib/libc.so}) are in fact GNU ld link scripts (horrible!),
|
|
the PCC linker also supports a subset of GNU ld scripts.
|
|
|
|
The @code{GROUP} and @code{FILE} commands are supported. @code{OUTPUT_FORMAT}
|
|
and @code{TARGET} are ignored.
|
|
|
|
Example from @file{/usr/lib/libc.so}:
|
|
@example
|
|
/* GNU ld script
|
|
Use the shared library, but some functions are only in
|
|
the static library, so try that secondarily. */
|
|
GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
|
|
@end example
|
|
|
|
@node Bounds
|
|
@chapter PazeCC Memory and Bound checks
|
|
@cindex bound checks
|
|
@cindex memory checks
|
|
|
|
This feature is activated with the @option{-b} option (@pxref{Invoke}).
|
|
Here are some examples of caught errors:
|
|
|
|
@table @asis
|
|
|
|
@item Invalid range with standard string function:
|
|
@example
|
|
@{
|
|
char tab[10];
|
|
memset(tab, 0, 11);
|
|
@}
|
|
@end example
|
|
|
|
@item Out of bounds-error in global or local arrays:
|
|
@example
|
|
@{
|
|
int tab[10];
|
|
for(i=0;i<11;i++) @{
|
|
sum += tab[i];
|
|
@}
|
|
@}
|
|
@end example
|
|
|
|
@item Out of bounds-error in malloc'ed data:
|
|
@example
|
|
@{
|
|
int *tab;
|
|
tab = malloc(20 * sizeof(int));
|
|
for(i=0;i<21;i++) @{
|
|
sum += tab[i];
|
|
@}
|
|
free(tab);
|
|
@}
|
|
@end example
|
|
|
|
@item Out of bounds-error in alloca'ed data:
|
|
@example
|
|
@{
|
|
char *p = alloca(10);
|
|
memset(p, 'a', 11);
|
|
@}
|
|
@end example
|
|
|
|
@item Out of bounds-error in variable length arrays:
|
|
@example
|
|
@{
|
|
int tab[n];
|
|
for(i=0;i<n+1;i++) @{
|
|
sum += tab[i];
|
|
@}
|
|
@}
|
|
@end example
|
|
|
|
@item Access of freed memory:
|
|
@example
|
|
@{
|
|
int *tab;
|
|
tab = malloc(20 * sizeof(int));
|
|
free(tab);
|
|
for(i=0;i<20;i++) @{
|
|
sum += tab[i];
|
|
@}
|
|
@}
|
|
@end example
|
|
|
|
@item Double free:
|
|
@example
|
|
@{
|
|
int *tab;
|
|
tab = malloc(20 * sizeof(int));
|
|
free(tab);
|
|
free(tab);
|
|
@}
|
|
@end example
|
|
|
|
@item Overlapping regions in memory and string functions:
|
|
@example
|
|
@{
|
|
char tab[10];
|
|
memcpy(tab, tab + 1, 5);
|
|
@}
|
|
@end example
|
|
@end table
|
|
|
|
The checks are not limited to the code generated by PCC: the bound
|
|
checking runtime also replaces the memory allocation functions
|
|
(@code{malloc}, @code{calloc}, @code{realloc}, @code{memalign},
|
|
@code{free}) and the most common memory and string functions
|
|
(@code{memcpy}, @code{memmove}, @code{memset}, @code{memcmp},
|
|
@code{strlen}, @code{strcpy}, @code{strncpy}, @code{strcmp},
|
|
@code{strncmp}, @code{strcat}, @code{strncat}, @code{strchr},
|
|
@code{strrchr}, @code{strdup}) by versions checking their arguments.
|
|
Regions created by @code{alloca}, by variable length arrays and (except
|
|
on Windows) by @code{mmap} are tracked as well. @code{setjmp} and
|
|
@code{longjmp} (and @code{sigsetjmp} and @code{siglongjmp}) are handled
|
|
so that the regions of the abandoned stack frames are released.
|
|
|
|
PCC defines @code{__TCC_BCHECK__} if activated.
|
|
|
|
There are five environment variables that can be used to control the behavior:
|
|
@itemize
|
|
@item PCC_BOUNDS_WARN_POINTER_ADD
|
|
- Print warning when pointer add creates an illegal pointer.
|
|
@item PCC_BOUNDS_PRINT_CALLS
|
|
- Print bound checking calls. Can be used for debugging.
|
|
@item PCC_BOUNDS_PRINT_HEAP
|
|
- Print heap objects that are not freed at exit of program.
|
|
@item PCC_BOUNDS_PRINT_STATISTIC
|
|
- Print statistic information at exit of program.
|
|
@item PCC_BOUNDS_NEVER_FATAL
|
|
- Try to continue in case of a bound checking error.
|
|
@end itemize
|
|
|
|
Also, a function @code{__bounds_checking(x)} can be used to turn off/on bounds
|
|
checking from usercode (see below).
|
|
|
|
Notes:
|
|
@itemize
|
|
@item Available for all targets and platforms supported by PCC, except
|
|
the TMS320C67xx target. The checking code is only built for the native
|
|
compiler, and can be left out with @option{--config-bcheck=no} at
|
|
configure time.
|
|
@item The generated code is slower and bigger.
|
|
@item The bound checking code is not included in shared libraries. The main
|
|
executable should always be compiled with the @option{-b}.
|
|
@item Pointer size is @emph{unchanged} and code generated with bound checks is
|
|
@emph{fully compatible} with unchecked code. When a pointer comes from
|
|
unchecked code, it is assumed to be valid. Even very obscure C code with
|
|
casts should work correctly.
|
|
@item Signal handlers are not compatible with bounds checking. The
|
|
bounds checking code disables checking in signal/sigaction handlers.
|
|
The fork() function call in a multi threaded application is also a problem.
|
|
The bound checking code fixes this for the child process.
|
|
@item The reason that signals and fork have problems is that we use locking
|
|
inside the bounds checking code.
|
|
Inside a signal handler we can not use locks. Also in a multi threaded
|
|
application after a fork the child process can have the lock set
|
|
by another thread.
|
|
@item The BOUNDS_CHECKING_OFF and BOUNDS_CHECKING_ON can also be used to
|
|
disable bounds checking for some code.
|
|
@item The __bounds_checking call adds a value to a thread local value.
|
|
The value starts at 0. If the value is not 0 the code is not checked
|
|
for bounds checking errors.
|
|
@end itemize
|
|
|
|
@example
|
|
#ifdef __TCC_BCHECK__
|
|
extern void __bounds_checking (int x);
|
|
# define BOUNDS_CHECKING_OFF __bounds_checking(1)
|
|
# define BOUNDS_CHECKING_ON __bounds_checking(-1)
|
|
#else
|
|
# define BOUNDS_CHECKING_OFF
|
|
# define BOUNDS_CHECKING_ON
|
|
#endif
|
|
@end example
|
|
|
|
For more information about the ideas behind this method, see
|
|
@url{http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html}.
|
|
|
|
@node Libtcc
|
|
@chapter The @code{libpcc} library
|
|
|
|
The @code{libpcc} library enables you to use PCC as a backend for
|
|
dynamic code generation.
|
|
|
|
Read the @file{libpcc.h} to have an overview of the API. Read
|
|
@file{tests/libpcc_test.c} to have a very simple example.
|
|
|
|
The idea consists in giving a C string containing the program you want
|
|
to compile directly to @code{libpcc}. Then you can access to any global
|
|
symbol (function or variable) defined.
|
|
|
|
A typical session creates a compilation state with @code{pcc_new()},
|
|
selects the output type with @code{pcc_set_output_type()} (this must be
|
|
done before any compilation), passes command line options with
|
|
@code{pcc_set_options()} and adds input with @code{pcc_compile_string()}
|
|
or @code{pcc_add_file()}. The result is then either written to a file
|
|
with @code{pcc_output_file()}, run with @code{pcc_run()}, or relocated in
|
|
memory with @code{pcc_relocate()} so that the compiled functions and
|
|
variables can be retrieved with @code{pcc_get_symbol()}.
|
|
@code{pcc_output_file()} and @code{pcc_run()} do the relocation
|
|
themselves, so @code{pcc_relocate()} must not be called before them.
|
|
Symbols of the host program are made visible to the compiled code with
|
|
@code{pcc_add_symbol()}. The state and everything it allocated is
|
|
released by @code{pcc_delete()}.
|
|
|
|
Errors and warnings go to @code{stderr} unless a callback is installed
|
|
with @code{pcc_set_error_func()}. Several states may be used at the same
|
|
time from different threads; see @file{tests/libpcc_test_mt.c}, which
|
|
also shows how @code{pcc_setjmp()} catches the runtime exceptions of code
|
|
compiled with @option{-b} or @option{-bt}.
|
|
|
|
@node devel
|
|
@chapter Developer's guide
|
|
|
|
This chapter gives some hints to understand how PCC works. You can skip
|
|
it if you do not intend to modify the PCC code.
|
|
|
|
@section File reading
|
|
|
|
The @code{BufferedFile} structure contains the context needed to read a
|
|
file, including the current line number. @code{pcc_open()} opens a new
|
|
file and @code{pcc_close()} closes it. @code{pcc_open_bf()} builds the
|
|
same context for a memory buffer instead of a file; it is used for the
|
|
@option{-D} options of the command line and for token pasting.
|
|
|
|
The contexts are stacked through the @code{BufferedFile.prev} field, so
|
|
the current file is always the innermost @code{#include}.
|
|
@code{next_c()} returns the next character and calls @code{handle_eob()}
|
|
to refill the buffer when it is exhausted.
|
|
|
|
@section Lexer
|
|
|
|
@code{next()} reads the next token in the current
|
|
file. @code{next_nomacro()} reads the next token without macro
|
|
expansion.
|
|
|
|
@code{tok} contains the current token (see @code{TOK_xxx}
|
|
constants). Identifiers and keywords are also tokens: they are entered
|
|
in the @code{table_ident} array of @code{TokenSym} structures, so a
|
|
string is never needed to designate them. @code{tokc} contains
|
|
additional infos about the token (for example a constant value if number
|
|
or string token).
|
|
|
|
@section Parser
|
|
|
|
The parser is hardcoded (yacc is not necessary). It does only one pass,
|
|
except:
|
|
|
|
@itemize
|
|
|
|
@item For initialized arrays with unknown size, a first pass
|
|
is done to count the number of elements.
|
|
|
|
@item For architectures where arguments are evaluated in
|
|
reverse order, a first pass is done to reverse the argument order.
|
|
|
|
@item For inline functions, the body is recorded as a token string and
|
|
parsed again where the function is really needed.
|
|
|
|
@end itemize
|
|
|
|
@section Types
|
|
|
|
The types are stored in a @code{CType} structure. @code{CType.t} is a
|
|
single 'int' holding the basic type, the type modifiers and, during
|
|
parsing, the storage class. @code{CType.ref} points to the @code{Sym}
|
|
describing the referenced type: the pointed type for pointers, the
|
|
element type for arrays, the return type and the parameters for
|
|
functions, and the fields for structures and unions.
|
|
|
|
@example
|
|
#define VT_BTYPE 0x000f /* mask for basic type */
|
|
#define VT_VOID 0 /* void type */
|
|
#define VT_BYTE 1 /* signed byte type */
|
|
#define VT_SHORT 2 /* short type */
|
|
#define VT_INT 3 /* integer type */
|
|
#define VT_LLONG 4 /* 64 bit integer */
|
|
#define VT_PTR 5 /* pointer */
|
|
#define VT_FUNC 6 /* function type */
|
|
#define VT_STRUCT 7 /* struct/union definition */
|
|
#define VT_FLOAT 8 /* IEEE float */
|
|
#define VT_DOUBLE 9 /* IEEE double */
|
|
#define VT_LDOUBLE 10 /* IEEE long double */
|
|
#define VT_BOOL 11 /* ISOC99 boolean type */
|
|
#define VT_QLONG 13 /* 128-bit integer, only for the x86-64 ABI */
|
|
#define VT_QFLOAT 14 /* 128-bit float, only for the x86-64 ABI */
|
|
|
|
#define VT_UNSIGNED 0x0010 /* unsigned type */
|
|
#define VT_DEFSIGN 0x0020 /* explicitly signed or unsigned */
|
|
#define VT_ARRAY 0x0040 /* array type (also has VT_PTR) */
|
|
#define VT_BITFIELD 0x0080 /* bitfield modifier */
|
|
#define VT_CONSTANT 0x0100 /* const modifier */
|
|
#define VT_VOLATILE 0x0200 /* volatile modifier */
|
|
#define VT_VLA 0x0400 /* VLA type (also has VT_PTR and VT_ARRAY) */
|
|
#define VT_LONG 0x0800 /* long type (also has VT_INT rsp. VT_LLONG) */
|
|
|
|
#define VT_STRUCT_SHIFT 20 /* shift for bitfield shift values */
|
|
@end example
|
|
|
|
The @code{VT_UNSIGNED} flag can be set for chars, shorts, ints and long
|
|
longs. @code{VT_DEFSIGN} tells whether the signedness was written
|
|
explicitly; @code{compare_types()} only looks at it for @code{char}, the
|
|
one type whose default signedness differs from an explicit one.
|
|
|
|
Arrays are considered as pointers @code{VT_PTR} with the flag
|
|
@code{VT_ARRAY} set. Variable length arrays have @code{VT_VLA} set in
|
|
addition to @code{VT_PTR} and @code{VT_ARRAY}.
|
|
|
|
@code{VT_LONG} is a modifier, not a basic type: @code{long} is
|
|
@code{VT_LONG | VT_INT} or @code{VT_LONG | VT_LLONG} depending on the
|
|
target. It does not change the generated code, but it is part of the type
|
|
so that @code{long} stays distinct from @code{int} rsp. @code{long long}
|
|
for type compatibility, for @code{_Generic} and in diagnostics.
|
|
|
|
If @code{VT_BITFIELD} is set, then the bitfield position is stored in the
|
|
6 bits starting at @code{VT_STRUCT_SHIFT} and the bitfield size in the 6
|
|
bits above it (macros @code{BIT_POS()} and @code{BIT_SIZE()}).
|
|
|
|
The same high order bits also distinguish types which share a basic
|
|
type: @code{VT_UNION} is a @code{VT_STRUCT} with 1 there, @code{VT_ENUM}
|
|
and @code{VT_ENUM_VAL} mark an integral type which is really an enum or
|
|
an enum constant, and @code{VT_ASM} marks a symbol which was created by
|
|
the assembler.
|
|
|
|
During parsing, the storage of an object is also stored in the type
|
|
integer:
|
|
|
|
@example
|
|
#define VT_EXTERN 0x00001000 /* extern definition */
|
|
#define VT_STATIC 0x00002000 /* static variable */
|
|
#define VT_TYPEDEF 0x00004000 /* typedef definition */
|
|
#define VT_INLINE 0x00008000 /* inline definition */
|
|
#define VT_TLS 0x00010000 /* thread-local storage */
|
|
@end example
|
|
|
|
The declaration properties which do not fit in the type integer
|
|
(alignment, packing, visibility, weak, dllimport/dllexport, calling
|
|
convention, @dots{}) are kept in the @code{SymAttr} and @code{FuncAttr}
|
|
bitfields of the @code{Sym} structure.
|
|
|
|
@section Symbols
|
|
|
|
All symbols are stored in symbol stacks, chained through the
|
|
@code{Sym.prev} field. Each symbol stack contains @code{Sym} structures.
|
|
|
|
@code{Sym.v} contains the symbol name (remember
|
|
an identifier is also a token, so a string is never necessary to store
|
|
it). @code{Sym.type} gives the type of the symbol. @code{Sym.r} is usually
|
|
the register in which the corresponding variable is stored. @code{Sym.c} is
|
|
usually a constant associated to the symbol like its address for normal
|
|
symbols, and the number of entries for symbols representing arrays.
|
|
Variable length array types use @code{Sym.c} as a location on the stack
|
|
which holds the runtime sizeof for the type.
|
|
|
|
Five main symbol stacks are defined:
|
|
|
|
@table @code
|
|
|
|
@item define_stack
|
|
for the macros (@code{#define}s).
|
|
|
|
@item global_stack
|
|
for the global variables, functions and types.
|
|
|
|
@item local_stack
|
|
for the local variables, functions and types.
|
|
|
|
@item global_label_stack
|
|
for the function local labels (for @code{goto}).
|
|
|
|
@item local_label_stack
|
|
for GCC block local labels (see the @code{__label__} keyword).
|
|
|
|
@end table
|
|
|
|
@code{sym_push()} is used to add a new symbol in the local symbol
|
|
stack. If no local symbol stack is active, it is added in the global
|
|
symbol stack.
|
|
|
|
@code{sym_pop(st,b,keep)} pops symbols from the symbol stack @var{st}
|
|
until the symbol @var{b} is on the top of stack. If @var{b} is NULL, the
|
|
stack is emptied. The symbols are always made invisible to the parser,
|
|
but if @var{keep} is non zero they are not freed, which is needed as long
|
|
as the recorded body of an inline function may still refer to them.
|
|
|
|
@code{sym_find(v)} returns the symbol associated to the identifier
|
|
@var{v}. No stack is searched: each @code{TokenSym} of
|
|
@code{table_ident} directly holds the innermost visible @code{Sym} for
|
|
its identifier, and @code{sym_pop()} restores the previous one. Tags of
|
|
structures, unions and enums live in their own namespace and are looked
|
|
up with @code{struct_find()}, labels with @code{label_find()}.
|
|
|
|
@section Sections
|
|
|
|
The generated code and data are written in sections. The structure
|
|
@code{Section} contains all the necessary information for a given
|
|
section. @code{new_section()} creates a new section. ELF file semantics
|
|
is assumed for each section.
|
|
|
|
The following sections are predefined:
|
|
|
|
@table @code
|
|
|
|
@item text_section
|
|
is the section containing the generated code. @var{ind} contains the
|
|
current position in the code section. @var{cur_text_section} is the
|
|
section the code of the current function goes to, which is another
|
|
section when the function has a @code{section} attribute.
|
|
|
|
@item data_section
|
|
contains initialized data
|
|
|
|
@item rodata_section
|
|
contains read only data: the literal strings and the objects declared
|
|
@code{const}
|
|
|
|
@item bss_section
|
|
contains uninitialized data
|
|
|
|
@item common_section
|
|
receives the tentative definitions when @option{-fcommon} is given. Its
|
|
symbols are @code{SHN_COMMON}, with the alignment in the symbol value,
|
|
and @code{resolve_common_syms()} allocates them in @code{bss_section} at
|
|
link time. By default PCC does not use common symbols and puts tentative
|
|
definitions directly in @code{bss_section}
|
|
|
|
@item bounds_section
|
|
@itemx lbounds_section
|
|
are used when bound checking is activated
|
|
|
|
@item stab_section
|
|
@itemx stabstr_section
|
|
are used when debugging is active and stabs debug information is selected
|
|
|
|
@item dwarf_info_section
|
|
@itemx dwarf_abbrev_section
|
|
@itemx dwarf_line_section
|
|
@itemx dwarf_aranges_section
|
|
@itemx dwarf_str_section
|
|
@itemx dwarf_line_str_section
|
|
are used when debugging is active and dwarf debug information is
|
|
selected, which is the default on macOS and Android and can be enabled
|
|
elsewhere with the @option{--config-dwarf} option of @command{configure}
|
|
|
|
@item tcov_section
|
|
contains the counters used by @option{-ftest-coverage}
|
|
|
|
@item symtab_section
|
|
contains the symbols and their names (in @code{symtab_section->link}).
|
|
It is needed for linking, not only for debugging.
|
|
|
|
@end table
|
|
|
|
@section Code generation
|
|
@cindex code generation
|
|
|
|
@subsection Introduction
|
|
|
|
The PCC code generator directly generates linked binary code in one
|
|
pass. It is rather unusual these days (see gcc for example which
|
|
generates text assembly), but it can be very fast and surprisingly
|
|
little complicated.
|
|
|
|
The PCC code generator is register based. Optimization is only done at
|
|
the expression level. No intermediate representation of expression is
|
|
kept except the current values stored in the @emph{value stack}.
|
|
|
|
Each target declares the registers the code generator may use in
|
|
@code{reg_classes[]}, and @code{NB_REGS} gives their number: five on
|
|
i386 (@code{eax}, @code{ecx}, @code{edx}, @code{ebx}, which is only
|
|
usable when @code{USE_EBX} is set, and the top of the x87 stack), 25 on
|
|
x86-64, 28 on arm64. When more registers are needed, one register is
|
|
spilled into a new temporary variable on the stack.
|
|
|
|
@subsection The value stack
|
|
@cindex value stack, introduction
|
|
|
|
When an expression is parsed, its value is pushed on the value stack
|
|
(@var{vstack}). The top of the value stack is @var{vtop}. Each value
|
|
stack entry is the structure @code{SValue}.
|
|
|
|
@code{SValue.type} is the type. @code{SValue.r} indicates how the value is
|
|
currently stored in the generated code. It is usually a CPU register
|
|
index (@code{REG_xxx} constants), but additional values and flags are
|
|
defined:
|
|
|
|
@example
|
|
#define VT_VALMASK 0x003f /* mask for value location, register or: */
|
|
#define VT_CONST 0x0030 /* constant in vc */
|
|
#define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
|
|
#define VT_LOCAL 0x0032 /* offset on stack */
|
|
#define VT_CMP 0x0033 /* the value is stored in processor flags */
|
|
#define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
|
|
#define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
|
|
#define VT_LVAL 0x0100 /* var is an lvalue */
|
|
#define VT_SYM 0x0200 /* a symbol value is added */
|
|
#define VT_MUSTCAST 0x0C00 /* value must be casted to be correct */
|
|
#define VT_NONCONST 0x1000 /* VT_CONST, but not an (C standard) integer
|
|
constant expression */
|
|
#define VT_MUSTBOUND 0x4000 /* bound checking must be done before
|
|
dereferencing value */
|
|
#define VT_BOUNDED 0x8000 /* value is bounded */
|
|
@end example
|
|
|
|
@code{SValue.r2} holds the second register when a value needs two of
|
|
them, as for a @code{long long} on a 32 bit target. It is set to
|
|
@code{VT_CONST} when it is not used.
|
|
|
|
@table @code
|
|
|
|
@item VT_CONST
|
|
indicates that the value is a constant. It is stored in the union
|
|
@code{SValue.c}, depending on its type.
|
|
|
|
@item VT_LOCAL
|
|
indicates a local variable pointer at offset @code{SValue.c.i} in the
|
|
stack.
|
|
|
|
@item VT_CMP
|
|
indicates that the value is actually stored in the CPU flags (i.e. the
|
|
value is the consequence of a test). The value is either 0 or 1. The
|
|
comparison which set the flags is kept in @code{SValue.cmp_op};
|
|
@code{SValue.cmp_r} is left to the code generator, which uses it to
|
|
remember where the compared values are on the targets that have no flags
|
|
register.
|
|
|
|
If any code is generated which destroys the CPU flags, this value MUST be
|
|
put in a normal register. @code{vcheck_cmp()} does it, and the functions
|
|
which manipulate the value stack call it before generating code.
|
|
|
|
@item VT_JMP
|
|
@itemx VT_JMPI
|
|
indicates that the value is the consequence of a conditional jump. For VT_JMP,
|
|
it is 1 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted.
|
|
|
|
These values are used to compile the @code{||} and @code{&&} logical
|
|
operators. The jumps which still have to be resolved are chained in
|
|
@code{SValue.jtrue} and @code{SValue.jfalse}; @code{gvtst()} adds to
|
|
these lists and generates the final test.
|
|
|
|
If any code is generated, this value MUST be put in a normal
|
|
register. Otherwise, the generated code won't be executed if the jump is
|
|
taken.
|
|
|
|
@item VT_LVAL
|
|
is a flag indicating that the value is actually an lvalue (left value of
|
|
an assignment). It means that the value stored is actually a pointer to
|
|
the wanted value.
|
|
|
|
Understanding the use @code{VT_LVAL} is very important if you want to
|
|
understand how PCC works.
|
|
|
|
@item VT_LLOCAL
|
|
is a saved lvalue on the stack. @code{VT_LVAL} must also be set with
|
|
@code{VT_LLOCAL}. @code{VT_LLOCAL} can arise when a @code{VT_LVAL} in
|
|
a register has to be saved to the stack, or it can come from an
|
|
architecture-specific calling convention.
|
|
|
|
@item VT_MUSTCAST
|
|
indicates that a cast to the value type must be performed if the value
|
|
is used (lazy casting), as for a @code{char} or a @code{short} kept in an
|
|
integer register. It is a two bit field, so that the width the value has
|
|
to be casted from (@code{int} or @code{long long}) is remembered too.
|
|
|
|
@item VT_SYM
|
|
indicates that the symbol @code{SValue.sym} must be added to the constant.
|
|
|
|
@item VT_NONCONST
|
|
marks a @code{VT_CONST} value which only became constant through
|
|
optimization. Such a value is folded like any other constant, but it is
|
|
not a constant expression in the sense of the C standard: it is rejected
|
|
where the language requires one (@code{expr_const64()}) and it is not a
|
|
null pointer constant.
|
|
|
|
@item VT_MUSTBOUND
|
|
@itemx VT_BOUNDED
|
|
are only used for optional bound checking.
|
|
|
|
@end table
|
|
|
|
@subsection Manipulating the value stack
|
|
@cindex value stack
|
|
|
|
@code{vsetc()} and @code{vset()} pushes a new value on the value
|
|
stack. If the previous @var{vtop} was stored in a very unsafe place(for
|
|
example in the CPU flags), then some code is generated to put the
|
|
previous @var{vtop} in a safe storage.
|
|
|
|
@code{vpop()} pops @var{vtop}. In some cases, it also generates cleanup
|
|
code (for example if stacked floating point registers are used as on
|
|
x86).
|
|
|
|
@code{vdup()}, @code{vswap()}, @code{vrotb()} and @code{vrott()}
|
|
duplicate, exchange and rotate the topmost entries. They all go through
|
|
@code{vcheck_cmp()} first, because a @code{VT_CMP} value may only stay on
|
|
top of the stack.
|
|
|
|
The @code{gv(rc)} function generates code to evaluate @var{vtop} (the
|
|
top value of the stack) into registers. @var{rc} selects in which
|
|
register class the value should be put. @code{gv()} is the @emph{most
|
|
important function} of the code generator.
|
|
|
|
@code{gv2()} is the same as @code{gv()} but for the top two stack
|
|
entries.
|
|
|
|
@code{get_reg(rc)} returns a free register of the class @var{rc}. When
|
|
they are all busy it spills the one held by the oldest value stack entry,
|
|
starting from the bottom of the stack so that the registers of the
|
|
operation being generated are never taken away. The spill itself is done
|
|
by @code{save_reg()}, which copies to the stack every value living in a
|
|
given register.
|
|
|
|
@subsection CPU dependent code generation
|
|
@cindex CPU dependent
|
|
See the @file{i386-gen.c} file to have an example.
|
|
|
|
@table @code
|
|
|
|
@item load()
|
|
must generate the code needed to load a stack value into a register.
|
|
|
|
@item store()
|
|
must generate the code needed to store a register into a stack value
|
|
lvalue.
|
|
|
|
@item gfunc_call(nb_args)
|
|
should generate a function call, the arguments and the function address
|
|
being the @code{nb_args + 1} topmost entries of the value stack.
|
|
@code{gfunc_sret()} tells the parser how a structure return value is
|
|
passed for the target ABI.
|
|
|
|
@item gfunc_prolog()
|
|
@itemx gfunc_epilog()
|
|
should generate a function prolog/epilog.
|
|
|
|
@item gen_opi(op)
|
|
must generate the binary integer operation @var{op} on the two top
|
|
entries of the stack which are guaranteed to contain integer types.
|
|
|
|
The result value should be put on the stack.
|
|
|
|
@item gen_opf(op)
|
|
same as @code{gen_opi()} for floating point operations. The two top
|
|
entries of the stack are guaranteed to contain floating point values of
|
|
same types.
|
|
|
|
@item gen_cvt_itof()
|
|
integer to floating point conversion.
|
|
|
|
@item gen_cvt_ftoi()
|
|
floating point to integer conversion.
|
|
|
|
@item gen_cvt_ftof()
|
|
floating point to floating point of different size conversion.
|
|
|
|
@item gen_cvt_csti()
|
|
performs the delayed cast of a @code{char} or a @code{short} held in an
|
|
integer register (see @code{VT_MUSTCAST}).
|
|
|
|
@item gjmp()
|
|
@itemx gjmp_addr()
|
|
@itemx gjmp_cond()
|
|
@itemx gsym_addr()
|
|
generate unconditional and conditional jumps, and patch the jumps whose
|
|
target was not known yet.
|
|
|
|
@item gen_vla_alloc()
|
|
@itemx gen_vla_sp_save()
|
|
@itemx gen_vla_sp_restore()
|
|
allocate a variable length array on the stack, and save and restore the
|
|
stack pointer around it.
|
|
|
|
@end table
|
|
|
|
@section Optimizations done
|
|
@cindex optimizations
|
|
@cindex constant propagation
|
|
@cindex strength reduction
|
|
@cindex comparison operators
|
|
@cindex caching processor flags
|
|
@cindex flags, caching
|
|
@cindex jump optimization
|
|
Constant propagation is done for all operations. Multiplications and
|
|
divisions by a power of two are optimized to shifts, and operations
|
|
which are a no-operation for the given constant (@code{x*1},
|
|
@code{x-0}, @code{x&-1}, @dots{}) are dropped. Constants added to a
|
|
symbol or to a local variable address are folded into the address.
|
|
Comparison operators are optimized by maintaining a special cache for
|
|
the processor flags. &&, || and ! are optimized by maintaining a special
|
|
'jump target' value. No other jump optimization is currently performed
|
|
because it would require to store the code in a more abstract fashion.
|
|
|
|
@unnumbered Concept Index
|
|
@printindex cp
|
|
|
|
@bye
|
|
|
|
@c Local variables:
|
|
@c fill-column: 78
|
|
@c texinfo-column-for-description: 32
|
|
@c End:
|