Skip to content

Commit 54fe154

Browse files
committed
ir: sort insertions, add file for register-allocation
1 parent baa05e6 commit 54fe154

File tree

7 files changed

+54
-7
lines changed

7 files changed

+54
-7
lines changed

ir/context.c2

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,12 @@ fn void Context.finalizeFunction(Context* c, SymbolId id) {
741741

742742
t.removeNone(fi2);
743743
if (print) c.print_function(symbol, fi2, "after remove none");
744+
745+
t.allocateRegisters(fi2);
746+
747+
// leave SSA (eliminate Phis)
748+
// generate Instruction
749+
// ..
744750
}
745751

746752
fn bool empty_block(const Block* b, const InstrList* instructions) {

ir/instr_inserter.c2

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@ public fn void InstrInserter.free(InstrInserter* ii) {
6363
public fn void InstrInserter.add(InstrInserter* ii, u32 location, u32 instr_idx) {
6464
if (ii.count == ii.capacity) ii.resize(ii.capacity * 2);
6565

66-
InstrInsertion* ins = &ii.ins[ii.count++];
67-
ins.location = location;
68-
ins.instr_idx = instr_idx;
66+
u32 idx = ii.count;
67+
while (idx > 0) {
68+
if (ii.ins[idx-1].location <= location) break;
69+
ii.ins[idx] = ii.ins[idx-1];
70+
idx--;
71+
}
72+
ii.ins[idx].location = location;
73+
ii.ins[idx].instr_idx = instr_idx;
74+
ii.count++;
6975
}
7076

7177
public fn void InstrInserter.start(InstrInserter* ii) {
@@ -87,7 +93,7 @@ public fn bool InstrInserter.needsFixup(const InstrInserter* ii) {
8793
return ii.count != 0;
8894
}
8995

90-
fn void InstrInserter.dump(const InstrInserter* ii) @(unused) {
96+
public fn void InstrInserter.dump(const InstrInserter* ii) @(unused) {
9197
printf("Insertions:\n");
9298
for (u32 i=0; i<ii.count; i++) {
9399
const InstrInsertion* ins = &ii.ins[i];

ir/print.c2

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const char* col_Symbol = color.Bblue;
3131
const char* col_Attr = color.Bmagenta;
3232
const char* col_Unknown = color.Yellow;
3333
const char* col_Value = color.Magenta;
34-
const char* col_Data = color.Bcyan;
34+
const char* col_Register = color.Cyan;
35+
const char* col_Data = color.Bgreen;
3536
const char* col_Slot = color.Bcyan;
3637
const char* col_Temp = color.Bgreen;
3738
const char* col_Param = color.Cyan;
@@ -512,9 +513,12 @@ fn void PrintHelper.printRef(PrintHelper* ph, Ref r, bool print_type) {
512513
out.color(col_Block);
513514
out.add(ph.c.pool.idx2str(r.value));
514515
break;
516+
case Register:
517+
out.color(col_Register);
518+
out.print("R%d", r.value);
519+
break;
515520
}
516521
out.color(color.Normal);
517-
518522
}
519523

520524

ir/ref.c2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public type RefKind enum u8 {
3030
Float, // constant_idx
3131
Double, // constant_idx
3232
Text, // idx into string pool, for comment
33+
Register, // nr (0,1,..)
3334
}
3435

3536
fn const char* RefKind.str(RefKind k) {
@@ -46,6 +47,7 @@ fn const char* RefKind.str(RefKind k) {
4647
case Float: return "float";
4748
case Double: return "double";
4849
case Text: return "text";
50+
case Register: return "register";
4951
}
5052
return nil;
5153
}

ir/register_alloc.c2

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Copyright 2022-2025 Bas van den Berg
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
module ir;
17+
18+
//import stdlib;
19+
//import string;
20+
//import stdio local;
21+
22+
fn void Tools.allocateRegisters(Tools* t, FunctionInfo* fi) {
23+
// Need to know:
24+
// how many registers available
25+
// locations of params (%1, %2, etc)
26+
// or just assign virtual regs: R0, R1?
27+
// also need to know return register?
28+
}
29+

ir_examples/bitfield_init.c2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const Design[] Designs = {
1111
{ .stackable=1, .rarity=5, .bla=12345 },
1212
{ .rarity=5, .stackable=1, .bla=12345 },
1313
{ .bla=12345, .rarity=5, .stackable=1 },
14-
{ 12345, 1, 6, 5, }, // error
1514
}
1615

1716
public fn i32 main() {

recipe.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ executable c2c
285285
ir/instr.c2
286286
ir/instr_inserter.c2
287287
ir/instr_kind.c2
288+
ir/register_alloc.c2
288289
ir/slot_collector.c2
289290
ir/ssa.c2
290291
ir/symbol_list.c2

0 commit comments

Comments
 (0)