Skip to content

Commit

Permalink
Removal label table global var (#14)
Browse files Browse the repository at this point in the history
* Made variables private

This change made the previosuly global `label_table` and the other
`lable_table_size` variables static and added "getter" (Well, C do-
es not have getters) functions that *gets* the values of the label
table values and it's size.

So, having the `label_table` and `label_table_size` variables hid-
den away prevents mistakes by both the caller and internal functions
that may accidentally: 1. redefine or attempt to clash with the
variable names, and 2. unintentionally modify values of the label
table's values. (So, basically, you can't be stupid enough to even
attempt to modify a function's value)

* Added declaration to headaer

This change added in the function defined and added in commit nu-
mber 19dbeef and chucked it into
the `label.c`'s corrisponding header file.

* Changed source of labels of `label_table` users

Migrated the "users" (The places where the `label_table` and the
corrisponding `label_table_size` gets called and used) to the more
secure and safe `label_get_size()` and `label_get_table()` functions
as added in commit number 19dbeef

* Removed `inline` keyword

Turns out the inline keyword requires an alternate implementation
of the same function, therfore rendering this function undefined
during link time. However when the compiler deems needed, the com-
iler itself can *automatically inline* the as it deem fit during
compile time without us explicitly specifying it in the function
prototype. - I mean you learn something every day, huh?
  • Loading branch information
cheng-alvin authored Dec 19, 2024
1 parent 309ddd5 commit c23a5ce
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- ![](https://github.com/cheng-alvin/jas/blob/main/logo.png) -->
<!-- ![Jas logo](https://github.com/cheng-alvin/jas/blob/main/logo.png) -->
![Jas logo - Christmas edition](https://github.com/user-attachments/assets/804d9479-77d2-4342-8f54-500a5f9d09fa)
<p align="center">

Expand Down
11 changes: 8 additions & 3 deletions libjas/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ buffer_t codegen(enum modes mode, instruction_t *instr_arr, size_t arr_size, enu
free(section_ent.data);
free(pad);

//!! DO NOT try and modify these - Please dont spoof me through pointer arithmetic as well :)
const size_t label_table_size = label_get_size();
const label_t *label_table = label_get_table();

for (size_t i = 0; i < label_table_size; i++) {
// Refer to https://www.sco.com/developers/devspecs/gabi41.pdf - Figure 4-16
uint8_t binding = 0;
Expand Down Expand Up @@ -156,9 +160,10 @@ static buffer_t assemble(enum modes mode, instruction_t *instr_arr, size_t arr_s
buf_write(&buf, data->data, data->len);
}
if (is_pre && IS_LABEL) {
for (size_t j = 0; j < label_table_size; j++) {
if (strcmp(label_table[j].name, instr_arr[i].operands[0].data) == 0) {
label_table[j].address = buf.len;
for (size_t j = 0; j < label_get_size; j++) {
label_t *tab = label_get_table();
if (strcmp(tab[j].name, instr_arr[i].operands[0].data) == 0) {
tab[j].address = buf.len;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion libjas/exe.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ buffer_t exe_sect_header(uint32_t str_offset, uint32_t type, uint64_t flags, uin

if (type == 0x02) {
buf_write(&ret, &(uint32_t){2}, 4); // Section link
buf_write(&ret, &(uint32_t){label_table_size + 1}, 4); // Section info
buf_write(&ret, &(uint32_t){label_get_size() + 1}, 4); // Section info
buf_write(&ret, QWORD_PAD, 8); // Section address alignment
buf_write(&ret, &(uint64_t){0x18}, 8); // Section entry size

Expand Down
4 changes: 2 additions & 2 deletions libjas/include/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ typedef struct {
size_t instr_index; /* Index to slip the label in the instruction array */
} label_t;

extern label_t *label_table;
extern size_t label_table_size;
size_t label_get_size();
label_t *label_get_table();

/**
* Function for destroying the label table, freeing the memory
Expand Down
17 changes: 10 additions & 7 deletions libjas/label.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
#include <stdlib.h>
#include <string.h>

label_t *label_table = NULL;
size_t label_table_size = 0;
static label_t *label_table = NULL;
static size_t label_table_size = 0;

void label_create(char *name, bool exported, bool ext, size_t address, size_t instr_index) {
if (label_lookup(name) != NULL) {
Expand All @@ -39,11 +39,11 @@ void label_create(char *name, bool exported, bool ext, size_t address, size_t in
}

label_t label = {
.name = name,
.exported = exported,
.ext = ext, .address = address,
.instr_index = instr_index
};
.name = name,
.exported = exported,
.ext = ext,
.address = address,
.instr_index = instr_index};

label_table_size++;
label_table = (label_t *)realloc(label_table, label_table_size * sizeof(label_t));
Expand All @@ -64,3 +64,6 @@ label_t *label_lookup(char *name) {

return NULL;
}

size_t label_get_size() { return label_table_size; }
label_t *label_get_table() { return label_table; }

0 comments on commit c23a5ce

Please sign in to comment.