Skip to content

Commit

Permalink
Merge pull request #65 from stormalf/1.0.22
Browse files Browse the repository at this point in the history
1.0.22
  • Loading branch information
stormalf authored Jul 13, 2024
2 parents 18cb4c8 + c34eaba commit c2063a3
Show file tree
Hide file tree
Showing 83 changed files with 12,669 additions and 517 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
/chibicc
/test/*.exe
/stage2
/build
/cmake-build-debug
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.22.1)
install(PROGRAMS ${Boost_LIBRARIES} DESTINATION ~/install/lib)

set(CMAKE_CXX_STANDARD 20)

project(chibicc)

FIND_PACKAGE(Boost 1.48 COMPONENTS REQUIRED)
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )

file(GLOB SOURCE
"*.h"
"*.c"
"include/*.h")

add_executable(chibicc ./main.c ${SOURCE} ${Boost_LIBRARIES})
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC=gcc
CFLAGS =-std=c11 -g -fno-common -Wall -Wno-switch
CFLAGS_DIAG=-dotfile
CFLAGS_DIAG=-dotfile
OBJECT=chibicc
OBJECTLIB=libchibicc
SRCS=$(wildcard *.c)
Expand Down Expand Up @@ -53,7 +53,7 @@ test-stage2: $(TESTS:test/%=stage2/test/%)
for i in $^; do echo $$i; ./$$i || exit 1; echo; done
test/driver.sh ./stage2/$(OBJECT)

projects-all: projects openssl nmap curl
projects-all: projects openssl nmap curl vim

projects: zlib util-linux nginx

Expand All @@ -76,6 +76,9 @@ util-linux:
nginx:
cd ../nginx && make clean && CC=chibicc CFLAGS=-fPIC ./auto/configure && make

vim:
cd ../vim && make clean && CC=chibicc CFLAGS=-fPIC ./configure && make && make test

# Misc.

libchibicc: $(OBJECT) $(OBJECTLIB).so
Expand Down
130 changes: 124 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ or
used to automate file dependency management
-fpic or -fPIC Generate position-independent code (PIC)
-fno-pic disables the generation of position-independent code with relative address references
-pie Create a dynamically linked position independent
-fpie Create a dynamically linked position independent
-fPIE Create a dynamically linked position independent
-fcommon is the default if not specified, it's mainly useful to enable legacy code to link without errors
-fno-common specifies that the compiler places uninitialized global variables in the BSS section of the object file.
-static pass to the linker to link a program statically
Expand All @@ -67,6 +70,18 @@ or
-dM Print macro definitions in -E mode instead of normal output
chibicc [ -o <path> ] <file>

## compile

To compile chibicc with make :

make

To compile chibicc with cmake you need to install boost library. Then you can compile it with cmake :

cmake -S . -B ./build
cd build
make

## Examples

Note that probably you need to define the include parameter and lib parameter to be able to execute these examples outside the chibicc directory. If you forgot you will have similar error message :
Expand Down Expand Up @@ -214,10 +229,34 @@ List of options ignored :
"defs"
"-flto"
"-flto=8"
"-fvisibility=default"
"-pedantic"
"-nostdinc"
"-mno-red-zone"
"-fvisibility=default"
"-Werror=invalid-command-line-argument"
"-Werror=unknown-warning-option"
"-Wsign-compare"
"-Wundef"
"-Wpointer-arith"
"-Wvolatile-register-var"
"-Wformat"
"-Wformat-security"
"-Wduplicated-branches"
"-Wduplicated-cond"
"-Wbad-function-cast"
"-Wwrite-strings"
"-Wlogical-op"
"-Wshadow=local"
"-Wmultistatement-macros"
"-fstack-protector"
"-fstack-protector-strong"
"-fstack-clash-protection"
"-fdiagnostics-show-option"
"-fasynchronous-unwind-tables"
"-fexceptions"
"-fsanitize=cfi"
"--print-search-dirs"
"-fdiagnostics-show-option"
"-w"


Expand Down Expand Up @@ -350,11 +389,67 @@ vim: https://github.com/vim/vim.git
FAILED: 0: []
skipped: 0

lxc: https://github.com/lxc/lxc.git

CC=chibicc CFLAGS=-fpic LDFLAGS=-fpic meson build
libwepb: https://github.com/webmproject/libwebp.git

CC=chibicc CFLAGS="-fpic" LDFLAGS="-fpic -lpng -ljpeg" cmake -S . -B ./build
cd build
meson compile
cmake --build .
~/libwebp/build$ ./img2webp -version
WebP Encoder version: 1.3.2
WebP Mux version: 1.3.2
libsharpyuv: 0.4.0
[no output file specified] [0 frames, 0 bytes].


memcached: https://github.com/memcached/memcached.git

autoreconf -fi
CC=chibicc CFLAGS=-fpic LDFLAGS=-fpic ./configure
make
make test



cpython: git clone [email protected]:python/cpython.git

CC=chibicc ./configure
make && make test
== Tests result: SUCCESS ==

10 slowest tests:
- test_imaplib: 52.3 sec
- test_signal: 49.1 sec
- test.test_concurrent_futures.test_wait: 48.4 sec
- test.test_multiprocessing_forkserver.test_processes: 41.1 sec
- test.test_multiprocessing_spawn.test_processes: 40.1 sec
- test.test_multiprocessing_spawn.test_misc: 36.5 sec
- test_io: 33.8 sec
- test.test_gdb.test_pretty_print: 33.4 sec
- test_socket: 33.4 sec
- test_xmlrpc: 29.1 sec

22 tests skipped:
test.test_asyncio.test_windows_events
test.test_asyncio.test_windows_utils test_bz2 test_dbm_gnu
test_dbm_ndbm test_devpoll test_idle test_ioctl test_kqueue
test_launcher test_msvcrt test_startfile test_tcl test_tkinter
test_ttk test_ttk_textonly test_turtle test_winapi
test_winconsoleio test_winreg test_winsound test_wmi

2 tests skipped (resource denied):
test_peg_generator test_zipfile64

448 tests OK.

Total duration: 2 min 41 sec
Total tests: run=42,576 skipped=1,689
Total test files: run=470/472 skipped=22 resource_denied=2
Result: SUCCESS

## meson

to be able to use meson with chibicc (meson doesn't know chibicc compiler), I changed the detect.py file in /usr/lib/python3/dist-packages/mesonbuild/compilers/detect.py to add support for chibicc. After that I can now using meson for some projects that are configured to use it.

## Limits

Expand All @@ -363,14 +458,34 @@ Some C projects doesn't compile for now. It helps to find some bugs and to try t
VLC : https://github.com/videolan/vlc.git

./bootstrap
CC=chibicc CFLAGS="-fPIC" DEFS="-DHAVE_CONFIG_H -DHAVE_ATTRIBUTE_PACKED -DVLC_USED -DVLC_API -DVLC_DEPRECATED -DVLC_MALLOC" ./configure --disable-lua --disable-a52 --disable-xcb --disable-qt --disable-po --disable-alsa --target=linux
CC=chibicc CFLAGS="-fPIC" DEFS="-DHAVE_CONFIG_H -DHAVE_ATTRIBUTE_PACKED -DVLC_USED -DVLC_API -DVLC_DEPRECATED -DVLC_MALLOC" LDFLAGS="-fPIC" ./configure --disable-xcb --disable-qt --disable-a52
make all

VLC doesn't compile with chibicc some issues to fix later.

lxc: https://github.com/lxc/lxc.git

CC=chibicc CFLAGS=-fpic LDFLAGS=-fpic meson build
cd build
meson compile
failed on lxc-attach compile for now


postgres: https://github.com/postgres/postgres.git

CC=chibicc CFLAGS="-fPIC" LDFLAGS="-fPIC" ./configure --host x86_64-linux-gnu
make
make check
Program received signal SIGSEGV, Segmentation fault.
#0 MemoryChunkSetHdrMask () at ../../../../src/include/utils/memutils_memorychunk.h:164
#1 0x0000000001f001b9 in AllocSetAlloc () at aset.c:885
the initdb command produced by chibicc failed with segmentation fault.


## TODO

- trying to compile other C projects from source to see what is missing or which bug we have with chibicc.
- Trying to find the root cause of segmentation fault with postgres initdb command.


## issues and pull requests fixed
Expand Down Expand Up @@ -407,10 +522,13 @@ Example of diagram generated with -dotfile parameter :
## release notes


1.0.21 fixing missing information on README (about help and ignored options). Fixing ISS-149 some extended assembly not taken in account (arrays). Fixing ISS-153 extended assembly giving different results when compiling with gcc or with chibicc. Fixing ISS-154 Extended assembly compiled with chibicc failed with ASSERT and works fine without assert function. Ignoring some parameters like -fvisibility=default or -flto=8.
1.0.22 Fixing ISS-149 some extended assembly not taken in account during libwebp compilation. Fixing ISS-156 fpie/pie/-fPIE not recognized by chibicc. Adding other parameters in ignored list. Fixing ISS-157 about union empty initializer like "union string_value lval = {}, rval = {};". Fixing ISS-158 during neovim compilation failure with not a struct nor a union. Fixing ISS-160 memcached compilation failed with IOV_MAX undefined (adding __GNU__ macro). Fixing ISS-161 trying to compile memcached failed with incorrect offset or not managed yet. Fixing ISS-163 during postgres compile failure with "invalid pointer dereference". Fixing ISS-162 during postgres compile failure with __typeof not recognized. Adding macro __INTEL_COMPILER. Adding include path to gcc that has many includes needed for some projects (adding in chibicc/include some of them like emmintrin.h, omp.h...). Adding some builtin void functions like _builtin_ia32_emms. Fixing ISS-165 during postgres compile failure due to staticAssertDecl function. Fixing ISS-166 during postgres compile segmentation fault (caused by VLA type in sizeof function). Fixing ISS-167 during postgres compile failure with bad register. Fixing ISS-168 during postgres compile failure with expected an expression due to incorrect previous fix(ISS-121). Adding by default to the linker "-allow-multiple-definition". Fixing issue with tgmath.h and ignoring .rsp files. Adding -dumpversion support provided by Urs Janssen. Added Xcode SDK path (from MarcusJohnson91). Fixing issue on extract_path function that caused corrupt malloc (ISS-170). Adding -ignore-assert to ignore static assertions (not managed at compile time now). Merge pull request #64 from arbruijn/multiple-funFix function declarations with shared return type. Fix assembler error on large files #119 by @rurban. Integrating many fixes from @n0tknowing. Adding CMakeLists.txt and using BOOST library to generate chibicc (from @Seeingu). Removing fix about old C style that causes other issue, it means that old C style doesn't compile anymore with chibicc.




## old release notes

<https://github.com/stormalf/chibicc/blob/main/RELEASE_NOTES.md>


4 changes: 3 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ Fixing issue #131 (ISS-131) parsing issue when trying to compile nginx project c
1.0.19 fixing extended assembly issue when a register is already used in the template, the variable should be stored in another register available.


1.0.20 Fixing ISS-143 extended assembly doesn't manage well input with r. Removing assign1.c test doesn't work with gcc. Fixing ISS-144 compiling util-linux failed with expression returning void is not supported. Fixing ISS-145 compiling util-linux failed with invalid initalizer2. Fixing ISS-147 compiling util-linux failed with undefined variable __BYTE_ORDER__. Fixing ISS-148 compiling VLC failed with storage class specifier not allowed caused by static_assert function. Fixing also some issues with extended assembly not working in some cases. Fixing issue with extended assembly in string_replace that truncates the null terminated character and causing during nginx compile failure due to incorrect character. Generating "nop" instruction each time we found the memory barrier : __asm__ volatile ("" ::: "memory"). Compiling successfully some projects like curl, nginx, zlib, util-linux, openssl, openssh-portable. But some tests failed for util-linux, openssl and curl that means that probably we have some bugs somewhere. Fixing ISS-152 extended assembly __asm__ __volatile__ ("rep; nop" ::: "memory"). Fixing ISS-150 fix on issue 40 caused other failures during VLC compilation. Fixing ISS-151 compiling neovim failed in struct_initializer2. Fixing ISS-153 -fomit-frame-pointer, -funwind-tables caused failure during neovim compilation. Adding -dM option to print the macro definition (it's not exactly the same result given by gcc) needed for compile neovim project.
1.0.20 Fixing ISS-143 extended assembly doesn't manage well input with r. Removing assign1.c test doesn't work with gcc. Fixing ISS-144 compiling util-linux failed with expression returning void is not supported. Fixing ISS-145 compiling util-linux failed with invalid initalizer2. Fixing ISS-147 compiling util-linux failed with undefined variable __BYTE_ORDER__. Fixing ISS-148 compiling VLC failed with storage class specifier not allowed caused by static_assert function. Fixing also some issues with extended assembly not working in some cases. Fixing issue with extended assembly in string_replace that truncates the null terminated character and causing during nginx compile failure due to incorrect character. Generating "nop" instruction each time we found the memory barrier : __asm__ volatile ("" ::: "memory"). Compiling successfully some projects like curl, nginx, zlib, util-linux, openssl, openssh-portable. But some tests failed for util-linux, openssl and curl that means that probably we have some bugs somewhere. Fixing ISS-152 extended assembly __asm__ __volatile__ ("rep; nop" ::: "memory"). Fixing ISS-150 fix on issue 40 caused other failures during VLC compilation. Fixing ISS-151 compiling neovim failed in struct_initializer2. Fixing ISS-153 -fomit-frame-pointer, -funwind-tables caused failure during neovim compilation. Adding -dM option to print the macro definition (it's not exactly the same result given by gcc) needed for compile neovim project.

1.0.21 fixing missing information on README (about help and ignored options). Fixing ISS-149 some extended assembly not taken in account (arrays). Fixing ISS-153 extended assembly giving different results when compiling with gcc or with chibicc. Fixing ISS-154 Extended assembly compiled with chibicc failed with ASSERT and works fine without assert function. Ignoring some parameters like -fvisibility=default or -flto=8.
27 changes: 19 additions & 8 deletions chibicc.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <math.h>


#ifndef __has_attribute
Expand All @@ -31,8 +32,8 @@
#endif

#define PRODUCT "chibicc"
#define VERSION "1.0.21"
#define MAXLEN 101
#define VERSION "1.0.22"
#define MAXLEN 501
#define DEFAULT_TARGET_MACHINE "x86_64-linux-gnu"

#define HELP PRODUCT " is a C compiler based on " PRODUCT " created by Rui Ueyama.\n \
Expand Down Expand Up @@ -66,6 +67,9 @@ this " PRODUCT " contains only some differences for now like new parameters\n"
used to automate file dependency management\n \
-fpic or -fPIC Generate position-independent code (PIC)\n \
-fno-pic disables the generation of position-independent code with relative address references\n \
-pie Create a dynamically linked position independent \n \
-fpie Create a dynamically linked position independent\n \
-fPIE Create a dynamically linked position independent\n \
-fcommon is the default if not specified, it's mainly useful to enable legacy code to link without errors\n \
-fno-common specifies that the compiler places uninitialized global variables in the BSS section of the object file.\n \
-static pass to the linker to link a program statically\n \
Expand All @@ -87,6 +91,7 @@ this " PRODUCT " contains only some differences for now like new parameters\n"
-dumpmachine it's required by some projects returns x86_64-linux-gnu\n \
-dotfile generates a file with .dot extension that can be visualized using graphviz package \n \
-dM Print macro definitions in -E mode instead of normal output\n \
-ignore-assert ingore static_assert and StaticAssertDecl functions if omit, the static assertions are not omitted \
chibicc [ -o <path> ] <file>\n"

typedef struct Type Type;
Expand Down Expand Up @@ -117,7 +122,7 @@ typedef struct
} StringArray;

void strarray_push(StringArray *arr, char *s);
char *format(char *fmt, ...) __attribute__((format(printf, 1, 2)));
char *format(const char *fmt, ...) __attribute__((format(printf, 1, 2)));

//
// tokenize.c
Expand All @@ -138,7 +143,7 @@ typedef enum
typedef struct
{
char *name;
int file_no;
unsigned int file_no;
char *contents;

// For #line directive
Expand Down Expand Up @@ -178,7 +183,7 @@ Token *skip(Token *tok, char *op, Context *ctx);
bool consume(Token **rest, Token *tok, char *str);
void convert_pp_tokens(Token *tok);
File **get_input_files(void);
File *new_file(char *name, int file_no, char *contents);
File *new_file(char *name, unsigned int file_no, char *contents);
Token *tokenize_string_literal(Token *tok, Type *basety);
Token *tokenize(File *file);
Token *tokenize_file(char *filename);
Expand Down Expand Up @@ -311,7 +316,8 @@ typedef enum
} NodeKind;

// AST node type
struct Node
struct
Node
{
NodeKind kind; // Node kind
Node *next; // Next node
Expand Down Expand Up @@ -396,6 +402,7 @@ Obj *parse(Token *tok);
VarScope *find_var(Token *tok);
Obj *find_func(char *name);


extern bool opt_fbuiltin;
//
// type.c
Expand Down Expand Up @@ -536,7 +543,7 @@ char *reg_di(int sz);
char *reg_si(int sz);
char *reg_r8w(int sz);
char *reg_r9w(int sz);
void assign_lvar_offsets_assembly(Obj *fn);
void assign_lvar_offsets(Obj *prog);
int add_register_used(char *regist);
void clear_register_used();
char *register32_to_64(char *regist);
Expand Down Expand Up @@ -590,9 +597,13 @@ void hashmap_test(void);

bool file_exists(char *path);
void dump_machine(void);
void dump_version(void);
bool startsWith(const char *restrict string, const char *restrict prefix);

extern StringArray include_paths;
extern bool opt_fpic;
extern bool opt_fpie;
extern bool opt_shared;
extern bool opt_fcommon;
extern char *base_file;
extern char *dot_file;
Expand All @@ -604,7 +615,7 @@ extern bool isDotfile;
extern bool isDebug;
extern bool isPrintMacro;
extern char *extract_filename(char *tmpl);
extern char *extract_path(char *tmpl, char *basename);
extern char *extract_path(char *tmpl);

//
// extended_asm.c
Expand Down
Loading

0 comments on commit c2063a3

Please sign in to comment.