Skip to content

Commit

Permalink
Use uint64_t instead of uintptr_t
Browse files Browse the repository at this point in the history
The type `uintptr_t` is for building environment.
  • Loading branch information
tyfkda committed Nov 14, 2024
1 parent c8cd988 commit 0edd94c
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 75 deletions.
10 changes: 5 additions & 5 deletions src/as/arch/aarch64/ir_asm_aarch64.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ static void sec_align_size(SectionInfo *section, size_t align) {
data_align(section->ds, align);
}

bool calc_label_address(uintptr_t start_address, Vector *sections, Table *label_table) {
bool calc_label_address(uint64_t start_address, Vector *sections, Table *label_table) {
bool settle = true;
uintptr_t address = start_address;
uint64_t address = start_address;
for (int sec = 0; sec < sections->len; ++sec) {
SectionInfo *section = sections->data[sec];
address = ALIGN(address, section->align);
Expand Down Expand Up @@ -100,10 +100,10 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre
for (int sec = 0; sec < sections->len; ++sec) {
SectionInfo *section = sections->data[sec];
Vector *irs = section->irs;
uintptr_t start_address = irs->len > 0 ? ((IR*)irs->data[0])->address : 0;
uint64_t start_address = irs->len > 0 ? ((IR*)irs->data[0])->address : 0;
for (int i = 0, len = irs->len; i < len; ++i) {
IR *ir = irs->data[i];
uintptr_t address = ir->address;
uint64_t address = ir->address;
switch (ir->kind) {
case IR_CODE:
{
Expand Down Expand Up @@ -208,7 +208,7 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre
}
}

intptr_t offset = value.offset - VOIDP2INT(address);
int64_t offset = value.offset - address;
if (inst->op == B) {
if (offset >= (1L << 27) || offset < -(1L << 27) || (offset & 3) != 0)
error("Jump offset too far (over 32bit)");
Expand Down
20 changes: 10 additions & 10 deletions src/as/arch/riscv64/ir_asm_riscv64.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ static void sec_align_size(SectionInfo *section, size_t align) {
data_align(section->ds, align);
}

bool calc_label_address(uintptr_t start_address, Vector *sections, Table *label_table) {
bool calc_label_address(uint64_t start_address, Vector *sections, Table *label_table) {
bool settle = true;
uintptr_t address = start_address;
uint64_t address = start_address;
for (int sec = 0; sec < sections->len; ++sec) {
SectionInfo *section = sections->data[sec];
address = ALIGN(address, section->align);
Expand Down Expand Up @@ -139,10 +139,10 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre
for (int sec = 0; sec < sections->len; ++sec) {
SectionInfo *section = sections->data[sec];
Vector *irs = section->irs;
uintptr_t start_address = irs->len > 0 ? ((IR*)irs->data[0])->address : 0;
uint64_t start_address = irs->len > 0 ? ((IR*)irs->data[0])->address : 0;
for (int i = 0, len = irs->len; i < len; ++i) {
IR *ir = irs->data[i];
uintptr_t address = ir->address;
uint64_t address = ir->address;
switch (ir->kind) {
case IR_CODE:
{
Expand All @@ -153,7 +153,7 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre
if (inst->opr[1].type == DIRECT) {
Value value = calc_expr(label_table, inst->opr[1].direct.expr);
if (value.label != NULL) {
uintptr_t offset = address - start_address;
uint64_t offset = address - start_address;
UnresolvedInfo *info;
info = calloc_or_die(sizeof(*info));
info->kind = UNRES_PCREL_HI;
Expand Down Expand Up @@ -195,7 +195,7 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre
break;
case J:
if (inst->opr[0].type == DIRECT) {
int64_t target_address = 0;
uint64_t target_address = 0;
Value value = calc_expr(label_table, inst->opr[0].direct.expr);
if (value.label != NULL) {
// Put rela even if the label is defined in the same object file.
Expand All @@ -215,7 +215,7 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre

if (target_address != 0) {
Code *code = &ir->code;
int64_t offset = target_address - VOIDP2INT(address);
int64_t offset = target_address - address;
bool is_long = ir->code.flag & INST_LONG_OFFSET;
if (!is_long) {
assert(code->len == 2);
Expand Down Expand Up @@ -243,14 +243,14 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre
case BEQ: case BNE: case BLT: case BGE: case BLTU: case BGEU:
if (inst->opr[2].type == DIRECT) {
bool comp = false;
int64_t target_address = 0;
uint64_t target_address = 0;
Value value = calc_expr(label_table, inst->opr[2].direct.expr);
if (value.label != NULL) {
LabelInfo *label_info = table_get(label_table, value.label);
if (label_info != NULL)
target_address = label_info->address + value.offset;

int64_t offset = target_address - VOIDP2INT(address);
int64_t offset = target_address - address;
comp = inst->opr[1].reg.no == 0 && is_rvc_reg(inst->opr[0].reg.no) &&
(inst->op == BEQ || inst->op == BNE) &&
offset < (1 << 7) && offset >= -(1 << 7);
Expand All @@ -269,7 +269,7 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre

if (target_address != 0) {
Code *code = &ir->code;
int64_t offset = target_address - VOIDP2INT(address);
int64_t offset = target_address - address;
bool is_long = ir->code.flag & INST_LONG_OFFSET;
if (!is_long) {
assert(code->len == 2);
Expand Down
16 changes: 8 additions & 8 deletions src/as/arch/x64/ir_asm_x64.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ static void sec_align_size(SectionInfo *section, size_t align) {
data_align(section->ds, align);
}

bool calc_label_address(uintptr_t start_address, Vector *sections, Table *label_table) {
bool calc_label_address(uint64_t start_address, Vector *sections, Table *label_table) {
bool settle = true;
uintptr_t address = start_address;
uint64_t address = start_address;
for (int sec = 0; sec < sections->len; ++sec) {
SectionInfo *section = sections->data[sec];
address = ALIGN(address, section->align);
Expand Down Expand Up @@ -94,7 +94,7 @@ bool calc_label_address(uintptr_t start_address, Vector *sections, Table *label_
return settle;
}

static void put_value(unsigned char *p, intptr_t value, int size) {
static void put_value(unsigned char *p, int64_t value, int size) {
for (int i = 0; i < size; ++i) {
*p++ = value;
value >>= 8;
Expand Down Expand Up @@ -123,10 +123,10 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre
for (int sec = 0; sec < sections->len; ++sec) {
SectionInfo *section = sections->data[sec];
Vector *irs = section->irs;
uintptr_t start_address = irs->len > 0 ? ((IR*)irs->data[0])->address : 0;
uint64_t start_address = irs->len > 0 ? ((IR*)irs->data[0])->address : 0;
for (int i = 0, len = irs->len; i < len; ++i) {
IR *ir = irs->data[i];
uintptr_t address = ir->address;
uint64_t address = ir->address;
switch (ir->kind) {
case IR_CODE:
{
Expand Down Expand Up @@ -167,7 +167,7 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre
put_value(ir->code.buf + 3, value.offset, sizeof(int32_t));
#endif
} else {
intptr_t offset = value.offset - (VOIDP2INT(address) + ir->code.len);
int64_t offset = value.offset - (address + ir->code.len);
put_value(ir->code.buf + 3, offset, sizeof(int32_t));
}
}
Expand Down Expand Up @@ -198,7 +198,7 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre
}
}

intptr_t offset = value.offset - (VOIDP2INT(address) + ir->code.len);
int64_t offset = value.offset - (address + ir->code.len);
bool long_offset = ir->code.flag & INST_LONG_OFFSET;
if (!long_offset) {
if (!is_im8(offset)) {
Expand Down Expand Up @@ -228,7 +228,7 @@ bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unre
vec_push(unresolved, info);
break;
}
intptr_t offset = value.offset - (VOIDP2INT(address) + ir->code.len);
int64_t offset = value.offset - (address + ir->code.len);
put_value(ir->code.buf + 1, offset, sizeof(int32_t));
}
break;
Expand Down
14 changes: 7 additions & 7 deletions src/as/emit_elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#if XCC_TARGET_PLATFORM != XCC_PLATFORM_APPLE
#include <assert.h>
#include <stdint.h> // uintptr_t
#include <stdint.h> // uint64_t
#include <stdio.h>
#include <string.h>

Expand Down Expand Up @@ -325,7 +325,7 @@ int emit_elf_obj(const char *ofn, Vector *sections, Table *label_table, Vector *
// Construct relas.
construct_relas(unresolved, &symtab, label_table);

uintptr_t addr = sizeof(Elf64_Ehdr);
uint64_t addr = sizeof(Elf64_Ehdr);
for (int sec = 0; sec < sections->len; ++sec) {
SectionInfo *section = sections->data[sec];
size_t size;
Expand All @@ -343,9 +343,9 @@ int emit_elf_obj(const char *ofn, Vector *sections, Table *label_table, Vector *
addr += sizeof(Elf64_Rela) * section->rela_count;
}

uintptr_t symtab_ofs = addr = ALIGN(addr, ELF_MIN_ALIGN);
uint64_t symtab_ofs = addr = ALIGN(addr, ELF_MIN_ALIGN);
addr += sizeof(*symtab.buf) * symtab.count;
uintptr_t strtab_ofs = addr;
uint64_t strtab_ofs = addr;
addr += symtab.strtab.size;

// Section headers.
Expand Down Expand Up @@ -448,7 +448,7 @@ int emit_elf_obj(const char *ofn, Vector *sections, Table *label_table, Vector *
};
data_append(&section_headers, &symtabsec, sizeof(symtabsec));

uintptr_t shstrtab_ofs = addr; // ALIGN(addr, 0x10);
uint64_t shstrtab_ofs = addr; // ALIGN(addr, 0x10);
size_t shstrtab_name = strtab_add(&shstrtab, alloc_name(".shstrtab", NULL, false));
Elf64_Shdr shstrtabsec = {
.sh_name = shstrtab_name,
Expand All @@ -460,7 +460,7 @@ int emit_elf_obj(const char *ofn, Vector *sections, Table *label_table, Vector *
data_append(&section_headers, &shstrtabsec, sizeof(shstrtabsec));
addr += shstrtab.size;

uintptr_t sh_ofs = addr = ALIGN(addr, 0x10);
uint64_t sh_ofs = addr = ALIGN(addr, 0x10);
// addr += section_headers.len;

//
Expand All @@ -476,7 +476,7 @@ int emit_elf_obj(const char *ofn, Vector *sections, Table *label_table, Vector *
}
}

uintptr_t entry = 0;
uint64_t entry = 0;
int phnum = 0;
#if XCC_TARGET_ARCH == XCC_ARCH_RISCV64
const int flags = EF_RISCV_RVC | EF_RISCV_FLOAT_ABI_DOUBLE;
Expand Down
6 changes: 3 additions & 3 deletions src/as/ir_asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#pragma once

#include <stddef.h> // size_t
#include <stdint.h> // uintptr_t
#include <stdint.h> // uint64_t

#include "asm_code.h" // Code

Expand Down Expand Up @@ -78,7 +78,7 @@ typedef struct {
int align;
int section;
};
uintptr_t address;
uint64_t address;
} IR;

IR *new_ir_label(const Name *label);
Expand All @@ -89,6 +89,6 @@ IR *new_ir_zero(size_t size);
IR *new_ir_align(int align);
IR *new_ir_expr(enum IrKind kind, const Expr *expr);

bool calc_label_address(uintptr_t start_address, Vector *sections, Table *label_table);
bool calc_label_address(uint64_t start_address, Vector *sections, Table *label_table);
bool resolve_relative_address(Vector *sections, Table *label_table, Vector *unresolved);
void emit_irs(Vector *sections);
8 changes: 4 additions & 4 deletions src/as/parse_asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ typedef struct SectionInfo {
int flag;

int index;
uintptr_t start_address;
uint64_t start_address;
DataStorage *ds;
size_t align;
size_t bss_size;
uintptr_t offset; // File offset.
uint64_t offset; // File offset.

int rela_count;
void *rela_buf;
uintptr_t rela_ofs;
uint64_t rela_ofs;
} SectionInfo;

typedef struct ParseInfo {
Expand Down Expand Up @@ -189,7 +189,7 @@ enum LabelKind {
typedef struct {
SectionInfo *section;
int flag;
uintptr_t address;
uint64_t address;
enum LabelKind kind;
int size;
int align;
Expand Down
6 changes: 3 additions & 3 deletions src/cc/arch/aarch64/ir_aarch64.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ const RegAllocSettings kArchRegAllocSettings = {

//

extern inline bool is_im9(intptr_t x) {
extern inline bool is_im9(int64_t x) {
return x <= ((1L << 8) - 1) && x >= -(1L << 8);
}

extern inline bool is_im13(intptr_t x) {
extern inline bool is_im13(int64_t x) {
return x <= ((1L << 12) - 1) && x >= -(1L << 12);
}

extern inline bool is_im48(intptr_t x) {
extern inline bool is_im48(int64_t x) {
return x <= ((1L << 47) - 1) && x >= -(1L << 47);
}

Expand Down
4 changes: 2 additions & 2 deletions src/cpp/pp_parser.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#pragma once

#include <stdint.h> // intptr_t
#include <stdint.h> // int64_t
#include <stdio.h> // FILE

#include "lexer.h" // TokenKind, Token

typedef struct Macro Macro;
typedef struct Vector Vector;

typedef intptr_t PpResult;
typedef int64_t PpResult;

typedef struct {
const char *filename;
Expand Down
Loading

0 comments on commit 0edd94c

Please sign in to comment.