~tim/malcc

4c56b952ba1ebfb227e6a8c34a2567f9e4115fd7 — Tim Morgan 5 years ago f53eddd
Use tcc to aot compile instead of gcc
5 files changed, 38 insertions(+), 17 deletions(-)

M malcc.c
M types.c
M types.h
M util.c
M util.h
M malcc.c => malcc.c +9 -8
@@ 988,14 988,15 @@ 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);
    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);
    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);
    if (result != 0) {
      fprintf(stderr, "There was an error compiling.\n");
      exit(1);

M types.c => types.c +18 -8
@@ 179,20 179,30 @@ 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 = mal_string_substring(orig, 0, index);
  char *after = mal_string_substring(orig, index + find_len, orig->str_len - (index + find_len));
  char *before = substring(orig->str, 0, index);
  char *after = substring(orig->str, 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;
}

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_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;
}

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);
char* mal_string_substring(MalType *orig, size_t start, size_t len);
MalType* mal_string_replace_all(MalType *orig, char *find, char *replace);
MalType* mal_string_to_list(MalType *orig);

MalType* mal_keyword(char *name);

M util.c => util.c +9 -0
@@ 56,6 56,15 @@ 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 +1 -0
@@ 13,6 13,7 @@
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);