Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tcc support (and fix msbuild without visual studio install) #411

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ paket-files/
*.gch

# Libraries
*.def
*.lib
*.a
*.la
Expand Down
94 changes: 54 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,74 @@ SRC = $(wildcard $(COMPILER_DIR)*.c) \
$(wildcard $(OPT_DIR)*.c)

INCLUDE = -I$(COMPILER_DIR) -I$(RUNTIME_DIR) -I$(SHARED_DIR) -I$(UTILS_DIR) -I$(OPT_DIR)
CFLAGS = $(INCLUDE) -std=gnu99 -fgnu89-inline -fPIC -DBUILD_GRAVITY_API
OBJ = $(SRC:.c=.o)

CFLAGS = $(INCLUDE) -fPIC -DBUILD_GRAVITY_API
ifneq ($(CC),tcc)
CFLAGS += -std=gnu99 -fgnu89-inline
endif


ifeq ($(OS),Windows_NT)
# Windows
LIBTARGET = gravity.dll
LDFLAGS = -lm -lShlwapi
# Windows
LIBTARGET = gravity.dll
LDFLAGS = -lShlwapi
ifeq ($(CC),tcc)
LDFLAGS += -lucrtbase
EXEXT = .exe
else
LDFLAGS += -lm
endif
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
# MacOS
LIBTARGET = libgravity.dylib
LDFLAGS = -lm
else ifeq ($(UNAME_S),OpenBSD)
# OpenBSD
LIBTARGET = libgravity.so
LDFLAGS = -lm
else ifeq ($(UNAME_S),FreeBSD)
# FreeBSD
LIBTARGET = libgravity.so
LDFLAGS = -lm
else ifeq ($(UNAME_S),NetBSD)
# NetBSD
LIBTARGET = libgravity.so
LDFLAGS = -lm
else ifeq ($(UNAME_S),DragonFly)
# DragonFly
LIBTARGET = libgravity.so
LDFLAGS = -lm
else
# Linux
LIBTARGET = libgravity.so
LDFLAGS = -lm -lrt
endif
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
# MacOS
LIBTARGET = libgravity.dylib
LDFLAGS = -lm
else ifeq ($(UNAME_S),OpenBSD)
# OpenBSD
LIBTARGET = libgravity.so
LDFLAGS = -lm
else ifeq ($(UNAME_S),FreeBSD)
# FreeBSD
LIBTARGET = libgravity.so
LDFLAGS = -lm
else ifeq ($(UNAME_S),NetBSD)
# NetBSD
LIBTARGET = libgravity.so
LDFLAGS = -lm
else ifeq ($(UNAME_S),DragonFly)
# DragonFly
LIBTARGET = libgravity.so
LDFLAGS = -lm
else
# Linux
LIBTARGET = libgravity.so
LDFLAGS = -lm -lrt
endif
endif

ifeq ($(mode),debug)
CFLAGS += -g -O0 -DDEBUG
CFLAGS += -g -O0 -DDEBUG
else
CFLAGS += -O2
CFLAGS += -O2
endif

all: gravity
all: gravity$(EXEXT)

gravity: $(OBJ) $(GRAVITY_SRC)
gravity$(EXEXT): $(OBJ) $(GRAVITY_SRC)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
example: $(OBJ) $(EXAMPLE_SRC)

example$(EXEXT): $(OBJ) $(EXAMPLE_SRC)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

lib: gravity
lib: gravity$(EXEXT)
$(CC) -shared -o $(LIBTARGET) $(OBJ) $(LDFLAGS)

cleanobj:
rm -f $(OBJ)

clean:
rm -f $(OBJ) gravity example libgravity.so gravity.dll
.PHONY: all clean gravity example
rm -f $(OBJ) gravity gravity.def gravity.exe example example.def example.exe libgravity.so gravity.dll

.PHONY: all clean gravity$(EXEXT) example$(EXEXT)
5 changes: 5 additions & 0 deletions rm.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

@SET OPTS= %*
@SET OPTS=%OPTS:/=\%
@SET OPTS=%OPTS: -f = /F %
@DEL %OPTS%
2 changes: 1 addition & 1 deletion src/cli/gravity.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static const char *unittest_read (const char *path, size_t *size, uint32_t *file
static void unittest_scan (const char *folder_path, unittest_data *data) {
DIRREF dir = directory_init(folder_path);
if (!dir) return;
#ifdef WIN32
#ifdef _WIN32
char outbuffer[MAX_PATH];
#else
char *outbuffer = NULL;
Expand Down
52 changes: 26 additions & 26 deletions src/compiler/gravity_codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -647,47 +647,47 @@ static void visit_loop_for_stmt (gvisitor_t *self, gnode_loop_stmt_t *node) {
//
// to:
// {
// var $expr = expr;
// var $value = $expr.iterate(null);
// while ($value) {
// cond = $expr.next($value);
// var expr = expr;
// var value = expr.iterate(null);
// while (value) {
// cond = expr.next(value);
// stmp;
// $value = $expr.iterate($value);
// value = expr.iterate(value);
// }
// }

// $expr and $value are temporary registers that must be protected, otherwise
// expr and value are temporary registers that must be protected, otherwise
// in visit_compound_statement, all temp registers are released within ircode_register_temps_clear
uint32_t $expr = ircode_register_push_temp_protected(code); // ++TEMP => 1
uint32_t $value = ircode_register_push_temp_protected(code); // ++TEMP => 2
uint32_t expr = ircode_register_push_temp_protected(code); // ++TEMP => 1
uint32_t value = ircode_register_push_temp_protected(code); // ++TEMP => 2

// get cpool index for the required methods
uint16_t iterate_idx = gravity_function_cpool_add(GET_VM(), context_function, VALUE_FROM_CSTRING(NULL, ITERATOR_INIT_FUNCTION));
uint16_t next_idx = gravity_function_cpool_add(GET_VM(), context_function, VALUE_FROM_CSTRING(NULL, ITERATOR_NEXT_FUNCTION));
uint32_t cond_idx = node2index(node->cond);

// generate code for $expr = expr (so expr is only evaluated once)
// generate code for expr = expr (so expr is only evaluated once)
visit(node->expr);
uint32_t once_expr = ircode_register_pop(code);
if (once_expr == REGISTER_ERROR) report_error(self, (gnode_t *)node, "Invalid for expression.");
ircode_add(code, MOVE, $expr, once_expr, 0, LINE_NUMBER(node));
ircode_add(code, MOVE, expr, once_expr, 0, LINE_NUMBER(node));

// generate code for $value = $expr.iterate(null);
// generate code for value = expr.iterate(null);
uint32_t iterate_fn = ircode_register_push_temp_protected(code); // ++TEMP => 3
ircode_add(code, LOADK, iterate_fn, iterate_idx, 0, LINE_NUMBER(node));
ircode_add(code, LOAD, iterate_fn, $expr, iterate_fn, LINE_NUMBER(node));
ircode_add(code, LOAD, iterate_fn, expr, iterate_fn, LINE_NUMBER(node));

uint32_t next_fn = ircode_register_push_temp_protected(code); // ++TEMP => 4
ircode_add(code, LOADK, next_fn, next_idx, 0, LINE_NUMBER(node));
ircode_add(code, LOAD, next_fn, $expr, next_fn, LINE_NUMBER(node));
ircode_add(code, LOAD, next_fn, expr, next_fn, LINE_NUMBER(node));

uint32_t temp1 = ircode_register_push_temp(code); // ++TEMP => 5
ircode_add(code, MOVE, temp1, iterate_fn, 0, LINE_NUMBER(node));
uint32_t temp2 = ircode_register_push_temp(code); // ++TEMP => 6
ircode_add(code, MOVE, temp2, $expr, 0, LINE_NUMBER(node));
ircode_add(code, MOVE, temp2, expr, 0, LINE_NUMBER(node));
uint32_t temp3 = ircode_register_push_temp(code); // ++TEMP => 7
ircode_add(code, LOADK, temp3, CPOOL_VALUE_NULL, 0, LINE_NUMBER(node));
ircode_add(code, CALL, $value, temp1, 2, LINE_NUMBER(node));
ircode_add(code, CALL, value, temp1, 2, LINE_NUMBER(node));
uint32_t temp = ircode_register_pop(code); // --TEMP => 6
DEBUG_ASSERT(temp != REGISTER_ERROR, "Unexpected register error.");
temp = ircode_register_pop(code); // --TEMP => 5
Expand All @@ -704,16 +704,16 @@ static void visit_loop_for_stmt (gvisitor_t *self, gnode_loop_stmt_t *node) {
ircode_setlabel_check(code, labelCheck);

ircode_marklabel(code, labelTrue, LINE_NUMBER(node));
ircode_add(code, JUMPF, $value, labelFalse, 1, LINE_NUMBER(node)); // flag JUMPF instruction to check ONLY BOOL values
ircode_add(code, JUMPF, value, labelFalse, 1, LINE_NUMBER(node)); // flag JUMPF instruction to check ONLY BOOL values

// cond = $expr.next($value);
// cond = expr.next(value);
// cond is a local variable
temp1 = ircode_register_push_temp_protected(code); // ++TEMP => 5
ircode_add(code, MOVE, temp1, next_fn, 0, LINE_NUMBER(node));
temp2 = ircode_register_push_temp_protected(code); // ++TEMP => 6
ircode_add(code, MOVE, temp2, $expr, 0, LINE_NUMBER(node));
ircode_add(code, MOVE, temp2, expr, 0, LINE_NUMBER(node));
temp3 = ircode_register_push_temp_protected(code); // ++TEMP => 7
ircode_add(code, MOVE, temp3, $value, 0, LINE_NUMBER(node));
ircode_add(code, MOVE, temp3, value, 0, LINE_NUMBER(node));
ircode_add(code, CALL, cond_idx, temp1, 2, LINE_NUMBER(node));

// process statement
Expand All @@ -733,15 +733,15 @@ static void visit_loop_for_stmt (gvisitor_t *self, gnode_loop_stmt_t *node) {
DEBUG_ASSERT(temp != REGISTER_ERROR, "Unexpected register error.");

ircode_marklabel(code, labelCheck, LINE_NUMBER(node));
// update $value for the next check
// $value = $expr.iterate($value);
// update value for the next check
// value = expr.iterate(value);
temp1 = ircode_register_push_temp(code); // ++TEMP => 5
ircode_add(code, MOVE, temp1, iterate_fn, 0, LINE_NUMBER(node));
temp2 = ircode_register_push_temp(code); // ++TEMP => 6
ircode_add(code, MOVE, temp2, $expr, 0, LINE_NUMBER(node));
ircode_add(code, MOVE, temp2, expr, 0, LINE_NUMBER(node));
temp2 = ircode_register_push_temp(code); // ++TEMP => 7
ircode_add(code, MOVE, temp2, $value, 0, LINE_NUMBER(node));
ircode_add(code, CALL, $value, temp1, 2, LINE_NUMBER(node));
ircode_add(code, MOVE, temp2, value, 0, LINE_NUMBER(node));
ircode_add(code, CALL, value, temp1, 2, LINE_NUMBER(node));
temp = ircode_register_pop(code); // --TEMP => 6
DEBUG_ASSERT(temp != REGISTER_ERROR, "Unexpected register error.");
temp = ircode_register_pop(code); // --TEMP => 5
Expand All @@ -767,8 +767,8 @@ static void visit_loop_for_stmt (gvisitor_t *self, gnode_loop_stmt_t *node) {
DEBUG_ASSERT(temp != REGISTER_ERROR, "Unexpected register error.");

// mark main for registers as reusable
ircode_register_temp_unprotect(code, $expr);
ircode_register_temp_unprotect(code, $value);
ircode_register_temp_unprotect(code, expr);
ircode_register_temp_unprotect(code, value);
ircode_register_temp_unprotect(code, iterate_fn);
ircode_register_temp_unprotect(code, next_fn);

Expand Down
6 changes: 3 additions & 3 deletions src/optionals/gravity_opt_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ static void scan_directory (gravity_vm *vm, char *path, bool recursive, gravity_
mem_free(name);
gravity_value_t p2 = VALUE_FROM_CSTRING(vm, path);
gravity_value_t p3 = VALUE_FROM_BOOL(true);
gravity_value_t params[] = {p1, p2, p3};
gravity_value_t params[3] = {p1, p2, p3};

gravity_vm_runclosure(vm, closure, VALUE_FROM_NULL, params, 3);
if (n) *n = *n + 1;
}

#ifdef WIN32
#ifdef _WIN32
char buffer[MAX_PATH];
#else
char *buffer = NULL;
Expand All @@ -192,7 +192,7 @@ static void scan_directory (gravity_vm *vm, char *path, bool recursive, gravity_
gravity_value_t p1 = VALUE_FROM_CSTRING(vm, target_file);
gravity_value_t p2 = VALUE_FROM_CSTRING(vm, full_path);
gravity_value_t p3 = VALUE_FROM_BOOL(false);
gravity_value_t params[] = {p1, p2, p3};
gravity_value_t params[3] = {p1, p2, p3};
mem_free(full_path);

gravity_vm_runclosure(vm, closure, VALUE_FROM_NULL, params, 3);
Expand Down
4 changes: 3 additions & 1 deletion src/shared/gravity_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef __GRAVITY_CONFIG__
#define __GRAVITY_CONFIG__

#ifdef _MSC_VER
#if defined(_MSC_VER) || ( defined(__TINYC__) && defined(_WIN32) )

#pragma comment(lib, "shlwapi")
#include <basetsd.h>
Expand All @@ -23,7 +23,9 @@
#endif

typedef SSIZE_T ssize_t;
#ifndef __TINYC__
typedef int mode_t;
#endif

#define open _open
#define close _close
Expand Down
Loading
Loading