How do VMs implement function calling?

I’m reading a compiler textbook that compiles to some form of assembly. Since I don’t know this assembly language I decided to invent my own simple “assembly language” and implement a basic “virtual machine” which will execute these instructions.

Currently I’m thinking how I can implement function decleration and function calling in the language and VM.

I had the following idea, please tell me what you think:

Function declerations would look like simple labels. At the end of a function there’s an end statement. The ‘main program’ is a function by itself. For examle:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>main:
// some logic
CALL funcA
// more logic
END
funcA:
// .. some logic
END
</code>
<code>main: // some logic CALL funcA // more logic END funcA: // .. some logic END </code>
main:
// some logic
CALL funcA
// more logic
END
funcA:
// .. some logic
END

However the difference between call <function> and goto <label> is this:

goto simply sets the ‘instruction pointer’ (the register that holds the number of the next line to be executed) to the line number of the label.

call does what goto does, but before jumping it pushes the current line number (plus 1) onto a stack.

When end is reached, the VM pops the top of the stack and jumps to this line number. If there is nothing in the stack, the program terminates.

So for example, for the code above this is what happens:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>main: // this is where the VM starts
// some logic
CALL funcA // push onto the stack the number 4 (3+1), and jump to the label funcA
// more logic .. this is where we return to from funcA
END // pop the top of the stack and jump to this line number. nothing -> terminate
funcA:
// .. some logic
END // pop the top of the stack (the number 4), and jump to this line number
</code>
<code>main: // this is where the VM starts // some logic CALL funcA // push onto the stack the number 4 (3+1), and jump to the label funcA // more logic .. this is where we return to from funcA END // pop the top of the stack and jump to this line number. nothing -> terminate funcA: // .. some logic END // pop the top of the stack (the number 4), and jump to this line number </code>
main: // this is where the VM starts
// some logic
CALL funcA // push onto the stack the number 4 (3+1), and jump to the label funcA
// more logic .. this is where we return to from funcA
END // pop the top of the stack and jump to this line number. nothing -> terminate
funcA:
// .. some logic
END // pop the top of the stack (the number 4), and jump to this line number

What do you think of this approach?

9

It depends on the VM, to be honest.

It is a bit easier to write a VM in an OO language that operates on data structures kept in memory rather than on some form of linearized bytecode. If you ever implement a toy programming language you might decide to do it this way, at least initially (I have done this before), in which case the VM is probably just a polymorphic “run()” method. A lot of scripting languages work this way, because the code will be generated at the same time in which it is run, so there is no need to maintain the bytecode for later.

If you compile all the way to a linearized bytecode (like how Java compiles), the way you handle operation calls will depend on the language. If you do not allow recursion (most old programming languages originally did not), you can simply inline the operation calls directly into the bytecode. This causes code bloat but is “safe” otherwise.

If you compile all the way to a linearized bytecode, but you want to allow recursion, then you’ll need to emulate a call stack. Each frame of the stack will hold a reference to the location in the code segment where that operation was invoked (for operation-based frames). It will also hold copies of all variables local to that frame. Objects to which those variables refer may be held on the stack or in a separate heap segment, depending on the design of the programming language and (more likely) the code optimizer being used by the compiler.

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