Skip to content

Commit f4e142b

Browse files
committed
Codegen: remove global object counter
The object counter was a relic from earlier versions of the compiler. In the current implementation (that is, the latest-released version), the counter is completely unnecessary. This change makes an object's (or pointer's) address completely determined by properties of the Object entity, without reference to any mutable state at the time of code generation (e.g., the number of objects we've already instantiated, etc) This, incidentally, is also a bug fix. - File A instantiates object 1 - File A dynamically includes file B - File B instantiates object 2 Since file B is dynamically included, the two units are compiled separately. When File B is compiled, its object counter never goes higher than 1 -- it only ever instantiates a single object. When File A is compiled, by the time we reach object 2, our object counter has already been incremented once by an earlier object instantiation. Therefore the two counters disagree with each other, and when File A later references object 2, it guesses the *wrong address*. Removing any dependency on mutable codegen state for address determination means that the two units, despite being independently compiled, will always agree on an object's address.
1 parent 4448725 commit f4e142b

11 files changed

Lines changed: 44 additions & 27 deletions

File tree

src/bpp_include/bpp_program.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,6 @@ uint64_t bpp_program::get_typeof_counter() const {
190190
return typeof_counter;
191191
}
192192

193-
void bpp_program::increment_object_counter() {
194-
object_counter++;
195-
}
196-
197-
uint64_t bpp_program::get_object_counter() const {
198-
return object_counter;
199-
}
200-
201193
void bpp_program::set_target_bash_version(BashVersion target_bash_version) {
202194
this->target_bash_version = target_bash_version;
203195
}

src/bpp_include/bpp_program.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class bpp_program : public bpp_code_entity, public std::enable_shared_from_this<
3232
uint64_t function_counter = 0;
3333
uint64_t dynamic_cast_counter = 0;
3434
uint64_t typeof_counter = 0;
35-
uint64_t object_counter = 0;
3635

3736
BashVersion target_bash_version = {5, 2};
3837

@@ -97,9 +96,6 @@ class bpp_program : public bpp_code_entity, public std::enable_shared_from_this<
9796
void increment_typeof_counter();
9897
uint64_t get_typeof_counter() const;
9998

100-
void increment_object_counter();
101-
uint64_t get_object_counter() const;
102-
10399
void set_target_bash_version(BashVersion target_bash_version);
104100
BashVersion get_target_bash_version() const;
105101

src/listener/handlers/ObjectInstantiation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ void BashppListener::enterObjectInstantiation(std::shared_ptr<AST::ObjectInstant
6565
// Note:
6666
// Here, we're incrementing an internal object counter and determining the location of the object in memory AT COMPILE-TIME
6767
// Should this be determined at run-time instead?
68-
new_object->set_address("bpp__" + std::to_string(program->get_object_counter()) + "__" + object_class->get_name() + "__" + new_object->get_name());
69-
program->increment_object_counter();
68+
new_object->set_address("bpp__" + object_class->get_name() + "__" + new_object->get_name());
7069

7170
// Verify the object's name is valid
7271
if (!bpp::is_valid_identifier(new_object->get_name())) {

src/listener/handlers/PointerDeclaration.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ void BashppListener::enterPointerDeclaration(std::shared_ptr<AST::PointerDeclara
6262
object_type.getCharPositionInLine()
6363
);
6464

65-
new_object->set_address("bpp____ptr__" + std::to_string(program->get_object_counter()) + "__" + new_object->get_class()->get_name() + "__" + new_object->get_name());
66-
program->increment_object_counter();
65+
new_object->set_address("bpp____ptr__" + new_object->get_class()->get_name() + "__" + new_object->get_name());
6766

6867
// Verify the object's name is valid
6968
if (!bpp::is_valid_identifier(new_object->get_name())) {

test-suite/TestRunner.bpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@include_once "TestStats.bpp"
2+
@include_once "Test.bpp"
23

34
@class TestRunner {
45
@public @TestStats* stats=@new TestStats
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
default value
2+
default value
3+
new value 1
4+
new value 2

test-suite/tests/expected/pointer-dereferencing

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ hello
1313
bpp__MyClass__[0-9]+
1414
default value
1515
default value
16-
bpp__[0-9]+__ContainingClass__containingObject
16+
bpp__ContainingClass__containingObject
1717
ContainingClass Instance
1818
bpp__MyClass__[0-9]+
1919
MyClass Instance
@@ -25,13 +25,13 @@ Displaying default value
2525
Displaying default value
2626
.+line [0-9]+: bpp__MyClass__[0-9]+: command not found
2727
MyClass Instance
28-
.+line [0-9]+: bpp__[0-9]+__ContainingClass__containingObject: command not found
28+
.+line [0-9]+: bpp__ContainingClass__containingObject: command not found
2929
ContainingClass Instance
3030
hello again
3131
hello again
3232
hello
3333
hello
34-
bpp__[0-9]+__ContainingClass__containingObject
34+
bpp__ContainingClass__containingObject
3535
echo hello again
3636
echo hello again
3737
new value

test-suite/tests/expected/super

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ Base class
33
Non-virtual method in base class
44
default value
55
default value
6-
bpp__0__Derived__d
7-
bpp__0__Derived__d
6+
bpp__Derived__d
7+
bpp__Derived__d
88
__this
99
__this
1010
Example Instance
1111
Derived Instance
12-
bpp__Example__something bpp__0__Derived__d
13-
bpp__Derived__something bpp__0__Derived__d
14-
bpp__Example__nonVirtual bpp__0__Derived__d
15-
bpp__Example__nonVirtual bpp__0__Derived__d
16-
bpp__0__Derived__d__member
17-
bpp__0__Derived__d__member
12+
bpp__Example__something bpp__Derived__d
13+
bpp__Derived__something bpp__Derived__d
14+
bpp__Example__nonVirtual bpp__Derived__d
15+
bpp__Example__nonVirtual bpp__Derived__d
16+
bpp__Derived__d__member
17+
bpp__Derived__d__member
1818
Example Instance
1919
Derived Instance
2020
protected value
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@class Object {
2+
@public member="default value"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@include_once "dynamic-includes-helper1.bpp"
2+
3+
@Object obj2

0 commit comments

Comments
 (0)