summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarson Fleming <cflems@cflems.net>2026-07-26 11:09:14 -0700
committerCarson Fleming <cflems@cflems.net>2026-07-26 11:09:14 -0700
commit6a169de321b131e73b86967a1a8564365217275a (patch)
treea407e682af226a9d77acbaef6cb8bf579f2a3ddf
parent0cabbd2a0853c332c82af621b8716643741d758a (diff)
downloadccc-6a169de321b131e73b86967a1a8564365217275a.tar.gz
clean up types
-rw-r--r--ast.h4
-rw-r--r--ccc.h3
-rw-r--r--codegen.c20
-rw-r--r--lexer.c9
-rw-r--r--lexer.h9
-rw-r--r--scope.c39
-rw-r--r--scope.h16
7 files changed, 50 insertions, 50 deletions
diff --git a/ast.h b/ast.h
index a51bfea..c41713d 100644
--- a/ast.h
+++ b/ast.h
@@ -11,11 +11,11 @@ struct type_ref_node {
};
struct int_lit_node {
- long long val;
+ integral_t val;
};
struct float_lit_node {
- double val;
+ floating_t val;
};
struct char_lit_node {
diff --git a/ccc.h b/ccc.h
index b846056..475704a 100644
--- a/ccc.h
+++ b/ccc.h
@@ -3,4 +3,7 @@
#define CCC_PANIC { perror("ccc"); exit(1); }
+typedef unsigned long long integral_t;
+typedef double floating_t;
+
#endif
diff --git a/codegen.c b/codegen.c
index a32046b..019baf0 100644
--- a/codegen.c
+++ b/codegen.c
@@ -34,12 +34,12 @@ static const struct storage_location MULDIV_OVERFLOW_LOC = {
static struct scope* scope;
static const struct fn_decl_node* active_fn;
-static unsigned long long branch_counter = 0;
-static unsigned long long loop_counter = 0;
+static integral_t branch_counter = 0;
+static integral_t loop_counter = 0;
static void enter_scope(
struct scope* child_scope,
- unsigned long long bp_offset
+ integral_t bp_offset
) {
if (child_scope == NULL ||
child_scope->next_out != scope)
@@ -99,7 +99,7 @@ static void deallocate_temporary(FILE* outfile, const struct lval_def* tmp) {
static void emit_storage_loc(
FILE* outfile,
const struct storage_location* loc,
- unsigned long long sz
+ integral_t sz
) {
switch (loc->type) {
case STO_LABEL:
@@ -153,7 +153,7 @@ static bool locs_equal(
CGEN_PANIC("unhandled storage type case");
}
-static void emit_size_const(FILE* outfile, unsigned long long sz) {
+static void emit_size_const(FILE* outfile, integral_t sz) {
if (sz > 4) fprintf(outfile, "qword ");
else if (sz > 2) fprintf(outfile, "dword ");
else if (sz > 1) fprintf(outfile, "word ");
@@ -273,7 +273,7 @@ static void emit_char_lit(
dst,
&(struct storage_location) {
.type = STO_IMM,
- .value = (unsigned long long) node->val
+ .value = (integral_t) node->val
});
}
}
@@ -342,8 +342,8 @@ static void emit_call(
const struct call_node* node,
const struct lval_def* dst
) {
- unsigned long long orig_bp_offset = scope->bp_offset;
- unsigned long long arg_bp_offset = orig_bp_offset;
+ integral_t orig_bp_offset = scope->bp_offset;
+ integral_t arg_bp_offset = orig_bp_offset;
struct decl_list_node* arg_decl = node->called_fn_ref->args;
struct expr_list_node* arg_eval = node->args;
@@ -574,7 +574,7 @@ static void emit_if(FILE* outfile, const struct if_node* node) {
emit_expr(outfile, node->cond, &cond_result);
emit_cmp_zero(outfile, &cond_result);
- unsigned long long branch_num = ++branch_counter;
+ integral_t branch_num = ++branch_counter;
fprintf(outfile, "\tjz branch_false@%lld\n", branch_num);
emit_stmt(outfile, node->true_branch);
@@ -611,7 +611,7 @@ static void emit_loop(FILE* outfile, const struct loop_node* node) {
if (node->init != NULL) emit_loop_init(outfile, node->init);
- unsigned long long loop_num = ++loop_counter;
+ integral_t loop_num = ++loop_counter;
if (node->cond != NULL) {
struct lval_def cond_dst =
allocate_temporary(outfile, node->cond->resolved_type);
diff --git a/lexer.c b/lexer.c
index f5072a1..c8c98db 100644
--- a/lexer.c
+++ b/lexer.c
@@ -1,4 +1,3 @@
-#include "ccc.h"
#include "lexer.h"
#include <stdlib.h>
#include <stdio.h>
@@ -104,11 +103,11 @@ static unsigned char digit_val(int c, unsigned char base) {
static void lex_float_lit(
struct token* p_token,
unsigned char base,
- float_lit_t iv
+ floating_t iv
) {
if (consume_char() != '.')
LEXER_PANIC("sanity error, float literal without decimal point");
- float_lit_t exp = 1.0;
+ floating_t exp = 1.0;
while (is_hexadecimal(lookahead)) {
int c = consume_char();
exp /= base;
@@ -124,7 +123,7 @@ static void lex_float_lit(
};
}
-static void lex_int_lit(struct token* p_token, int_lit_t iv) {
+static void lex_int_lit(struct token* p_token, integral_t iv) {
unsigned char base = 10;
if (iv == 0) {
@@ -149,7 +148,7 @@ static void lex_int_lit(struct token* p_token, int_lit_t iv) {
}
if (lookahead == '.') {
- lex_float_lit(p_token, base, (float_lit_t) iv);
+ lex_float_lit(p_token, base, (floating_t) iv);
return;
}
diff --git a/lexer.h b/lexer.h
index aefca82..f228b2a 100644
--- a/lexer.h
+++ b/lexer.h
@@ -1,6 +1,8 @@
#ifndef LEXER_H
#define LEXER_H
+#include "ccc.h"
+
enum token_type {
TK_NOT_FOUND,
TK_IDENT,
@@ -54,15 +56,12 @@ enum token_type {
TK_SHL_EQ
};
-typedef unsigned long long int_lit_t;
-typedef double float_lit_t;
-
struct token {
enum token_type type;
union {
char* ident;
- int_lit_t int_lit;
- float_lit_t float_lit;
+ integral_t int_lit;
+ floating_t float_lit;
char char_lit;
char* str_lit;
void* unused;
diff --git a/scope.c b/scope.c
index b37e4b2..d725c2f 100644
--- a/scope.c
+++ b/scope.c
@@ -17,14 +17,14 @@ static void var_destroy(struct var_def* var) {
}
void scope_destroy(struct scope* scope) {
- for (unsigned long long i = 0; i < scope->type_cap; i++) {
+ for (integral_t i = 0; i < scope->type_cap; i++) {
if (scope->types[i] != NULL) {
free(scope->types[i]);
}
}
free(scope->types);
- for (unsigned long long i = 0; i < scope->var_cap; i++) {
+ for (integral_t i = 0; i < scope->var_cap; i++) {
if (scope->vars[i] != NULL) {
var_destroy(scope->vars[i]);
free(scope->vars[i]);
@@ -33,25 +33,22 @@ void scope_destroy(struct scope* scope) {
free(scope->vars);
}
-static inline unsigned long long advance_hash(
- unsigned long long hash,
- unsigned long long val,
- unsigned long long cap
+static inline integral_t advance_hash(
+ integral_t hash,
+ integral_t val,
+ integral_t cap
) {
return ((hash << 5) - hash + val) % cap;;
}
-static unsigned long long hash_name(const char* name, unsigned long long cap) {
- unsigned long long hash = 0, i = 0;
+static integral_t hash_name(const char* name, integral_t cap) {
+ integral_t hash = 0, i = 0;
while (name[i] != 0) hash = advance_hash(hash, name[i++], cap);
return hash;
}
-unsigned long long hash_type_key(
- const struct type_key* key,
- unsigned long long cap
-) {
- unsigned long long hash = hash_name(key->name, cap);
+integral_t hash_type_key(const struct type_key* key, integral_t cap) {
+ integral_t hash = hash_name(key->name, cap);
hash = advance_hash(hash, key->how_long, cap);
hash = advance_hash(hash, key->marked_signed, cap);
hash = advance_hash(hash, key->marked_unsigned, cap);
@@ -72,8 +69,8 @@ static struct type_def** type_cell(
const struct scope* scope,
const struct type_key* key
) {
- unsigned long long orig_idx = hash_type_key(key, scope->type_cap);
- unsigned long long idx = orig_idx;
+ integral_t orig_idx = hash_type_key(key, scope->type_cap);
+ integral_t idx = orig_idx;
do {
if (scope->types[idx] == NULL
@@ -85,7 +82,7 @@ static struct type_def** type_cell(
static void rehash_types(struct scope* scope) {
struct type_def** old_types = scope->types;
- unsigned long long old_cap = scope->type_cap;
+ integral_t old_cap = scope->type_cap;
scope->type_cap *= 2;
scope->types = calloc(scope->type_cap, sizeof(struct type_def*));
if (scope->types == NULL) {
@@ -93,7 +90,7 @@ static void rehash_types(struct scope* scope) {
exit(1);
}
- for (unsigned long long i = 0; i < old_cap; i++) {
+ for (integral_t i = 0; i < old_cap; i++) {
if (old_types[i] == NULL) continue;
struct type_def** cell = type_cell(scope, &old_types[i]->key);
@@ -110,8 +107,8 @@ static struct var_def** var_cell(
const struct scope* scope,
const char* name
) {
- unsigned long long orig_idx = hash_name(name, scope->var_cap);
- unsigned long long idx = orig_idx;
+ integral_t orig_idx = hash_name(name, scope->var_cap);
+ integral_t idx = orig_idx;
do {
if (scope->vars[idx] == NULL
@@ -123,7 +120,7 @@ static struct var_def** var_cell(
static void rehash_vars(struct scope* scope) {
struct var_def** old_vars = scope->vars;
- unsigned long long old_cap = scope->var_cap;
+ integral_t old_cap = scope->var_cap;
scope->var_cap *= 2;
scope->vars = calloc(scope->var_cap, sizeof(struct var_def*));
if (scope->vars == NULL) {
@@ -131,7 +128,7 @@ static void rehash_vars(struct scope* scope) {
exit(1);
}
- for (unsigned long long i = 0; i < old_cap; i++) {
+ for (integral_t i = 0; i < old_cap; i++) {
if (old_vars[i] == NULL) continue;
struct var_def** cell = var_cell(scope, old_vars[i]->name);
diff --git a/scope.h b/scope.h
index 09a1375..da35044 100644
--- a/scope.h
+++ b/scope.h
@@ -1,6 +1,8 @@
#ifndef SCOPE_H
#define SCOPE_H
+#include "ccc.h"
+
struct storage_location {
enum {
STO_REG,
@@ -16,7 +18,7 @@ struct storage_location {
const struct reg* reg;
const char* label;
long long bp_offset;
- unsigned long long value;
+ integral_t value;
struct fn_decl_node* decl;
};
};
@@ -37,7 +39,7 @@ struct type_def {
struct type_key key;
bool is_signed;
bool is_floating;
- unsigned long long sz;
+ integral_t sz;
};
struct var_def {
@@ -49,15 +51,15 @@ struct var_def {
struct scope {
struct type_def** types;
- unsigned long long type_sz;
- unsigned long long type_cap;
+ integral_t type_sz;
+ integral_t type_cap;
struct var_def** vars;
- unsigned long long var_sz;
- unsigned long long var_cap;
+ integral_t var_sz;
+ integral_t var_cap;
struct scope* next_out;
- unsigned long long bp_offset;
+ integral_t bp_offset;
};
struct ast;