kripken/emscripten — Issue #273: Function Parameters Lost When Optimized with -O1
Author: peterleecomcn
Created: 2012-02-23 18:37:12+00:00
State: open

The following source code in `main.cpp`:

```c++
class Complex
{
public:
Complex(double x = 1.0, double y = 2.0)
:x(x), y(y)
{

}

double GetLengthSquared()
{
return x*x + y*y;
}

private:
double x;
double y;
};

extern "C"
{

#include
#include

Complex* Complex_Ctor(double x, double y)
{
return new Complex(x, y);
}

void Complex_Dtor(Complex* pc)
{
delete pc;
}

double Complex_GetLengthSquared(Complex* pc)
{
return pc->GetLengthSquared();
}

}

int main(int argc, char* argv[])
{
Complex* pc = Complex_Ctor(3.0, 4.0);
printf("%lf\n", Complex_GetLengthSquared(pc));
Complex_Dtor(pc);

return 0;
}
```

With `-O0` optimization:
```
emcc -O0 main.cpp -o main.js
```

I could still see the generated function with two parameters

```javascript
function _Complex_Ctor($x, $y) {
;
var __label__;
__label__ = -1;
while(1) switch(__label__) {
case -1: // $0
var $1;
var $2;
var $3;
var $4;
$1=$x;
$2=$y;
var $5=__Znwj(16);
var $6=$5;
var $7=$1;
var $8=$2;
(function() { try { __THREW__ = false; return __ZN7ComplexC1Edd($6, $7, $8) } catch(e) { if (typeof e != "number") throw e; if (ABORT) throw e; __THREW__ = true; print("Exception: " + e + ", currently at: " + (new Error().stack)); return null } })(); if (!__THREW__) { __label__ = 1; break; } else { __label__ = 2; break; }
case 1: // $9
;
return $6;
case 2: // $10
var $11=___cxa_find_matching_catch(HEAP32[((_llvm_eh_exception.buf)>>2)],HEAP32[((_llvm_eh_exception.buf+4)>>2)],[]);
var $12=$11.f0;
$3=$12;
var $13=$11.f1;
$4=$13;
__ZdlPv($5);
__label__ = 3; break;
case 3: // $14
var $15=$3;
var $16=$3;
var $17=$4;
var $18=(tempValue = [0,0,0,0,0,0,0,0], tempValue.f0 = $16, tempValue);
var $19=($18.f1 = $17, $18);
print("Resuming exception");throw HEAP32[((_llvm_eh_exception.buf)>>2)];
default: assert(0, "bad label: " + __label__);
}
}
```

But with `-O1`
```
emcc -O1 main.cpp -o main.js
```

The generated function does not have the two parameters:

```javascript
function _Complex_Ctor() {
var $1 = __Znwj();
var $2 = $1;
__ZN7ComplexC1Edd($2);
return $2;
}
```

Thanks.