Skip to content

Commit 3493fd1

Browse files
committed
Add const qualifiers to immutable local variables
This applies const qualifier to local variables that are initialized once and never modified throughout their scope, improving code clarity by explicitly marking immutable values.
1 parent 5db0bd9 commit 3493fd1

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

src/arm-codegen.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ void emit(int code)
188188
void emit_ph2_ir(ph2_ir_t *ph2_ir)
189189
{
190190
func_t *func;
191-
int rd = ph2_ir->dest;
192-
int rn = ph2_ir->src0;
193-
int rm = ph2_ir->src1;
191+
const int rd = ph2_ir->dest;
192+
const int rn = ph2_ir->src0;
193+
const int rm = ph2_ir->src1;
194194
int ofs;
195195

196196
/* Prepare this variable to reuse code for:

src/globals.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ void *arena_alloc(arena_t *arena, int size)
173173
}
174174

175175
/* Align to sizeof(void*) bytes for host compatibility */
176-
int alignment = sizeof(void *);
176+
const int alignment = sizeof(void *);
177177
size = (size + alignment - 1) & ~(alignment - 1);
178178

179179
if (!arena->head || arena->head->offset + size > arena->head->capacity) {
180180
/* Need a new block: choose capacity = max(DEFAULT_ARENA_SIZE,
181181
* arena->block_size, size) */
182-
int base =
182+
const int base =
183183
(arena->block_size > DEFAULT_ARENA_SIZE ? arena->block_size
184184
: DEFAULT_ARENA_SIZE);
185-
int new_capacity = (size > base ? size : base);
185+
const int new_capacity = (size > base ? size : base);
186186
arena_block_t *new_block = arena_block_create(new_capacity);
187187
new_block->next = arena->head;
188188
arena->head = new_block;
@@ -282,7 +282,7 @@ void *arena_realloc(arena_t *arena, char *oldptr, int oldsz, int newsz)
282282
*/
283283
char *arena_strdup(arena_t *arena, char *str)
284284
{
285-
int n = strlen(str);
285+
const int n = strlen(str);
286286
char *dup = arena_alloc(arena, n + 1);
287287
memcpy(dup, str, n);
288288
dup[n] = '\0';
@@ -368,14 +368,14 @@ void arena_free(arena_t *arena)
368368
*/
369369
int hashmap_hash_index(int size, char *key)
370370
{
371-
int hash = 0x811c9dc5, mask;
371+
int hash = 0x811c9dc5;
372372

373373
for (; *key; key++) {
374374
hash ^= *key;
375375
hash *= 0x01000193;
376376
}
377377

378-
mask = hash >> 31;
378+
const int mask = hash >> 31;
379379
return ((hash ^ mask) - mask) & (size - 1);
380380
}
381381

@@ -431,7 +431,7 @@ hashmap_node_t *hashmap_node_new(char *key, void *val)
431431
if (!key)
432432
return NULL;
433433

434-
int len = strlen(key);
434+
const int len = strlen(key);
435435
hashmap_node_t *node = arena_alloc(HASHMAP_ARENA, sizeof(hashmap_node_t));
436436

437437

src/opt-sccp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ bool simple_sccp(func_t *func)
122122
if (insn->rs1 && insn->rs1->is_const && insn->rs2 &&
123123
insn->rs2->is_const && !insn->rd->is_global) {
124124
int result = 0;
125-
int l = insn->rs1->init_val, r = insn->rs2->init_val;
125+
const int l = insn->rs1->init_val, r = insn->rs2->init_val;
126126

127127
/* Compute result based on operation type */
128128
switch (insn->opcode) {

src/parser.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void read_expr(block_t *parent, basic_block_t **bb);
132132

133133
int write_symbol(const char *data)
134134
{
135-
int start_len = elf_data->size;
135+
const int start_len = elf_data->size;
136136
elf_write_str(elf_data, data);
137137
elf_write_byte(elf_data, 0);
138138
return start_len;
@@ -1367,7 +1367,7 @@ void read_literal_param(block_t *parent, basic_block_t *bb)
13671367
combined_len += literal_len;
13681368
}
13691369

1370-
int index = write_symbol(combined);
1370+
const int index = write_symbol(combined);
13711371

13721372
var_t *vd = require_typed_ptr_var(parent, TY_char, true);
13731373
gen_name_to(vd->var_name);

src/reg-alloc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,9 @@ void dump_ph2_ir(void)
887887
for (int i = 0; i < ph2_ir_idx; i++) {
888888
ph2_ir_t *ph2_ir = PH2_IR_FLATTEN[i];
889889

890-
int rd = ph2_ir->dest + 48;
891-
int rs1 = ph2_ir->src0 + 48;
892-
int rs2 = ph2_ir->src1 + 48;
890+
const int rd = ph2_ir->dest + 48;
891+
const int rs1 = ph2_ir->src0 + 48;
892+
const int rs2 = ph2_ir->src1 + 48;
893893

894894
switch (ph2_ir->op) {
895895
case OP_define:

0 commit comments

Comments
 (0)