Understanding class structures and constructor calls

Having lost through loops, branches, tables, and all these good operators, I almost started to feel comfortable with the language to create something useful, but there is some logic that I still do not understand. Please feel free to be a little.

Question . Can someone explain how the translated code works? I have added specific questions below.

First, here is some trivial C ++ code that I converted:

class FirstClass {
  int prop1 = 111;
  int prop2 = 222;
  int prop3 = 333;

  public:
  FirstClass(int param1, int param2) {
    prop1 += param1 + param2;  

  }
};

class SecondClass {
  public:
  SecondClass() {

  }
};

int main() {
  FirstClass firstClass1(10, 5);
  FirstClass firstClass2(30, 15);
  FirstClass firstClass3(2, 4);
  FirstClass firstClass4(2, 4);
}

It means:

(module
  (table 0 anyfunc)
  (memory $0 1)
  (export "memory" (memory $0))
  (export "main" (func $main))
  (func $main (result i32)
    (local $0 i32)
    (i32.store offset=4
      (i32.const 0)
      (tee_local $0
        (i32.sub
          (i32.load offset=4
            (i32.const 0)
          )
          (i32.const 64)
        )
      )
    )
    (drop
      (call $_ZN10FirstClassC2Eii
        (i32.add
          (get_local $0)
          (i32.const 48)
        )
        (i32.const 10)
        (i32.const 5)
      )
    )
    (drop
      (call $_ZN10FirstClassC2Eii
        (i32.add
          (get_local $0)
          (i32.const 32)
        )
        (i32.const 30)
        (i32.const 15)
      )
    )
    (drop
      (call $_ZN10FirstClassC2Eii
        (i32.add
          (get_local $0)
          (i32.const 16)
        )
        (i32.const 2)
        (i32.const 4)
      )
    )
    (drop
      (call $_ZN10FirstClassC2Eii
        (get_local $0)
        (i32.const 2)
        (i32.const 4)
      )
    )
    (i32.store offset=4
      (i32.const 0)
      (i32.add
        (get_local $0)
        (i32.const 64)
      )
    )
    (i32.const 0)
  )
  (func $_ZN10FirstClassC2Eii (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
    (i32.store offset=8
      (get_local $0)
      (i32.const 222)
    )
    (i32.store offset=4
      (get_local $0)
      (i32.const 222)
    )
    (i32.store
      (get_local $0)
      (i32.add
        (i32.add
          (get_local $1)
          (get_local $2)
        )
        (i32.const 111)
      )
    )
    (get_local $0)
  )
)

So now I have some questions about what is really going on here. Although I think I understand most of this, there are some more things that I'm just not sure about:

For example, see the constructor and its signature:

(func $_ZN10FirstClassC2Eii (param $0 i32) (param $1 i32) (param $2 i32) (result i32)

: (param $0 i32), , , . , . , , 4 , , (local $0 i32), , , ?

:

(drop
  (call $_ZN10FirstClassC2Eii
    (i32.add
      (get_local $0)
      (i32.const 32)
    )
    (i32.const 30)
    (i32.const 15)
  )
)

3 . ? ? , 16 (im ), . , .

:

(i32.store offset=4
  (i32.const 0)
  (tee_local $0
    (i32.sub
      (i32.load offset=4
        (i32.const 0)
      )
      (i32.const 64)
    )
  )
)

? , , 4? 4 ?

+4
1

, , ++ IR . , , LLVM, LLVM IR, spelunking. , , LLVM IR. , WebAssembly LLVM IR, ++ . , , !


, ++, *this. . i32? WebAssembly i32.

LLVM IR :

define linkonce_odr void @FirstClass::FirstClass(int, int)(%class.FirstClass*, i32, i32) unnamed_addr #2 comdat align 2 !dbg !29 {

%class.FirstClass* - *this. , WebAssembly, i32.


... ? *this, . LLVM :

  %1 = alloca %class.FirstClass, align 4
  %2 = alloca %class.FirstClass, align 4
  %3 = alloca %class.FirstClass, align 4
  %4 = alloca %class.FirstClass, align 4

, FirstClass. WebAssembly, - . WebAssembly 3 , ++:

  • ( , add 2, 1).
  • .
  • Memory.

, 1. 2. *this , Memory. Memory? Emscripten ! , 4, , (i32.load offset=4 (i32.const 0)). alloca LLVM , (i32.add (get_local $0) (i32.const 48)) ( $0) . *this.

, ++ on-stack ! / WebAssembly ( ). ISA, x86 ARM: , ISA . WebAssembly ISA, , , LLVM/Emscripten , . , , - ( ), ( WebAssembly ).


, :

  • .
  • 64.
  • .

. , , 64 . alloca. () WebAssembly ABI, .

64? 4 x 16, FirstClass: 3 i32, 16 , . sizeof(FirstClass) ++ ( 12), ( 4 , ). ++ LLVM WebAssembly.

+1
source

Source: https://habr.com/ru/post/1675444/


All Articles