Skip to content

Commit 18680fc

Browse files
committed
WASM_X86: Add comments about emit_exit2() and X86Visitor stack working
1 parent 2b0e213 commit 18680fc

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/libasr/codegen/wasm_to_x86.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ namespace LFortran {
1111

1212
namespace wasm {
1313

14+
/*
15+
16+
This X86Visitor uses stack to pass arguments and return values from functions.
17+
Since in X86, instructions operate on registers (and not on stack),
18+
for every instruction we pop elements from top of stack and store them into
19+
registers. After operating on the registers, the result value is then
20+
pushed back onto the stack.
21+
22+
One of the reasons to use stack to pass function arguments is that,
23+
it allows us to define and call functions with any number of parameters.
24+
As registers are limited in number, if we use them to pass function arugments,
25+
the number of arguments we could pass to a function would get limited by
26+
the number of registers available with the CPU.
27+
28+
*/
29+
1430
class X86Visitor : public WASMDecoder<X86Visitor>,
1531
public WASM_INSTS_VISITOR::BaseWASMVisitor<X86Visitor> {
1632
public:

src/libasr/codegen/x86_assembler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,14 @@ void emit_elf32_footer(X86Assembler &a);
815815

816816
void emit_exit(X86Assembler &a, const std::string &name,
817817
uint32_t exit_code);
818+
819+
// this is similar to emit_exit() but takes the argument (i.e. exit code)
820+
// from top of stack. To call this exit2, one needs to jump to it
821+
// instead of call it. (Because calling pushes the instruction address and
822+
// base pointer value (ebp) of previous function and thus makes the
823+
// exit code parameter less reachable)
818824
void emit_exit2(X86Assembler &a, const std::string &name);
825+
819826
void emit_data_string(X86Assembler &a, const std::string &label,
820827
const std::string &s);
821828
void emit_print(X86Assembler &a, const std::string &msg_label,

0 commit comments

Comments
 (0)