Skip to content

Commit 7ba6577

Browse files
driusanircmaxell
authored andcommitted
Explicitly declare variable type.
1 parent 3c6a844 commit 7ba6577

File tree

2 files changed

+108
-28
lines changed

2 files changed

+108
-28
lines changed

lib/JIT/Builtin/MemoryManager/PHP.php

+105-27
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,90 @@ public function implement(): void {
167167
// These variables are a hack because compile{}
168168
// blocks currently only accept variables as arguments.
169169
$jit = $this->context->constantFromString("jit");
170-
$two = 2;
170+
$__type = $this->context->getTypeFromString('int32');
171+
172+
173+
$__kind = $__type->getKind();
174+
$__value = 2;
175+
switch ($__kind) {
176+
case \PHPLLVM\Type::KIND_INTEGER:
177+
if (!is_object($__value)) {
178+
$two = $__type->constInt($__value, false);
179+
break;
180+
}
181+
$__other_type = $__value->typeOf();
182+
switch ($__other_type->getKind()) {
183+
case \PHPLLVM\Type::KIND_INTEGER:
184+
if ($__other_type->getWidth() >= $__type->getWidth()) {
185+
$two = $this->context->builder->truncOrBitCast($__value, $__type);
186+
} else {
187+
$two = $this->context->builder->zExtOrBitCast($__value, $__type);
188+
}
189+
break;
190+
case \PHPLLVM\Type::KIND_DOUBLE:
191+
192+
$two = $this->context->builder->fpToSi($__value, $__type);
193+
194+
break;
195+
case \PHPLLVM\Type::KIND_ARRAY:
196+
case \PHPLLVM\Type::KIND_POINTER:
197+
$two = $this->context->builder->ptrToInt($__value, $__type);
198+
break;
199+
default:
200+
throw new \LogicException("Unknown how to handle type pair (int, " . $__other_type->toString() . ")");
201+
}
202+
break;
203+
case \PHPLLVM\Type::KIND_DOUBLE:
204+
if (!is_object($__value)) {
205+
$two = $__type->constReal(2);
206+
break;
207+
}
208+
$__other_type = $__value->typeOf();
209+
switch ($__other_type->getKind()) {
210+
case \PHPLLVM\Type::KIND_INTEGER:
211+
212+
$two = $this->context->builder->siToFp($__value, $__type);
213+
214+
break;
215+
case \PHPLLVM\Type::KIND_DOUBLE:
216+
$two = $this->context->builder->fpCast($__value, $__type);
217+
break;
218+
default:
219+
throw new \LogicException("Unknown how to handle type pair (double, " . $__other_type->toString() . ")");
220+
}
221+
break;
222+
case \PHPLLVM\Type::KIND_ARRAY:
223+
case \PHPLLVM\Type::KIND_POINTER:
224+
if (!is_object($__value)) {
225+
// this is very likely very wrong...
226+
$two = $__type->constInt($__value, false);
227+
break;
228+
}
229+
$__other_type = $__value->typeOf();
230+
switch ($__other_type->getKind()) {
231+
case \PHPLLVM\Type::KIND_INTEGER:
232+
$two = $this->context->builder->intToPtr($__value, $__type);
233+
break;
234+
case \PHPLLVM\Type::KIND_ARRAY:
235+
// $__tmp = $this->context->builder->($__value, $this->context->context->int64Type());
236+
// $(result) = $this->context->builder->intToPtr($__tmp, $__type);
237+
// break;
238+
case \PHPLLVM\Type::KIND_POINTER:
239+
$two = $this->context->builder->pointerCast($__value, $__type);
240+
break;
241+
default:
242+
throw new \LogicException("Unknown how to handle type pair (double, " . $__other_type->toString() . ")");
243+
}
244+
break;
245+
default:
246+
throw new \LogicException("Unsupported type cast: " . $__type->toString());
247+
}
248+
171249

172-
$fn___c81e728d9d4c2f636f067f89cc14862c = $this->context->lookupFunction('__mm__malloc');
173-
$block___c81e728d9d4c2f636f067f89cc14862c = $fn___c81e728d9d4c2f636f067f89cc14862c->appendBasicBlock('main');
174-
$this->context->builder->positionAtEnd($block___c81e728d9d4c2f636f067f89cc14862c);
175-
$size = $fn___c81e728d9d4c2f636f067f89cc14862c->getParam(0);
250+
$fn___eccbc87e4b5ce2fe28308fd9f2a7baf3 = $this->context->lookupFunction('__mm__malloc');
251+
$block___eccbc87e4b5ce2fe28308fd9f2a7baf3 = $fn___eccbc87e4b5ce2fe28308fd9f2a7baf3->appendBasicBlock('main');
252+
$this->context->builder->positionAtEnd($block___eccbc87e4b5ce2fe28308fd9f2a7baf3);
253+
$size = $fn___eccbc87e4b5ce2fe28308fd9f2a7baf3->getParam(0);
176254

177255
$result = $this->context->builder->call(
178256
$this->context->lookupFunction('_emalloc') ,
@@ -186,11 +264,11 @@ public function implement(): void {
186264
$this->context->builder->returnValue($result);
187265

188266
$this->context->builder->clearInsertionPosition();
189-
$fn___a87ff679a2f3e71d9181a67b7542122c = $this->context->lookupFunction('__mm__realloc');
190-
$block___a87ff679a2f3e71d9181a67b7542122c = $fn___a87ff679a2f3e71d9181a67b7542122c->appendBasicBlock('main');
191-
$this->context->builder->positionAtEnd($block___a87ff679a2f3e71d9181a67b7542122c);
192-
$void = $fn___a87ff679a2f3e71d9181a67b7542122c->getParam(0);
193-
$size = $fn___a87ff679a2f3e71d9181a67b7542122c->getParam(1);
267+
$fn___e4da3b7fbbce2345d7772b0674a318d5 = $this->context->lookupFunction('__mm__realloc');
268+
$block___e4da3b7fbbce2345d7772b0674a318d5 = $fn___e4da3b7fbbce2345d7772b0674a318d5->appendBasicBlock('main');
269+
$this->context->builder->positionAtEnd($block___e4da3b7fbbce2345d7772b0674a318d5);
270+
$void = $fn___e4da3b7fbbce2345d7772b0674a318d5->getParam(0);
271+
$size = $fn___e4da3b7fbbce2345d7772b0674a318d5->getParam(1);
194272

195273
$result = $this->context->builder->call(
196274
$this->context->lookupFunction('_erealloc') ,
@@ -205,10 +283,10 @@ public function implement(): void {
205283
$this->context->builder->returnValue($result);
206284

207285
$this->context->builder->clearInsertionPosition();
208-
$fn___1679091c5a880faf6fb5e6087eb1b2dc = $this->context->lookupFunction('__mm__free');
209-
$block___1679091c5a880faf6fb5e6087eb1b2dc = $fn___1679091c5a880faf6fb5e6087eb1b2dc->appendBasicBlock('main');
210-
$this->context->builder->positionAtEnd($block___1679091c5a880faf6fb5e6087eb1b2dc);
211-
$void = $fn___1679091c5a880faf6fb5e6087eb1b2dc->getParam(0);
286+
$fn___8f14e45fceea167a5a36dedd4bea2543 = $this->context->lookupFunction('__mm__free');
287+
$block___8f14e45fceea167a5a36dedd4bea2543 = $fn___8f14e45fceea167a5a36dedd4bea2543->appendBasicBlock('main');
288+
$this->context->builder->positionAtEnd($block___8f14e45fceea167a5a36dedd4bea2543);
289+
$void = $fn___8f14e45fceea167a5a36dedd4bea2543->getParam(0);
212290

213291
$this->context->builder->call(
214292
$this->context->lookupFunction('_efree') ,
@@ -223,10 +301,10 @@ public function implement(): void {
223301

224302
$this->context->builder->clearInsertionPosition();
225303
} else {
226-
$fn___c9f0f895fb98ab9159f51fd0297e236d = $this->context->lookupFunction('__mm__malloc');
227-
$block___c9f0f895fb98ab9159f51fd0297e236d = $fn___c9f0f895fb98ab9159f51fd0297e236d->appendBasicBlock('main');
228-
$this->context->builder->positionAtEnd($block___c9f0f895fb98ab9159f51fd0297e236d);
229-
$size = $fn___c9f0f895fb98ab9159f51fd0297e236d->getParam(0);
304+
$fn___45c48cce2e2d7fbdea1afc51c7c6ad26 = $this->context->lookupFunction('__mm__malloc');
305+
$block___45c48cce2e2d7fbdea1afc51c7c6ad26 = $fn___45c48cce2e2d7fbdea1afc51c7c6ad26->appendBasicBlock('main');
306+
$this->context->builder->positionAtEnd($block___45c48cce2e2d7fbdea1afc51c7c6ad26);
307+
$size = $fn___45c48cce2e2d7fbdea1afc51c7c6ad26->getParam(0);
230308

231309
$result = $this->context->builder->call(
232310
$this->context->lookupFunction('_emalloc') ,
@@ -236,11 +314,11 @@ public function implement(): void {
236314
$this->context->builder->returnValue($result);
237315

238316
$this->context->builder->clearInsertionPosition();
239-
$fn___d3d9446802a44259755d38e6d163e820 = $this->context->lookupFunction('__mm__realloc');
240-
$block___d3d9446802a44259755d38e6d163e820 = $fn___d3d9446802a44259755d38e6d163e820->appendBasicBlock('main');
241-
$this->context->builder->positionAtEnd($block___d3d9446802a44259755d38e6d163e820);
242-
$void = $fn___d3d9446802a44259755d38e6d163e820->getParam(0);
243-
$size = $fn___d3d9446802a44259755d38e6d163e820->getParam(1);
317+
$fn___6512bd43d9caa6e02c990b0a82652dca = $this->context->lookupFunction('__mm__realloc');
318+
$block___6512bd43d9caa6e02c990b0a82652dca = $fn___6512bd43d9caa6e02c990b0a82652dca->appendBasicBlock('main');
319+
$this->context->builder->positionAtEnd($block___6512bd43d9caa6e02c990b0a82652dca);
320+
$void = $fn___6512bd43d9caa6e02c990b0a82652dca->getParam(0);
321+
$size = $fn___6512bd43d9caa6e02c990b0a82652dca->getParam(1);
244322

245323
$result = $this->context->builder->call(
246324
$this->context->lookupFunction('_erealloc') ,
@@ -251,10 +329,10 @@ public function implement(): void {
251329
$this->context->builder->returnValue($result);
252330

253331
$this->context->builder->clearInsertionPosition();
254-
$fn___c20ad4d76fe97759aa27a0c99bff6710 = $this->context->lookupFunction('__mm__free');
255-
$block___c20ad4d76fe97759aa27a0c99bff6710 = $fn___c20ad4d76fe97759aa27a0c99bff6710->appendBasicBlock('main');
256-
$this->context->builder->positionAtEnd($block___c20ad4d76fe97759aa27a0c99bff6710);
257-
$void = $fn___c20ad4d76fe97759aa27a0c99bff6710->getParam(0);
332+
$fn___c51ce410c124a10e0db5e4b97fc2af39 = $this->context->lookupFunction('__mm__free');
333+
$block___c51ce410c124a10e0db5e4b97fc2af39 = $fn___c51ce410c124a10e0db5e4b97fc2af39->appendBasicBlock('main');
334+
$this->context->builder->positionAtEnd($block___c51ce410c124a10e0db5e4b97fc2af39);
335+
$void = $fn___c51ce410c124a10e0db5e4b97fc2af39->getParam(0);
258336

259337
$this->context->builder->call(
260338
$this->context->lookupFunction('_efree') ,

lib/JIT/Builtin/MemoryManager/PHP.pre

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class PHP extends MemoryManager {
3636
// These variables are a hack because compile{}
3737
// blocks currently only accept variables as arguments.
3838
$jit = $this->context->constantFromString("jit");
39-
$two = 2;
39+
compile {
40+
$two = (int32) 2;
41+
}
4042

4143
compile {
4244
function __mm__malloc($size) {

0 commit comments

Comments
 (0)