Commit 409a587
committed
Sort switch cases by value; coalesce identical-body cases
Three composing changes to the optimized Ok() switch generator:
1. Case-label sort. Each switch arm's labels are sorted by the
underlying integer/enum value before emit. _case_sort_key()
returns the int for sorting. Sorted cases give older embedded
GCCs (the ones shipped with microblaze-elf and many bare-metal
arm-none-eabi toolchains) a better shot at emitting a dense
jump table rather than an if-ladder.
2. Identical-body coalescing. Cases whose rendered body text is
identical (same field set in the same order) are merged into a
single arm with multiple \`case X:\` labels. The C++ compiler
emits one body for the whole arm — a real text-size win once a
later PR (disjunction matching) starts producing such pairs.
3. Multi-field per case. When two conditional fields share a
discriminant + case value (\`if tag == 0: a\` and \`if tag == 0: b\`),
they're now bundled into the same case arm rather than the second
falling back to a separate if-statement. Each field's validation
becomes one line of the case body.
The ok_method_switch_case template becomes ok_method_switch_arm,
taking pre-formatted \${case_labels} and \${case_body} strings.
Single-label single-field arms render identically to the old
template, so golden churn is limited to the f0_copy field in
testdata/many_conditionals.emb folding into case 0 of the
LargeConditionals switch.1 parent 507136e commit 409a587
3 files changed
Lines changed: 81 additions & 43 deletions
File tree
- compiler/back_end/cpp
- testdata/golden_cpp
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
412 | 412 | | |
413 | 413 | | |
414 | 414 | | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
| 415 | + | |
| 416 | + | |
419 | 417 | | |
420 | 418 | | |
421 | 419 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1364 | 1364 | | |
1365 | 1365 | | |
1366 | 1366 | | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
1367 | 1383 | | |
1368 | 1384 | | |
1369 | 1385 | | |
| |||
1470 | 1486 | | |
1471 | 1487 | | |
1472 | 1488 | | |
1473 | | - | |
1474 | | - | |
| 1489 | + | |
1475 | 1490 | | |
1476 | 1491 | | |
1477 | | - | |
1478 | 1492 | | |
1479 | | - | |
1480 | | - | |
1481 | | - | |
1482 | | - | |
1483 | | - | |
1484 | | - | |
1485 | | - | |
1486 | | - | |
1487 | | - | |
1488 | | - | |
1489 | | - | |
1490 | | - | |
1491 | | - | |
1492 | | - | |
1493 | | - | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
1494 | 1497 | | |
1495 | 1498 | | |
1496 | 1499 | | |
| |||
1508 | 1511 | | |
1509 | 1512 | | |
1510 | 1513 | | |
1511 | | - | |
1512 | | - | |
1513 | | - | |
1514 | | - | |
1515 | | - | |
1516 | | - | |
1517 | | - | |
1518 | | - | |
1519 | | - | |
1520 | | - | |
1521 | | - | |
1522 | | - | |
1523 | | - | |
1524 | | - | |
1525 | | - | |
1526 | | - | |
1527 | | - | |
1528 | | - | |
| 1514 | + | |
1529 | 1515 | | |
1530 | 1516 | | |
1531 | 1517 | | |
| |||
1538 | 1524 | | |
1539 | 1525 | | |
1540 | 1526 | | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
| 1578 | + | |
| 1579 | + | |
| 1580 | + | |
| 1581 | + | |
| 1582 | + | |
1541 | 1583 | | |
1542 | 1584 | | |
1543 | 1585 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
100 | 101 | | |
101 | 102 | | |
102 | 103 | | |
| |||
500 | 501 | | |
501 | 502 | | |
502 | 503 | | |
503 | | - | |
504 | | - | |
505 | | - | |
506 | 504 | | |
507 | 505 | | |
508 | 506 | | |
| |||
0 commit comments