How is variable reassignment dealt with in code generation in compilers?

I’m trying to build my own compiler with C++ for my own basic programming language. In the beginning, I was following a series of YouTube videos from the channel Pixeled, but after a while I decided to stop watching them and try to continue the project on my own. Currently all I have is variable declaration (only of integers), binary operations (sum, subtraction, division, multiplication and comparisons), if statements, and (not functioning) variable reassignment.

When trying to implement it, everything started not going as expected.

My current logic is:

  • If I encounter a variable during parsing, that was already instantiated, followed by an equal sign (=), it must be reassignment.

  • I create a new “Statement” node to my AST specific to variable reassignment.

  • I move the stack pointer to “one position” above the position of the said variable in the stack (8 bytes because I’m working with 64-bit x86).

  • I generate the code for the expression related to the reassignment.

I move the stack pointer one position above because my code generation for the expression (let’s suppose here it is just an integer, to simply the question) moves the desired value to rax and then pushes the value stored in rax into the stack. So, moving the stack pointer prior to this assures that the new value is pushed into the right position.

But, I abstracted the push instruction into a function that also increments the value of my compiler variable stack_size. So, even though I’m not changing the number of elements in the stack with this strategy, it still changes the value of the variable.

I obviously thought of not incrementing the stack_size variable when executing a push, but my code generation for whenever I find an integer (being a variable assignment, variable initialization, term inside the exit() function, etc) is the same: I always do this strategy of moving the value into rax and then executing a push.

I also thought of adding a parameter to some code generation functions related to variable reassignment, so that, for example, if I’m dealing with this process, I don’t increment the stack_size variable. But, first of all, I thought of it not being a “smart/correct/elegant” solution, because I would have to add this optional parameter to the statement generation function, that also calls the expression generation function, that calls the term generation function, that finally calls the push function. And, obviously, I can’t write the assembly code myself whenever I find a variable reassignment statement, because I can’t guarantee that the expression on the right of the equal (=) sign will be an integer, a binary expression, another variable, etc, and therefore I need to have this sequence of function calls.

I don’t know If I explained the whole question sufficiently, or even in a way that you can understand my problem. But how should I deal with this? Should I restructure a lot of things, or is there a simple (not necessarily efficient/best) solution?

PS: I did not include any code, because my code is a mess/enormous, with lots of files and it is in Portuguese, but if needed I can edit the post or put the link to my repository.

14

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật