Use long as implicit type in chibicc

This makes it possible to write 64-bit C without headers and prototypes.
This commit is contained in:
Justine Tunney 2023-07-09 10:10:34 -07:00
parent 42ba9901e4
commit d3cf9d4ef1
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
3 changed files with 77 additions and 5 deletions

View file

@ -689,7 +689,9 @@ static void OnCtrlC(int sig, siginfo_t *si, void *ctx) {
}
int chibicc(int argc, char **argv) {
#ifndef NDEBUG
ShowCrashReports();
#endif
atexit(chibicc_cleanup);
sigaction(SIGINT, &(struct sigaction){.sa_sigaction = OnCtrlC}, NULL);
for (int i = 1; i < argc; i++) {

View file

@ -22,11 +22,14 @@
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/ffs.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
#include "libc/x/xasprintf.h"
#include "third_party/chibicc/chibicc.h"
#include "third_party/chibicc/kw.h"
#define IMPLICIT_FUNCTIONS
typedef struct InitDesg InitDesg;
typedef struct Initializer Initializer;
typedef struct Scope Scope;
@ -665,7 +668,7 @@ static Type *declspec(Token **rest, Token *tok, VarAttr *attr) {
INT128 = 1 << 19,
};
unsigned char kw;
Type *ty = copy_type(ty_int);
Type *ty = copy_type(ty_long); // [jart] use long as implicit type
int counter = 0;
bool is_const = false;
bool is_atomic = false;
@ -3508,14 +3511,17 @@ static Node *primary(Token **rest, Token *tok) {
if (tok->kind == TK_IDENT) {
// Variable or enum constant
VarScope *sc = find_var(tok);
*rest = tok->next;
#ifdef IMPLICIT_FUNCTIONS
// [jart] support implicit function declarations with `long` type
if (!sc && EQUAL(tok->next, "(")) {
Type *ty = func_type(ty_long);
ty->is_variadic = true;
return new_var_node(new_gvar(strndup(tok->loc, tok->len), ty), tok);
Obj *fn = new_var(strndup(tok->loc, tok->len), ty);
fn->next = globals;
fn->is_function = true;
globals = fn;
sc = find_var(tok);
}
#endif
*rest = tok->next;
// For "static inline" function
if (sc && sc->var && sc->var->is_function) {
if (current_fn) {

View file

@ -0,0 +1,64 @@
add(x, y) {
return x + y;
}
implicit_functions_are_long() {
if (add(0xffffffff, 0xffffffff) != 0x0001fffffffe) {
__builtin_trap();
}
}
external_functions_are_long() {
if (_bsrl(0x0001000000000000) != 48) {
__builtin_trap();
}
}
auto_variables_are_long() {
auto x, y, z;
x = 0x0101fffffffe;
y = 0x0201fffffffe;
z = x + y;
if (z != 0x0303fffffffc) {
__builtin_trap();
}
}
static_variables_are_long() {
static x, y, z;
x = 0x0101fffffffe;
y = 0x0201fffffffe;
z = x + y;
if (z != 0x0303fffffffc) {
__builtin_trap();
}
}
x_ = 0x0101fffffffe;
y_ = 0x0201fffffffe;
z_;
globo_variables_are_long() {
z_ = x_ + y_;
if (z_ != 0x0303fffffffc) {
__builtin_trap();
}
}
implicit_function_string() {
auto lol = strdup("hello");
if (strlen(lol) != 5) {
__builtin_trap();
}
free(lol);
}
main() {
ShowCrashReports();
implicit_functions_are_long();
external_functions_are_long();
auto_variables_are_long();
static_variables_are_long();
globo_variables_are_long();
implicit_function_string();
}