~tim/malcc

bfbfb3348092718803c367eea199a125d9cb4d84 — Tim Morgan 1 year, 4 months ago 4c56b95
Revert "Use tcc to aot compile instead of gcc"

This reverts commit 4c56b952ba1ebfb227e6a8c34a2567f9e4115fd7.

This didn't work on macOS :-(

From my understanding, TCC does not support building an executable on
macOS. So, we're stuck with gcc/clang for now.
5 files changed, 17 insertions(+), 38 deletions(-)

M malcc.c
M types.c
M types.h
M util.c
M util.h
M malcc.c => malcc.c +8 -9
@@ 988,15 988,14 @@ void aot_compile(MalType *arg_vec) {
    FILE *f = fopen(mal_sprintf("%S.c", out_filename)->str, "w");
    fprintf(f, "%s\n", out->str);
    fclose(f);
    MalType *cmd = mal_string(
      "./tinycc/tcc -g -I./tinycc -B./tinycc -o FILENAME FILENAME.c " \
      "./reader.c ./printer.c ./hashmap.c ./types.c ./util.c ./env.c ./core.c ./tinycc/libtcc.a " \
      "-ledit -lgc -lpcre -ldl"
    );
    cmd = mal_string_replace_all(cmd, "./", mal_sprintf("%s/", PATH)->str);
    cmd = mal_string_replace_all(cmd, "FILENAME", out_filename->str);
    fprintf(stderr, "%s\n", cmd->str);
    int result = system(cmd->str);
    char *cmd = mal_sprintf(
      "gcc -g -I %s/tinycc -I %s -o %S %S.c %s/reader.c %s/printer.c %s/hashmap.c %s/types.c %s/util.c %s/env.c %s/core.c " \
      "%s/tinycc/libtcc.a -ledit -lgc -lpcre -ldl",
      PATH, PATH, out_filename, out_filename, PATH, PATH, PATH, PATH, PATH, PATH, PATH,
      PATH
    )->str;
    fprintf(stderr, "%s\n", cmd);
    int result = system(cmd);
    if (result != 0) {
      fprintf(stderr, "There was an error compiling.\n");
      exit(1);

M types.c => types.c +8 -18
@@ 179,30 179,20 @@ MalType* mal_string_replace(MalType *orig, char *find, char *replace) {
  assert(pos);
  size_t index = pos - orig->str;
  size_t find_len = strlen(find);
  char *before = substring(orig->str, 0, index);
  char *after = substring(orig->str, index + find_len, orig->str_len - (index + find_len));
  char *before = mal_string_substring(orig, 0, index);
  char *after = mal_string_substring(orig, index + find_len, orig->str_len - (index + find_len));
  MalType *final = mal_string(before);
  mal_string_append(final, replace);
  mal_string_append(final, after);
  assert(strlen(final->str) == final->str_len);
  return final;
}

MalType* mal_string_replace_all(MalType *orig, char *find, char *replace) {
  MalType *final = mal_string("");
  char *pos = NULL;
  char *str = orig->str, *before;
  size_t find_len = strlen(find);
  while ((pos = strstr(str, find))) {
    size_t index = pos - str;
    before = substring(str, 0, index);
    mal_string_append(final, before);
    mal_string_append(final, replace);
    str = str + index + find_len;
  }
  mal_string_append(final, str);
  assert(strlen(final->str) == final->str_len);
  return final;
char* mal_string_substring(MalType *orig, size_t start, size_t len) {
  assert(start < orig->str_len);
  assert(start + len <= orig->str_len);
  char *buffer = GC_MALLOC(len + 1);
  snprintf(buffer, len + 1, "%s", orig->str + start);
  return buffer;
}

MalType* mal_string_to_list(MalType *orig) {

M types.h => types.h +1 -1
@@ 140,7 140,7 @@ void mal_string_append_mal_string(MalType *val, MalType *str);
void mal_string_append_char(MalType *val, char c);
void mal_string_append_long_long(MalType *val, long long n);
MalType* mal_string_replace(MalType *val, char *find, char *replace);
MalType* mal_string_replace_all(MalType *orig, char *find, char *replace);
char* mal_string_substring(MalType *orig, size_t start, size_t len);
MalType* mal_string_to_list(MalType *orig);

MalType* mal_keyword(char *name);

M util.c => util.c +0 -9
@@ 56,15 56,6 @@ char* string(char *str) {
  return copy;
}

char* substring(char *orig, size_t start, size_t len) {
  size_t orig_len = strlen(orig);
  assert(start < orig_len);
  assert(start + len <= orig_len);
  char *buffer = GC_MALLOC(len + 1);
  snprintf(buffer, len + 1, "%s", orig + start);
  return buffer;
}

MalType* program_arguments_as_vector(int argc, char *argv[]) {
  MalType *arg_vec = mal_vector();
  for (int i=1; i<argc; i++) {

M util.h => util.h +0 -1
@@ 13,7 13,6 @@
char* long_long_to_string(long long num);
size_t num_char_len(long long num);
char* string(char *str);
char* substring(char *orig, size_t start, size_t len);
void add_core_ns_to_env(MalEnv *env);
MalType* program_arguments_as_vector(int argc, char *argv[]);
MalType* read_file(char *filename);