Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 727a846

Browse files
committed
bug fixing
1 parent 373d2de commit 727a846

35 files changed

+356
-264
lines changed

basecode/compiler/byte_code_emitter.cpp

Lines changed: 109 additions & 49 deletions
Large diffs are not rendered by default.

basecode/compiler/byte_code_emitter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,23 @@ namespace basecode::compiler {
187187
const identifier_by_section_t& vars,
188188
identifier_list_t& locals);
189189

190+
uint8_t allocate_temp() {
191+
return ++_temp;
192+
}
193+
194+
void free_temp() {
195+
if (_temp > 0)
196+
--_temp;
197+
}
198+
199+
void reset_temp() {
200+
_temp = 0;
201+
}
202+
190203
bool is_temp_local(const vm::instruction_operand_t& operand);
191204

192205
private:
206+
uint8_t _temp = 0;
193207
compiler::session& _session;
194208
basic_block_stack_t _block_stack {};
195209
flow_control_stack_t _control_flow_stack {};

basecode/vm/terp.cpp

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -946,14 +946,26 @@ namespace basecode::vm {
946946
register_type_t::integer :
947947
register_type_t::floating_point;
948948

949-
auto range = static_cast<uint16_t>(operand.value.u);
950-
auto start = (range >> 8) & 0xff;
951-
auto end = range & 0xff;
952-
auto delta = end < start ? -1 : 1;
953-
auto count = std::abs(end - start);
954-
955-
for (uint8_t reg = static_cast<uint8_t>(start); count >= 0; reg += delta, --count) {
956-
auto reg_index = register_index(static_cast<registers_t>(reg), type);
949+
if (operand.is_range()) {
950+
auto range = static_cast<uint16_t>(operand.value.u);
951+
auto start = (range >> 8) & 0xff;
952+
auto end = range & 0xff;
953+
auto delta = end < start ? -1 : 1;
954+
auto count = std::abs(end - start);
955+
956+
for (auto reg = static_cast<uint8_t>(start);
957+
count >= 0;
958+
reg += delta, --count) {
959+
auto reg_index = register_index(
960+
static_cast<registers_t>(reg),
961+
type);
962+
auto alias = _registers.r[reg_index];
963+
push(alias.qw);
964+
}
965+
} else if (operand.is_reg()) {
966+
auto reg_index = register_index(
967+
static_cast<registers_t>(operand.value.r),
968+
type);
957969
auto alias = _registers.r[reg_index];
958970
push(alias.qw);
959971
}
@@ -991,14 +1003,25 @@ namespace basecode::vm {
9911003
register_type_t::integer :
9921004
register_type_t::floating_point;
9931005

994-
auto range = static_cast<uint16_t>(operand.value.u);
995-
auto start = (range >> 8) & 0xff;
996-
auto end = range & 0xff;
997-
auto delta = end < start ? -1 : 1;
998-
auto count = std::abs(end - start);
999-
1000-
for (uint8_t reg = static_cast<uint8_t>(start); count >= 0; reg += delta, --count) {
1001-
auto reg_index = register_index(static_cast<registers_t>(reg), type);
1006+
if (operand.is_range()) {
1007+
auto range = static_cast<uint16_t>(operand.value.u);
1008+
auto start = (range >> 8) & 0xff;
1009+
auto end = range & 0xff;
1010+
auto delta = end < start ? -1 : 1;
1011+
auto count = std::abs(end - start);
1012+
1013+
for (auto reg = static_cast<uint8_t>(start);
1014+
count >= 0;
1015+
reg += delta, --count) {
1016+
auto reg_index = register_index(
1017+
static_cast<registers_t>(reg),
1018+
type);
1019+
_registers.r[reg_index].qw = pop();
1020+
}
1021+
} else if (operand.is_reg()) {
1022+
auto reg_index = register_index(
1023+
static_cast<registers_t>(operand.value.r),
1024+
type);
10021025
_registers.r[reg_index].qw = pop();
10031026
}
10041027
}

modules/core/module.bc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ intrinsics :: module("intrinsics.bc");
1515

1616
types :: module("types.bc");
1717
maths :: module("math.bc");
18-
assert :: module("assert.bc");
18+
asserts :: module("assert.bc");
1919
compiler :: module("compiler.bc");
2020

2121
import public from maths;
2222
import public from types;
23-
import public from assert;
23+
import public from asserts;
2424
import public from compiler;
2525
import public from intrinsics;

tests/asserts.bc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ MODE :: 2;
77
//#assert 2 == 1;
88

99
#if MODE == 1 {
10-
print("mode 1\n");
10+
core::print("mode 1\n");
1111
} #elif MODE == 2 {
12-
print("mode 2\n");
12+
core::print("mode 2\n");
1313
} #else {
14-
print("unknown mode\n");
14+
core::print("unknown mode\n");
1515
};
1616

1717
factorial :: proc(n: u64 := 12): u64 {
@@ -32,10 +32,10 @@ fib :: proc(n: u64 := 10): u64 {
3232

3333
#run {
3434
result := fib(20);
35-
print("fib := %llu\n", result);
36-
assert(result == 1, "fib expected 1");
35+
core::print("fib := %llu\n", result);
36+
core::assert(result == 1, "fib expected 1");
3737

3838
result := factorial();
39-
print("factorial := %llu\n", result);
40-
assert(result == 479001600, "factorial expected 479001600");
39+
core::print("factorial := %llu\n", result);
40+
core::assert(result == 479001600, "factorial expected 479001600");
4141
};

tests/automatic-pointer-deref.bc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ b: two;
2121
b.next := address_of(c);
2222

2323
a_ptr := address_of(a);
24-
assign_string(address_of(a_ptr.next.next.name), "hello world!", 13);
24+
core::assign_string(address_of(a_ptr.next.next.name), "hello world!", 13);
2525

2626
#run {
27-
print("a.next.next.name.data = %s\n", a_ptr.next.next.name.data);
27+
core::print("a.next.next.name.data = %s\n", a_ptr.next.next.name.data);
2828
};
2929

3030
vector3 :: struct {
@@ -63,7 +63,7 @@ barzar :: struct {
6363
bar.e.pos.y := 4.1;
6464
bar.e.pos.z := -33.2;
6565

66-
assign_string(address_of(bar.c.name), "magicks class", 13);
67-
print("bar.c.name.data = %s\n", bar.c.name.data);
66+
core::assign_string(address_of(bar.c.name), "magicks class", 13);
67+
core::print("bar.c.name.data = %s\n", bar.c.name.data);
6868
};
6969
};

tests/character-literals.bc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ a_type := type_of(a);
88
b_type := type_of(b);
99

1010
temp: string;
11-
assign_string(address_of(temp), "hello world!", 12);
11+
core::assign_string(address_of(temp), "hello world!", 12);
1212

1313
#run {
14-
print("a := %c\n", a);
15-
print("b := %c\n", b);
16-
print("%c\n", 'Z');
14+
core::print("a := %c\n", a);
15+
core::print("b := %c\n", b);
16+
core::print("%c\n", 'Z');
1717

18-
print("a_type.name := %s\n", a_type.name.data);
19-
print("b_type.name := %s\n", b_type.name.data);
18+
core::print("a_type.name := %s\n", a_type.name.data);
19+
core::print("b_type.name := %s\n", b_type.name.data);
2020

21-
print("%s\n", temp.data);
21+
core::print("%s\n", temp.data);
2222
};

tests/compiler-variables.bc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ foo :: proc(a, b, c: f32): f32 {
6262
core::print("player.dir.y := %d\n", player.dir.y);
6363
core::print("player.dir.z := %d\n", player.dir.z);
6464

65+
// player.cls.name := text;
6566
core::print("player.cls.name.length := %d\n", player.cls.name.length);
6667
core::print("player.cls.name.capacity := %d\n", player.cls.name.capacity);
6768

tests/composite-operators.bc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ utf32_string :: struct {
1515
make_string :: proc(): string {
1616
s: string;
1717
s.length := 0;
18-
s.capacity := next_power_of_two(32);
18+
s.capacity := core::next_power_of_two(32);
1919
s.data := alloc(s.capacity);
2020
return s;
2121
};
@@ -25,7 +25,7 @@ resize_string :: proc(s: ^string, target_length: u32) {
2525
return;
2626
};
2727

28-
s.capacity := next_power_of_two(target_length);
28+
s.capacity := core::next_power_of_two(target_length);
2929

3030
buffer: ^u8 := alloc(s.capacity);
3131
fill(buffer, 0, s.capacity);
@@ -52,7 +52,7 @@ string_append :: proc(lhs: ^string, rhs: ^string) {
5252
string_assignment_1 :: proc(lhs: ^utf16_string, rhs: string) {
5353
with lhs {
5454
.length := rhs.length;
55-
.capacity := next_power_of_two(size_of(u16) * rhs.length);
55+
.capacity := core::next_power_of_two(size_of(u16) * rhs.length);
5656
.data := alloc(.capacity);
5757
fill(.data, 0, .capacity);
5858
};
@@ -62,7 +62,7 @@ string_assignment_1 :: proc(lhs: ^utf16_string, rhs: string) {
6262
string_assignment_2 :: proc(lhs: ^utf32_string, rhs: string) {
6363
with lhs {
6464
.length := rhs.length;
65-
.capacity := next_power_of_two(size_of(u32) * rhs.length);
65+
.capacity := core::next_power_of_two(size_of(u32) * rhs.length);
6666
.data := alloc(.capacity);
6767
fill(.data, 0, .capacity);
6868
};

tests/constant-folding.bc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ core :: module("../modules/core");
1616

1717
inverted: u8 := ~%1111_0000;
1818

19-
print("inverted := %d\n", inverted);
19+
core::print("inverted := %d\n", inverted);
2020

2121
// the code dom will retain the full expression
2222
// for this variable declaration's initializer

0 commit comments

Comments
 (0)