about summary refs log tree commit diff stats
path: root/027call_ingredient.cc
Commit message (Expand)AuthorAgeFilesLines
* 2990Kartik K. Agaram2016-05-201-5/+5
* 2931 - be explicit about making copiesKartik K. Agaram2016-05-061-2/+2
* 2864 - replace all address:shared with just addressKartik K. Agaram2016-04-241-5/+3
* 2862Kartik K. Agaram2016-04-241-0/+185
='#n31'>31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608'll uniformly use '*' to indicate getting at the value in an address. This will also provide a consistent hint of the addressing mode. === Compilation strategy --- User-defined statements User-defined functions will be called with the same syntax as primitives. They'll translate to a sequence of push instructions (one per operand, both in and in-out), a call instruction, and a sequence of pop instructions, either to a black hole (in operands) or a location (in-out operands). This follows the standard Unix calling convention: push EBP copy ESP to EBP push arg 1 push arg 2 ... call pop arg n ... pop arg 1 copy EBP to ESP pop ESP Implication: each function argument needs to be something push/pop can accept. It can't be an address, so arrays and structs will either have to be passed by value, necessitating copies, or allocated on the heap. We may end up allocating members of structs in separate heap allocations just so we can pass them piecemeal to helper functions. (Mu has explored this trade-off in the past.) --- Primitive statements Operands may be: in code (literals) in registers on the stack on the global segment Operands are always scalar. Variables on the stack or global segment are immutable references. - Variables on the stack are stored at addresses like *(EBP+n) - Global variables are stored at addresses like *disp32, where disp32 is a statically known constant #define local(n) 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 #define disp32(N) 0/mod 5/rm32/include-disp32 N/disp32 Since the language will not be orthogonal, compilation proceeds by pattern matching over a statement along with knowledge about the types of its operands, as well as where they're stored (register/stack/global). We now enumerate mappings for various categories of statements, based on the type and location of their operands. Many statements will end up encoding to the exact same x86 instructions. But the types differ, and they get type-checked differently along the way. A. x : int <- add y Requires y to be scalar (32 bits). Result will always be an int. No pointer arithmetic. reg <- add literal => 81 0/subop 3/mod ...(0) reg <- add reg => 01 3/mod ...(1) reg <- add stack => 03 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 reg/r32 ...(2) reg <- add global => 03 0/mod 5/rm32/include-disp32 global/disp32 reg/r32 ...(3) stack <- add literal => 81 0/subop 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 literal/imm32 ...(4) stack <- add reg => 01 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 reg/r32 ...(5) stack <- add stack => disallowed stack <- add global => disallowed global <- add literal => 81 0/subop 0/mod 5/rm32/include-disp32 global/disp32 literal/imm32 ...(6) global <- add reg => 01 0/mod 5/rm32/include-disp32 global/disp32 reg/r32 ...(7) global <- add stack => disallowed global <- add global => disallowed Similarly for sub, and, or, xor and even copy. Replace the opcodes above with corresponding ones from this table: add sub and or xor copy/mov reg <- op literal 81 0/subop 81 5/subop 81 4/subop 81 1/subop 81 6/subop c7 reg <- op reg 01 or 03 29 or 2b 21 or 23 09 or 0b 31 or 33 89 or 8b reg <- op stack 03 2b 23 0b 33 8b reg <- op global 03 2b 23 0b 33 8b stack <- op literal 81 0/subop 81 5/subop 81 4/subop 81 1/subop 81 6/subop c7 stack <- op reg 01 29 21 09 31 89 global <- op literal 81 0/subop 81 5/subop 81 4/subop 81 1/subop 81 6/subop c7 global <- op reg 01 29 21 09 31 89 B. x/reg : int <- mul y Requires y to be scalar. x must be in a register. Multiplies can't write to memory. reg <- mul literal => 69 ...(8) reg <- mul reg => 0f af 3/mod ...(9) reg <- mul stack => 0f af 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 reg/r32 ...(10) reg <- mul global => 0f af 0/mod 5/rm32/include-disp32 global/disp32 reg/r32 ...(11) C. x/EAX/quotient : int, y/EDX/remainder : int <- idiv z # divide EAX by z; store results in EAX and EDX Requires source x and z to both be scalar. x must be in EAX and y must be in EDX. Divides can't write anywhere else. First clear EDX (we don't support ints larger than 32 bits): 31/xor 3/mod 2/rm32/EDX 2/r32/EDX then: EAX, EDX <- idiv literal => disallowed EAX, EDX <- idiv reg => f7 7/subop 3/mod ...(12) EAX, EDX <- idiv stack => f7 7/subop 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 ...(13) EAX, EDX <- idiv global => f7 7/subop 0/mod 5/rm32/include-disp32 global/disp32 reg/r32 ...(14) D. x : int <- not (weird syntax, but we'll ignore that) Requires x to be an int. reg <- not => f7 3/mod ...(15) stack <- not => f7 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 ...(16) global <- not => f7 0/mod 5/rm32/include-disp32 global/disp32 reg/r32 ...(17) E. x : (address t) <- get o : T, %f (Assumes T.f has type t.) o can't be on a register since it's a non-primitive (likely larger than a word) f is a literal x must be in a register (by definition for an address) reg1 <- get reg2, literal => 8d/lea 1/mod reg2/rm32 literal/disp8 reg1/r32 ...(18) reg <- get stack, literal => 8d/lea 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n+literal/disp8 reg/r32 ...(19) (simplifying assumption: stack frames can't be larger than 256 bytes) reg <- get global, literal => 8d/lea 0/mod 5/rm32/include-disp32 global+literal/disp32, reg/r32 ...(20) F. x : (offset T) <- index i : int, %size(T) This statement is used to translate an array index (denominated in the type of array elements) into an offset (denominated in bytes). It's just a multiply but with a new type for the result so that we can keep the type system sound. Since index statements translate to multiplies, 'x' must be a register. The %size(T) argument is statically known, so will always be a literal. reg1 <- index reg2, literal => 69/mul 3/mod reg2/rm32 literal/imm32 -> reg1/r32 or 68/mul 3/mod reg2/rm32 literal/imm8 -> reg1/r32 ...(21) reg1 <- index stack, literal => 69/mul 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 literal/imm32 -> reg1/r32 ...(22) reg1 <- index global, literal => 69/mul 0/mod 5/rm32/include-disp32 global/disp32 literal/imm32 -> reg1/r32 ...(23) G. x : (address T) <- advance a : (array T), idx : (offset T) reg <- advance a/reg, idx/reg => 8d/lea 0/mod 4/rm32/SIB a/base idx/index 0/scale reg/r32 ...(24) reg <- advance stack, literal => 8d/lea 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n+literal/disp8 reg/r32 ...(25) reg <- advance stack, reg2 => 8d/lea 1/mod 4/rm32/SIB 5/base/EBP reg2/index 0/scale n/disp8 reg/r32 ...(26) reg <- advance global, literal => 8d/lea 0/mod 5/rm32/include-disp32 global+literal/disp32, reg/r32 ...(27) === Example Putting it all together: code generation for `a[i].y = 4` where a is an array of 2-d points with x, y coordinates. If a is allocated on the stack, say of type (array point 6): offset/EAX : (offset point) <- index i, 8 # (22) tmp/EBX : (address point) <- advance a : (array point 6), offset/EAX # (26) tmp2/ECX : (address number) <- get tmp/EBX : (address point), 4/y # (18) *tmp2/ECX <- copy 4 # (5 for copy/mov with 0 disp8) === More complex statements A couple of statement types expand to multiple instructions: Function calls. We've already seen these above. Bounds checking against array length in 'advance' Dereferencing 'ref' types (see type list up top). Requires an alloc id check. G'. Bounds checking the 'advance' statement begins with a few extra instructions. For example: x/EAX : (address T) <- advance a : (array T), literal Suppose array 'a' lies on the stack starting at EBP+4. Its length will be at EBP+4, and the actual contents of the array will start from EBP+8. compare *(EBP+4), literal jump-if-greater panic # rudimentary error handling Now we're ready to perform the actual 'lea': lea EBP+8 + literal, reg # line 25 above H. Dereferencing a 'ref' needs to be its own statement, yielding an address. This statement has two valid forms: reg : (address T) <- deref stack : (ref T) reg : (address T) <- deref global : (ref T) Since refs need 8 bytes they can't be in a register. And of course the output is an address so it must be in a register. Compiling 'deref' will take a few instructions. Consider the following example where 's' is on the stack, say starting at EBP+4: EDX : (address T) <- deref s : (ref T) The alloc id of 's' is at *(EBP+4) and the actual address is at *(EBP+8). The above statement will compile down to the following: EDX/s <- copy *(EBP+8) # the address stored in s EDX/alloc-id <- copy *EDX # alloc id of payload *s compare EDX, *(EBP+4) # compare with alloc id of pointer jump-unless-equal panic # rudimentary error handling # compute *(EBP+8) + 4 EDX <- copy *(EBP+8) # recompute the address in s because we can't save the value anywhere) EDX <- add EDX, 4 # skip alloc id this time Subtleties: a) if the alloc id of the payload is 0, then the payload is reclaimed b) looking up the payload's alloc id *could* cause a segfault. What to do? === More speculative ideas Initialize data segment with special extensible syntax for literals. All literals except numbers and strings start with %. Global variable declarations would now look like: var s : (array character) = "abc" # exception to the '%' convention var p : point = %point(3, 4) === Credits Forth C Rust Lisp qhasm