cpyte 4.6.4__tar.gz → 4.6.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. {cpyte-4.6.4/source/cpyte.egg-info → cpyte-4.6.7}/PKG-INFO +30 -1
  2. {cpyte-4.6.4 → cpyte-4.6.7}/readme.md +29 -0
  3. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/__init__.py +1 -1
  4. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/bytecoding.py +172 -65
  5. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/compiling.py +19 -5
  6. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/gc_runtime.c +228 -62
  7. cpyte-4.6.7/source/cpyte/gc_runtime_addition.c +95 -0
  8. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/linker.py +5 -1
  9. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/mainpie.py +1 -1
  10. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/runtime.c +202 -52
  11. {cpyte-4.6.4 → cpyte-4.6.7/source/cpyte.egg-info}/PKG-INFO +30 -1
  12. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte.egg-info/SOURCES.txt +1 -0
  13. {cpyte-4.6.4 → cpyte-4.6.7}/test/test_compiler_regressions.py +42 -4
  14. {cpyte-4.6.4 → cpyte-4.6.7}/MANIFEST.in +0 -0
  15. {cpyte-4.6.4 → cpyte-4.6.7}/pyproject.toml +0 -0
  16. {cpyte-4.6.4 → cpyte-4.6.7}/setup.cfg +0 -0
  17. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/__main__.py +0 -0
  18. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/_bignum_bc.py +0 -0
  19. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/_runtime_bc.py +0 -0
  20. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/astparse.py +0 -0
  21. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/bignum.c +0 -0
  22. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/bignum_protos.h +0 -0
  23. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/clib.py +0 -0
  24. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/elf2sef.py +0 -0
  25. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/extension_hooks.py +0 -0
  26. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/floats_runtime.c +0 -0
  27. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/formatter.py +0 -0
  28. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/generate_bc.py +0 -0
  29. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/lexar.py +0 -0
  30. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/lsp_server.py +0 -0
  31. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/mksef.py +0 -0
  32. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/optimizations.py +0 -0
  33. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/package_manifest.py +0 -0
  34. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/runpy_runtime.c +0 -0
  35. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/runtime_scorpion.c +0 -0
  36. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/scorpion_abi.py +0 -0
  37. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/sef.py +0 -0
  38. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/semantic_analasis.py +0 -0
  39. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/ugc.h +0 -0
  40. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/ui.py +0 -0
  41. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/update_check.py +0 -0
  42. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/winjit_patch.py +0 -0
  43. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte/winjit_stubs.c +0 -0
  44. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte.egg-info/dependency_links.txt +0 -0
  45. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte.egg-info/entry_points.txt +0 -0
  46. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte.egg-info/requires.txt +0 -0
  47. {cpyte-4.6.4 → cpyte-4.6.7}/source/cpyte.egg-info/top_level.txt +0 -0
  48. {cpyte-4.6.4 → cpyte-4.6.7}/test/test_bignum_jit.py +0 -0
  49. {cpyte-4.6.4 → cpyte-4.6.7}/test/test_fuzz_del_has.py +0 -0
  50. {cpyte-4.6.4 → cpyte-4.6.7}/test/test_scorpion_abi.py +0 -0
  51. {cpyte-4.6.4 → cpyte-4.6.7}/test/test_scorpion_dl.py +0 -0
  52. {cpyte-4.6.4 → cpyte-4.6.7}/test/test_sef_tools.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cpyte
3
- Version: 4.6.4
3
+ Version: 4.6.7
4
4
  Summary: The Cpyte programming language compiler
5
5
  Author: Hoang Duy Tung
6
6
  License: MIT
@@ -100,3 +100,32 @@ The compiler auto-discovers a C compiler (`clang` → `cc` → `gcc`) and system
100
100
  ## Continuous Integration
101
101
 
102
102
  GitHub Actions (see `.github/workflows/code_quality.yml`) builds and runs a curated, cross-platform regression corpus on Ubuntu (x86_64 + arm64), macOS (x86_64 + arm64), and Windows. `ci_test.py` AOT-compiles each program with the system linker and executes the result to exercise the full pipeline — lexer, parser, semantic analysis, LLVM codegen, the C and GC runtimes, and linking.
103
+
104
+ ## CI Benchmark Results
105
+
106
+ The table below is updated by the benchmark CI job on `main`. It is a separate
107
+ measurement from any historical benchmark results and is collected on the
108
+ dedicated benchmark runner.
109
+
110
+ <!-- CPYTE_CI_BENCHMARKS_START -->
111
+ Latest run: [c840d618fbaf](https://github.com/Cpyte/Cpyte/actions/runs/41209544274) · updated 2026-10-31 14:23 UTC
112
+
113
+ | Benchmark | C | Cpy | Python | Cpy/C | Python/C |
114
+ |---|---:|---:|---:|---:|---:|
115
+ | `array_sum` | 6.79ms | 6.77ms | 249.80ms | 1.00x | 36.77x |
116
+ | `bitwise_mix` | 2.87ms | 3.00ms | 681.37ms | 1.05x | 237.59x |
117
+ | `branch_mix` | 4.38ms | 5.88ms | 438.32ms | 1.34x | 100.08x |
118
+ | `call_overhead` | 2.87ms | 2.99ms | 341.99ms | 1.04x | 119.34x |
119
+ | `fib_iterative` | 16.73ms | 16.96ms | 11.059s | 1.01x | 661.21x |
120
+ | `fib_recursive` | 161.87ms | 1.13ms | 7.278s | 0.01x | 44.96x |
121
+ | `float_math` | 6.02ms | 6.15ms | 253.87ms | 1.02x | 42.15x |
122
+ | `integer_mix` | 3.47ms | 3.45ms | 387.15ms | 0.99x | 111.46x |
123
+ | `loop_sum` | 968.71us | 1.04ms | 2.933s | 1.07x | 3027.80x |
124
+ | `matrix_mult` | 4.08ms | 4.11ms | 1.741s | 1.01x | 426.44x |
125
+ | `mem_alloc` | 960.98us | 1.00ms | 17.47ms | 1.04x | 18.18x |
126
+ | `prime_count` | 66.94ms | 79.57ms | 1.827s | 1.19x | 27.29x |
127
+ | `quicksort` | 3.69ms | 2.60ms | 29.43ms | 0.70x | 7.98x |
128
+ | `string_concat` | 19.51ms | 156.87ms | 25.83ms | 8.04x | 1.32x |
129
+
130
+ Process wall time includes startup. These are measurements from the dedicated `cpyte-official-runner-1`, not the historical benchmark figures elsewhere in this README; results can vary with host load and software versions.
131
+ <!-- CPYTE_CI_BENCHMARKS_END -->
@@ -88,3 +88,32 @@ The compiler auto-discovers a C compiler (`clang` → `cc` → `gcc`) and system
88
88
  ## Continuous Integration
89
89
 
90
90
  GitHub Actions (see `.github/workflows/code_quality.yml`) builds and runs a curated, cross-platform regression corpus on Ubuntu (x86_64 + arm64), macOS (x86_64 + arm64), and Windows. `ci_test.py` AOT-compiles each program with the system linker and executes the result to exercise the full pipeline — lexer, parser, semantic analysis, LLVM codegen, the C and GC runtimes, and linking.
91
+
92
+ ## CI Benchmark Results
93
+
94
+ The table below is updated by the benchmark CI job on `main`. It is a separate
95
+ measurement from any historical benchmark results and is collected on the
96
+ dedicated benchmark runner.
97
+
98
+ <!-- CPYTE_CI_BENCHMARKS_START -->
99
+ Latest run: [c840d618fbaf](https://github.com/Cpyte/Cpyte/actions/runs/41209544274) · updated 2026-10-31 14:23 UTC
100
+
101
+ | Benchmark | C | Cpy | Python | Cpy/C | Python/C |
102
+ |---|---:|---:|---:|---:|---:|
103
+ | `array_sum` | 6.79ms | 6.77ms | 249.80ms | 1.00x | 36.77x |
104
+ | `bitwise_mix` | 2.87ms | 3.00ms | 681.37ms | 1.05x | 237.59x |
105
+ | `branch_mix` | 4.38ms | 5.88ms | 438.32ms | 1.34x | 100.08x |
106
+ | `call_overhead` | 2.87ms | 2.99ms | 341.99ms | 1.04x | 119.34x |
107
+ | `fib_iterative` | 16.73ms | 16.96ms | 11.059s | 1.01x | 661.21x |
108
+ | `fib_recursive` | 161.87ms | 1.13ms | 7.278s | 0.01x | 44.96x |
109
+ | `float_math` | 6.02ms | 6.15ms | 253.87ms | 1.02x | 42.15x |
110
+ | `integer_mix` | 3.47ms | 3.45ms | 387.15ms | 0.99x | 111.46x |
111
+ | `loop_sum` | 968.71us | 1.04ms | 2.933s | 1.07x | 3027.80x |
112
+ | `matrix_mult` | 4.08ms | 4.11ms | 1.741s | 1.01x | 426.44x |
113
+ | `mem_alloc` | 960.98us | 1.00ms | 17.47ms | 1.04x | 18.18x |
114
+ | `prime_count` | 66.94ms | 79.57ms | 1.827s | 1.19x | 27.29x |
115
+ | `quicksort` | 3.69ms | 2.60ms | 29.43ms | 0.70x | 7.98x |
116
+ | `string_concat` | 19.51ms | 156.87ms | 25.83ms | 8.04x | 1.32x |
117
+
118
+ Process wall time includes startup. These are measurements from the dedicated `cpyte-official-runner-1`, not the historical benchmark figures elsewhere in this README; results can vary with host load and software versions.
119
+ <!-- CPYTE_CI_BENCHMARKS_END -->
@@ -1,4 +1,4 @@
1
- __version__ = "4.6.4"
1
+ __version__ = "4.6.7"
2
2
 
3
3
  # ---------------------------------------------------------------------------
4
4
  # Public library API.
@@ -579,25 +579,10 @@ class LLVM:
579
579
  neg_q = self.builder.neg(q)
580
580
  return neg_q
581
581
  elif is_pow2 and is_rem:
582
- # Power-of-two signed remainder: x - (q * d)
583
- k = _log2_floor(abs(d_raw))
584
582
  if isinstance(left, ir.Constant):
585
583
  l = self._norm_signed(left.constant, width)
586
584
  return ir.Constant(left.type, l % d_raw)
587
- k_const = ir.Constant(left.type, k)
588
- sign_bit = self.builder.ashr(
589
- left, ir.Constant(left.type, width - 1)
590
- )
591
- bias = self.builder.and_(
592
- sign_bit, ir.Constant(left.type, (1 << k) - 1)
593
- )
594
- adj = self.builder.add(left, bias)
595
- q = self.builder.ashr(adj, k_const)
596
- if d_raw < 0:
597
- q = self.builder.neg(q)
598
- d_val = ir.Constant(left.type, abs(d_raw))
599
- prod = self.builder.mul(q, d_val)
600
- return self.builder.sub(left, prod)
585
+ return self.builder.srem(left, right)
601
586
  else:
602
587
  # Non-power-of-two constant divisor: plain sdiv/srem. The
603
588
  # custom magic-number lowering (`_compute_signed_magic`)
@@ -644,34 +629,26 @@ class LLVM:
644
629
 
645
630
  is_zero = self.builder.icmp_signed("==", right, ir.Constant(right.type, 0))
646
631
  trap_bb = self.builder.append_basic_block("div.trap")
647
- guard_bb = self.builder.append_basic_block("div.guard")
648
- self.builder.cbranch(is_zero, trap_bb, guard_bb)
632
+ normal_bb = self.builder.append_basic_block("div.normal")
633
+ self.builder.cbranch(is_zero, trap_bb, normal_bb)
649
634
  self.builder.position_at_end(trap_bb)
650
635
  self.builder.call(self._get_trap_fn(), [])
651
636
  self.builder.unreachable()
652
637
 
653
- self.builder.position_at_end(guard_bb)
638
+ self.builder.position_at_end(normal_bb)
654
639
  is_min = self.builder.icmp_signed("==", left, ir.Constant(left.type, int_min))
655
640
  is_neg1 = self.builder.icmp_signed("==", right, ir.Constant(right.type, -1))
656
641
  overflow = self.builder.and_(is_min, is_neg1)
657
- overflow_bb = self.builder.append_basic_block("div.overflow")
658
- normal_bb = self.builder.append_basic_block("div.normal")
659
- end_bb = self.builder.append_basic_block("div.end")
660
- self.builder.cbranch(overflow, overflow_bb, normal_bb)
661
-
662
- self.builder.position_at_end(overflow_bb)
663
- self.builder.branch(end_bb)
664
- self.builder.position_at_end(normal_bb)
665
- if is_rem:
666
- normal_val = self.builder.srem(left, right)
667
- else:
668
- normal_val = self.builder.sdiv(left, right)
669
- self.builder.branch(end_bb)
670
- self.builder.position_at_end(end_bb)
671
- phi = self.builder.phi(left.type)
672
- phi.add_incoming(ir.Constant(left.type, 0 if is_rem else int_min), overflow_bb)
673
- phi.add_incoming(normal_val, normal_bb)
674
- return phi
642
+ safe_left = self.builder.select(overflow, ir.Constant(left.type, 1), left)
643
+ safe_right = self.builder.select(overflow, ir.Constant(right.type, 1), right)
644
+ normal_val = (
645
+ self.builder.srem(safe_left, safe_right)
646
+ if is_rem
647
+ else self.builder.sdiv(safe_left, safe_right)
648
+ )
649
+ return self.builder.select(
650
+ overflow, ir.Constant(left.type, 0 if is_rem else int_min), normal_val
651
+ )
675
652
 
676
653
  def _emit_floor_div(self, left, right):
677
654
  q = self._emit_int_divmod(left, right, is_rem=False)
@@ -698,6 +675,13 @@ class LLVM:
698
675
  return fn
699
676
 
700
677
  def _clamp_shift_amount(self, val, bitwidth):
678
+ if isinstance(val, ir.Constant):
679
+ try:
680
+ c = int(val.constant)
681
+ if 0 <= c < bitwidth:
682
+ return val
683
+ except (ValueError, TypeError):
684
+ pass
701
685
  zero = ir.Constant(val.type, 0)
702
686
  max_shift = ir.Constant(val.type, bitwidth - 1)
703
687
  lt_zero = self.builder.icmp_signed("<", val, zero)
@@ -1057,6 +1041,7 @@ class LLVM:
1057
1041
  gc_fns = [
1058
1042
  ("gc_init", _void, []),
1059
1043
  ("gc_malloc", _i8ptr, [_i64]),
1044
+ ("gc_array_alloc", _i8ptr, [_i64, _i64]),
1060
1045
  ("gc_write_barrier", _void, [_i8ptr, _i8ptr]),
1061
1046
  ("gc_collect", _void, []),
1062
1047
  ("gc_array_reserve", _i8ptr, [_i8ptr, _i64, _i64]),
@@ -1822,23 +1807,52 @@ class LLVM:
1822
1807
  as_i64 = self.builder.ptrtoint(val, _i64)
1823
1808
  return self.builder.call(self.functions["str_of_ptr"], [as_i64])
1824
1809
 
1825
- def _concat_strings(self, left, right):
1826
- """Concatenate two heap strings (i8*), returning a new heap string."""
1810
+ def _get_const_strlen(self, node):
1811
+ """Return the native byte length of a string literal, if known.
1812
+
1813
+ Runtime strings are UTF-8 C strings, so Python's character count is
1814
+ not sufficient for non-ASCII literals. Match ``strlen`` by stopping
1815
+ at an embedded NUL as well.
1816
+ """
1817
+ if isinstance(node, String):
1818
+ return len(node.value.encode("utf-8").split(b"\0", 1)[0])
1819
+ return None
1820
+
1821
+ def _concat_strings(self, left, right, left_len_const=None, right_len_const=None):
1822
+ """Concatenate two heap strings (i8*), returning a new heap string.
1823
+ If left_len_const or right_len_const are provided (as integers), they are
1824
+ compile-time known lengths that avoid runtime strlen calls. The copies
1825
+ use LLVM's memcpy intrinsic so LLVM can lower them directly to the
1826
+ target's native memory operation.
1827
+ """
1827
1828
  strlen_fn = self._get_strlen_fn()
1828
1829
  alloc_fn = (
1829
- self._get_malloc_fn()
1830
- if self.no_gc
1831
- else self._get_gc_uninit_alloc_fn()
1830
+ self._get_malloc_fn() if self.no_gc else self._get_gc_uninit_alloc_fn()
1832
1831
  )
1833
1832
  memcpy_fn = self._get_memcpy_fn()
1834
- left_len = self.builder.call(strlen_fn, [left])
1835
- right_len = self.builder.call(strlen_fn, [right])
1833
+
1834
+ # Use compile-time known lengths if available, otherwise call strlen
1835
+ if left_len_const is not None:
1836
+ left_len = ir.Constant(_i64, left_len_const)
1837
+ else:
1838
+ left_len = self.builder.call(strlen_fn, [left])
1839
+ if right_len_const is not None:
1840
+ right_len = ir.Constant(_i64, right_len_const)
1841
+ else:
1842
+ right_len = self.builder.call(strlen_fn, [right])
1843
+
1836
1844
  total_len = self.builder.add(left_len, right_len)
1837
1845
  plus_one = self.builder.add(total_len, ir.Constant(total_len.type, 1))
1838
1846
  new_str = self.builder.call(alloc_fn, [self.builder.zext(plus_one, _i64)])
1839
- self.builder.call(memcpy_fn, [new_str, left, left_len])
1847
+ self.builder.call(
1848
+ memcpy_fn,
1849
+ [new_str, left, left_len, ir.Constant(_i1, 0)],
1850
+ )
1840
1851
  dest_plus = self.builder.gep(new_str, [left_len], inbounds=True)
1841
- self.builder.call(memcpy_fn, [dest_plus, right, right_len])
1852
+ self.builder.call(
1853
+ memcpy_fn,
1854
+ [dest_plus, right, right_len, ir.Constant(_i1, 0)],
1855
+ )
1842
1856
  null_byte = self.builder.gep(new_str, [total_len], inbounds=True)
1843
1857
  self.builder.store(ir.Constant(_i8, 0), null_byte)
1844
1858
  return new_str
@@ -2105,7 +2119,10 @@ class LLVM:
2105
2119
 
2106
2120
  def _emit_and_or_step(self, op, node, lhs):
2107
2121
  """One short-circuit step, mirroring emit_binop's AND/OR branches."""
2108
- lhs_true = self._truthy_expr(node.left)
2122
+ # `lhs` is already the value emitted by the preceding step. Re-emitting
2123
+ # node.left here would duplicate calls, allocations, and other effects
2124
+ # when the iterative deep-expression fallback is active.
2125
+ lhs_true = lhs
2109
2126
  entry_bb = self.builder.block
2110
2127
  suffix = "and" if op == TokenType.AND else "or"
2111
2128
  rhs_bb = self.builder.append_basic_block(f"{suffix}.rhs")
@@ -2567,9 +2584,11 @@ class LLVM:
2567
2584
 
2568
2585
  # Use length-prefixed header allocation for arrays to avoid hash table lookups
2569
2586
  if node.size is not None:
2570
- alloc_fn = self.functions.get("cpyte_array_alloc")
2587
+ alloc_name = "gc_array_alloc" if not self.no_gc else "cpyte_array_alloc"
2588
+ alloc_fn = self.functions.get(alloc_name)
2571
2589
  if alloc_fn is not None and not self.no_userspace:
2572
- # Call cpyte_array_alloc(elem_size, count)
2590
+ # Call the active length-prefixed allocator. GC builds use an
2591
+ # object-backed variant so arrays are collector-managed too.
2573
2592
  if elem_size.type != _i64:
2574
2593
  elem_size = self.builder.zext(elem_size, _i64)
2575
2594
  ptr = self.builder.call(alloc_fn, [elem_size, count])
@@ -2618,7 +2637,8 @@ class LLVM:
2618
2637
  count = ir.Constant(_i64, n)
2619
2638
 
2620
2639
  # Use length-prefixed header allocation for arrays to avoid hash table lookups
2621
- alloc_fn = self.functions.get("cpyte_array_alloc")
2640
+ alloc_name = "gc_array_alloc" if not self.no_gc else "cpyte_array_alloc"
2641
+ alloc_fn = self.functions.get(alloc_name)
2622
2642
  if alloc_fn is not None and not self.no_userspace:
2623
2643
  if elem_size.type != _i64:
2624
2644
  elem_size = self.builder.zext(elem_size, _i64)
@@ -5106,6 +5126,35 @@ class LLVM:
5106
5126
  ):
5107
5127
  return self._emit_dyn_binop(node)
5108
5128
 
5129
+ # Fold the control-flow shape of logical operators when one operand is
5130
+ # already known. The unknown operand is still emitted when it can have
5131
+ # effects; only the unreachable RHS of a known short-circuit is omitted.
5132
+ if node.op in (TokenType.AND, TokenType.OR):
5133
+ left_const = _const_int_value(node.left)
5134
+ if (
5135
+ left_const is None
5136
+ and isinstance(node.left, Variable)
5137
+ and self._is_const_var(node.left.name)
5138
+ ):
5139
+ left_const = self._const_var_value(node.left.name)
5140
+ right_const = _const_int_value(node.right)
5141
+ if node.op == TokenType.AND:
5142
+ if left_const is not None and left_const == 0:
5143
+ return ir.Constant(_i1, 0)
5144
+ if left_const is not None and left_const != 0:
5145
+ return self._truthy_expr(node.right)
5146
+ if right_const is not None and right_const == 0:
5147
+ self._truthy_expr(node.left)
5148
+ return ir.Constant(_i1, 0)
5149
+ else:
5150
+ if left_const is not None and left_const != 0:
5151
+ return ir.Constant(_i1, 1)
5152
+ if left_const is not None and left_const == 0:
5153
+ return self._truthy_expr(node.right)
5154
+ if right_const is not None and right_const != 0:
5155
+ self._truthy_expr(node.left)
5156
+ return ir.Constant(_i1, 1)
5157
+
5109
5158
  match node.op:
5110
5159
  case TokenType.AND:
5111
5160
  lhs_true = self._truthy_expr(node.left)
@@ -5353,13 +5402,21 @@ class LLVM:
5353
5402
  if is_float:
5354
5403
  match node.op:
5355
5404
  case TokenType.PLUS:
5356
- return self.builder.fadd(left, right)
5405
+ res = self.builder.fadd(left, right)
5406
+ res.flags.append("contract")
5407
+ return res
5357
5408
  case TokenType.MINUS:
5358
- return self.builder.fsub(left, right)
5409
+ res = self.builder.fsub(left, right)
5410
+ res.flags.append("contract")
5411
+ return res
5359
5412
  case TokenType.STAR:
5360
- return self.builder.fmul(left, right)
5413
+ res = self.builder.fmul(left, right)
5414
+ res.flags.append("contract")
5415
+ return res
5361
5416
  case TokenType.SLASH:
5362
- return self.builder.fdiv(left, right)
5417
+ res = self.builder.fdiv(left, right)
5418
+ res.flags.append("contract")
5419
+ return res
5363
5420
  case TokenType.SLASH_SLASH:
5364
5421
  div = self.builder.fdiv(left, right)
5365
5422
  return self.builder.call(self._get_floor_fn(), [div])
@@ -5532,9 +5589,17 @@ class LLVM:
5532
5589
  return self.builder.load(out_ptr)
5533
5590
 
5534
5591
  def _emit_string_concat(self, node):
5592
+ # Keep operands in source order and emit each one exactly once.
5593
+ right_len = self._get_const_strlen(node.right)
5594
+ left_len = self._get_const_strlen(node.left)
5595
+
5535
5596
  left = self.emit(node.left)
5536
- right = self.emit(node.right)
5537
- return self._concat_strings(left, right)
5597
+ right = (
5598
+ self._string_const(node.right.value)
5599
+ if isinstance(node.right, String)
5600
+ else self.emit(node.right)
5601
+ )
5602
+ return self._concat_strings(left, right, left_len, right_len)
5538
5603
 
5539
5604
  def _get_strlen_fn(self):
5540
5605
  fn = self._strlen_fn
@@ -5554,15 +5619,20 @@ class LLVM:
5554
5619
  fn = self._memcpy_fn
5555
5620
  if fn is not None:
5556
5621
  return fn
5622
+ # These are compiler-owned copies. Use LLVM's intrinsic instead of
5623
+ # calling a user-space wrapper or libc symbol so the backend can inline
5624
+ # and vectorize them for the selected CPU.
5625
+ fn = self.module.declare_intrinsic("llvm.memcpy", [_i8ptr, _i8ptr, _i64])
5626
+ self._memcpy_fn = fn
5627
+ return fn
5628
+
5629
+ def _get_str_grow_fn(self):
5630
+ fn_name = "cpy_str_grow" if self.no_gc else "cpy_str_grow_gc"
5557
5631
  for f in self.module.functions:
5558
- if f.name == "memcpy":
5559
- self._memcpy_fn = f
5632
+ if f.name == fn_name:
5560
5633
  return f
5561
- # n is size_t (i64 on every cpyte target).
5562
5634
  fnty = ir.FunctionType(_i8ptr, [_i8ptr, _i8ptr, _i64])
5563
- fn = ir.Function(self.module, fnty, "memcpy")
5564
- self._memcpy_fn = fn
5565
- return fn
5635
+ return ir.Function(self.module, fnty, fn_name)
5566
5636
 
5567
5637
  @register_emitter(UnaryOp)
5568
5638
  def emit_unaryop(self, node: UnaryOp):
@@ -5940,6 +6010,31 @@ class LLVM:
5940
6010
 
5941
6011
  ptr = self.locals.get(name)
5942
6012
  if ptr is not None:
6013
+ if (
6014
+ (self.local_types.get(name) == "str" or getattr(node.target, "inferred_type", None) == "str")
6015
+ and isinstance(node.value, BinOp)
6016
+ and node.value.op == TokenType.PLUS
6017
+ and isinstance(node.value.left, Variable)
6018
+ and node.value.left.name == name
6019
+ ):
6020
+ s_val = self.builder.load(ptr)
6021
+ right_node = node.value.right
6022
+ right_const_len = self._get_const_strlen(right_node)
6023
+ right_val = (
6024
+ self._string_const(right_node.value)
6025
+ if isinstance(right_node, String)
6026
+ else self.emit(right_node)
6027
+ )
6028
+ if right_val.type != _i8ptr:
6029
+ right_val = self.builder.bitcast(right_val, _i8ptr)
6030
+ if right_const_len is not None:
6031
+ right_len_val = ir.Constant(_i64, right_const_len)
6032
+ else:
6033
+ right_len_val = self.builder.call(self._get_strlen_fn(), [right_val])
6034
+ grow_fn = self._get_str_grow_fn()
6035
+ new_str = self.builder.call(grow_fn, [s_val, right_val, right_len_val])
6036
+ self.builder.store(new_str, ptr)
6037
+ return None
5943
6038
  if self._is_dynamic_expr(node.value):
5944
6039
  value = self._unbox_dyn_to(
5945
6040
  node.value, self.local_types.get(name) or "int"
@@ -6554,7 +6649,8 @@ class LLVM:
6554
6649
 
6555
6650
  malloc_fn = self._get_malloc_fn()
6556
6651
  eight = ir.Constant(_i64, 8)
6557
- alloc_fn = self.functions.get("cpyte_array_alloc")
6652
+ alloc_name = "gc_array_alloc" if not self.no_gc else "cpyte_array_alloc"
6653
+ alloc_fn = self.functions.get(alloc_name)
6558
6654
  if alloc_fn is not None and not self.no_userspace:
6559
6655
  buf = self.builder.call(alloc_fn, [eight, count])
6560
6656
  else:
@@ -6934,6 +7030,11 @@ class LLVM:
6934
7030
  with self.builder.if_then(moved):
6935
7031
  inner_dv = self.builder.insert_value(cur, new_data, 1)
6936
7032
  self.builder.store(inner_dv, slot)
7033
+ if not self.no_gc:
7034
+ self._emit_write_barrier(
7035
+ self.builder.bitcast(slot, _i8ptr),
7036
+ self.builder.bitcast(new_ptr, _i8ptr),
7037
+ )
6937
7038
  value_rv = self.emit(value)
6938
7039
  if value_rv.type == _DynValue:
6939
7040
  elem = value_rv
@@ -6995,6 +7096,11 @@ class LLVM:
6995
7096
  with self.builder.if_then(moved):
6996
7097
  slot_elem_ty = slot.type.pointee
6997
7098
  self.builder.store(self.builder.bitcast(new_ptr, slot_elem_ty), slot)
7099
+ if not self.no_gc:
7100
+ self._emit_write_barrier(
7101
+ self.builder.bitcast(slot, _i8ptr),
7102
+ self.builder.bitcast(new_ptr, _i8ptr),
7103
+ )
6998
7104
  value_rv = self.emit(value)
6999
7105
  if elem_llvm == _DynValue and value_rv.type != _DynValue:
7000
7106
  kind, bits = self._box_dyn(value_rv, getattr(value, "inferred_type", None))
@@ -7709,8 +7815,9 @@ class LLVM:
7709
7815
  def _string_const(self, val: str):
7710
7816
  val = val + "\0"
7711
7817
  if val not in self.string_pool:
7712
- arr_ty = ir.ArrayType(ir.IntType(8), len(val))
7713
- init = ir.Constant(arr_ty, bytearray(val.encode("utf-8")))
7818
+ encoded = val.encode("utf-8")
7819
+ arr_ty = ir.ArrayType(ir.IntType(8), len(encoded))
7820
+ init = ir.Constant(arr_ty, bytearray(encoded))
7714
7821
  name = f".str.{self.string_id}"
7715
7822
  self.string_id += 1
7716
7823
  gv = ir.GlobalVariable(self.module, arr_ty, name=name)
@@ -417,7 +417,7 @@ def host_cpu_features():
417
417
  return cpu, features
418
418
 
419
419
 
420
- def make_target_machine(pic=False, cpu=None, features=None, codemodel=None):
420
+ def make_target_machine(pic=False, cpu=None, features=None, codemodel=None, opt=3):
421
421
  """Build a :class:`llvmlite.binding.TargetMachine` for the host (or an
422
422
  explicit cpu/features override) with SIMD enabled.
423
423
 
@@ -457,6 +457,8 @@ def make_target_machine(pic=False, cpu=None, features=None, codemodel=None):
457
457
  kwargs["features"] = features
458
458
  if codemodel:
459
459
  kwargs["codemodel"] = codemodel
460
+ if opt is not None:
461
+ kwargs["opt"] = opt
460
462
  return target.create_target_machine(**kwargs)
461
463
 
462
464
 
@@ -876,7 +878,7 @@ def optimize(mod, opt_level=3, opt_size=False):
876
878
  binding.initialize_native_asmprinter()
877
879
  if opt_level <= 0 and not opt_size:
878
880
  return
879
- target_machine = make_target_machine()
881
+ target_machine = make_target_machine(opt=0 if opt_level <= 0 else opt_level)
880
882
 
881
883
  if opt_size:
882
884
  # -OSize: optimize purely for code size, completely ignoring speed.
@@ -961,13 +963,25 @@ def optimize(mod, opt_level=3, opt_size=False):
961
963
  mpm.add_constant_merge_pass()
962
964
  mpm.add_global_dead_code_eliminate_pass()
963
965
  mpm.add_dead_arg_elimination_pass()
966
+ mpm.add_always_inliner_pass()
967
+ mpm.add_partial_inliner_pass()
964
968
  if extra_heavy:
965
969
  mpm.add_post_order_function_attributes_pass()
966
970
  mpm.add_aggressive_instcombine_pass()
967
- mpm.add_always_inliner_pass()
968
- mpm.add_partial_inliner_pass()
969
971
  mpm.add_merge_functions_pass()
970
972
  mpm.run(mod, pb)
973
+
974
+ # Cleanup SSA & dead code after inlining
975
+ fpm_post = pb.getFunctionPassManager()
976
+ fpm_post.add_sroa_pass()
977
+ fpm_post.add_instruction_combine_pass()
978
+ fpm_post.add_simplify_cfg_pass()
979
+ fpm_post.add_new_gvn_pass()
980
+ fpm_post.add_dead_store_elimination_pass()
981
+ fpm_post.add_aggressive_dce_pass()
982
+ for fn in mod.functions:
983
+ if not fn.is_declaration:
984
+ fpm_post.run(fn, pb)
971
985
  elif opt_size:
972
986
  # Size-oriented module phase: coalesce constants, eliminate dead globals
973
987
  # and arguments, merge identical function bodies, drop unused prototypes.
@@ -1930,7 +1944,7 @@ def run_aot(
1930
1944
  optimize(mod, opt_level)
1931
1945
  mod.verify()
1932
1946
 
1933
- target_machine = make_target_machine(pic=pic, codemodel="small")
1947
+ target_machine = make_target_machine(pic=pic, codemodel="small", opt=opt_level)
1934
1948
 
1935
1949
  obj = target_machine.emit_object(mod)
1936
1950