FAQ

Reasons and solutions for functions configuring virtualization but not being virtualized

1.Functions may not be virtualized when there is a linear condition judgment. For example:

void foo(int a) {
  switch (a) {
  case 0: puts("0");
  case 1: puts("1");
  case 2: puts("2");
  default:puts("O");
  }
}

Or

void foo(int a) {
  if (a == 0) puts("0");
  else if (a == 1) puts("1");
  else if (a == 2) puts("2");
  else puts("O");
}

The reason why this kind of code can't be virtualized is that when the compiler optimizes, it can dynamically calculate the address of the conditional branch through parameter a, and then execute the selected branch by register jump.(arm64: BX X1,x86-64: JMP RDI). When this happens, the KiwiVMCompiler will automatically filter without virtualization.。 The solution is to change the branch to non-linear, so that the compiler can not optimize, such as changing the above 0, 1, 2 to 0, 3, 8.

2.If the function argument or return value is a floating point number, or if such a function is called, those functions will not be virtualized. For example:

float foo(float a) {
  return a + 1.0f;
}

Or

int foo(int a) {
  return pow((float)a, 2);
}

The solution is to make the important code a separate function, not in a floating-point function.

3.Functions with a restrained assembly will not be virtualized. For example:

void foo(void) {
  __asm__("ret");
}

Or

int foo(int a) {
  return ntol(a);//Will be optimized to an assembly instruction
}

The solution is to make the code that produces the assembly instructions into a separate function.

4.Functions with a large object as the return value will not be virtualized. For example:

std::string foo(void) {
  return "foo";
}

Solution:

void foo(std::string &result) {
  result = "foo";
}

5.Functions having non-reference large objects as parameters will not be virtualized. For example:

void foo(std::string a) {
  puts(a.c_str());
}

Solution:

void foo(std::string &a) {
  puts(a.c_str());
}

6. Functions containing a large number of floating point operations may not be virtualized. Currently KiwiVM has limited support for floating point numbers.

The solution is to make the important code a separate function, not in a floating-point function.

results matching ""

    No results matching ""