We planned to upgrade GitLab and Mattermost to the latest version this Friday morning (early). You may experience some downtime!

Verified Commit ceee44ba authored by Camil Staps's avatar Camil Staps 🚀

In bytecode, first list global labels, then local labels, so that the global...

In bytecode, first list global labels, then local labels, so that the global labels are ordered by name without gaps
parent bce9a745
Pipeline #16813 passed with stages
in 17 minutes and 40 seconds
......@@ -4405,21 +4405,25 @@ void code_dummy(void) {
}
#endif
static int count_and_renumber_labels(struct label_node *node, int start) {
static void count_and_renumber_labels(struct label_node *node, int *start, int *end) {
if (node->label_node_left != NULL)
start = count_and_renumber_labels(node->label_node_left, start);
count_and_renumber_labels(node->label_node_left, start, end);
struct label *label = node->label_node_label_p;
label->label_id = start++;
if (label->label_offset < 0 || label->label_module_n < 0) {
label->label_id=*start;
*start=*start+1;
} else {
label->label_id=*end;
*end=*end-1;
}
if (label->label_offset < 0) {
global_label_count++;
global_label_string_count+=strlen(label->label_name);
}
if (node->label_node_right != NULL)
start = count_and_renumber_labels(node->label_node_right, start);
return start;
count_and_renumber_labels(node->label_node_right, start, end);
}
#ifndef LINK_CLEAN_RUNTIME
......@@ -4469,22 +4473,38 @@ static void print_global_labels(struct label_node *node, FILE *program_file) {
print_global_labels(node->label_node_left, program_file);
struct label *label = node->label_node_label_p;
fwrite(&label->label_offset, sizeof(label->label_offset), 1, program_file);
if (label->label_module_n == - 1 || label->label_offset < 0)
if (label->label_module_n == - 1 || label->label_offset < 0) {
fwrite(&label->label_offset, sizeof(label->label_offset), 1, program_file);
fprintf(program_file, "%s", label->label_name);
fputc('\0', program_file);
fputc('\0', program_file);
}
if (node->label_node_right != NULL)
print_global_labels(node->label_node_right, program_file);
}
static void print_local_labels(struct label_node *node, FILE *program_file) {
if (node->label_node_right != NULL)
print_local_labels(node->label_node_right, program_file);
struct label *label = node->label_node_label_p;
if (!(label->label_module_n == - 1 || label->label_offset < 0)) {
fwrite(&label->label_offset, sizeof(label->label_offset), 1, program_file);
fputc('\0', program_file);
}
if (node->label_node_left != NULL)
print_local_labels(node->label_node_left, program_file);
}
void write_program(FILE *program_file) {
fwrite(&pgrm.code_size, sizeof(pgrm.code_size), 1, program_file);
fwrite(&pgrm.words_in_strings, sizeof(pgrm.words_in_strings), 1, program_file);
fwrite(&pgrm.strings_size, sizeof(pgrm.strings_size), 1, program_file);
fwrite(&pgrm.data_size, sizeof(pgrm.data_size), 1, program_file);
count_and_renumber_labels(labels, 0);
int start=0, end=label_id-1;
count_and_renumber_labels(labels, &start, &end);
fwrite(&label_id, sizeof(label_id), 1, program_file);
fwrite(&global_label_string_count, sizeof(global_label_string_count), 1, program_file);
......@@ -4503,6 +4523,7 @@ void write_program(FILE *program_file) {
print_data(pgrm.data_size,pgrm.data,program_file);
print_global_labels(labels, program_file);
print_local_labels(labels, program_file);
print_relocations(pgrm.code_reloc_size,pgrm.code_relocations,program_file);
print_relocations(pgrm.data_reloc_size,pgrm.data_relocations,program_file);
......@@ -4519,12 +4540,13 @@ static char *print_global_labels_to_string(struct label_node *node, char *ptr) {
ptr=print_global_labels_to_string(node->label_node_left, ptr);
struct label *label = node->label_node_label_p;
memcpy(ptr, &label->label_offset, sizeof(label->label_offset));
ptr+=sizeof(label->label_offset);
if (label->label_module_n == - 1 || label->label_offset < 0)
if (label->label_module_n == - 1 || label->label_offset < 0) {
memcpy(ptr, &label->label_offset, sizeof(label->label_offset));
ptr+=sizeof(label->label_offset);
for (char *name_ptr=label->label_name; *name_ptr; name_ptr++)
*ptr++=*name_ptr;
*ptr++='\0';
*ptr++='\0';
}
if (node->label_node_right != NULL)
ptr=print_global_labels_to_string(node->label_node_right, ptr);
......@@ -4532,8 +4554,26 @@ static char *print_global_labels_to_string(struct label_node *node, char *ptr) {
return ptr;
}
static char *print_local_labels_to_string(struct label_node *node, char *ptr) {
if (node->label_node_right != NULL)
ptr=print_local_labels_to_string(node->label_node_right, ptr);
struct label *label = node->label_node_label_p;
if (!(label->label_module_n == - 1 || label->label_offset < 0)) {
memcpy(ptr, &label->label_offset, sizeof(label->label_offset));
ptr+=sizeof(label->label_offset);
*ptr++='\0';
}
if (node->label_node_left != NULL)
ptr=print_local_labels_to_string(node->label_node_left, ptr);
return ptr;
}
char *write_program_to_string(uint32_t *bytes_needed) {
count_and_renumber_labels(labels, 0);
int start=0, end=label_id-1;
count_and_renumber_labels(labels, &start, &end);
*bytes_needed =
9*sizeof(uint32_t) +
......@@ -4572,6 +4612,7 @@ char *write_program_to_string(uint32_t *bytes_needed) {
}
ptr=print_global_labels_to_string(labels, ptr);
ptr=print_local_labels_to_string(labels, ptr);
for (int i=0; i<pgrm.code_reloc_size; i++) {
memcpy(ptr, &pgrm.code_relocations[i].relocation_offset, sizeof(uint32_t));
......
......@@ -51,6 +51,7 @@ static struct code_index *code_indices;
static uint64_t *data;
static uint32_t n_labels;
static uint32_t n_global_labels;
static struct s_label *labels;
static uint32_t code_reloc_size;
......@@ -416,7 +417,7 @@ static void activate_label(struct s_label *label) {
static struct s_label *find_label_by_name(const char *name) {
int l=0;
int r=n_labels-1;
int r=n_global_labels-1;
while (l<=r) {
int i=(l+r)/2;
struct s_label *label=&labels[i];
......@@ -501,6 +502,8 @@ void prepare_strip_bytecode(uint32_t *bytecode, int activate_start_label) {
for (int i=0; i<n_labels; i++) {
labels[i].offset=*(int32_t*)labels_in_bytecode;
labels[i].name=&labels_in_bytecode[4];
if (labels[i].name[0]!='\0')
n_global_labels=i;
labels[i].bcgen_label=NULL;
labels[i].label_type=DLT_NORMAL;
if (!(labels[i].offset & 1)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment