Commit Graph

631 Commits

Author SHA1 Message Date
13298126c8 fix(llvm): MapBox core-first implementation with plugin fallback by ChatGPT
Implemented elegant solution for MapBox as core box with plugin fallback:

1. Core-first Strategy:
   - Removed MapBox type_id from nyash_box.toml
   - MapBox now uses env.box.new fallback (core implementation)
   - Consistent with self-hosting goals

2. Plugin Fallback Option:
   - Added NYASH_LLVM_FORCE_PLUGIN_MAP=1 environment variable
   - Allows forcing MapBox to plugin path when needed
   - Preserves flexibility during transition

3. MIR Type Inference:
   - Added MapBox method type inference (size/has/get)
   - Ensures proper return type handling

4. Documentation:
   - Added core vs plugin box explanation in nyrt
   - Clarified the transition strategy

This aligns with Phase 15 goals where basic boxes will eventually
be implemented in Nyash itself for true self-hosting.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-11 23:09:16 +09:00
89e6fbf010 feat(llvm): Comprehensive LLVM backend improvements by ChatGPT
Major enhancements to LLVM code generation and type handling:

1. String Operations:
   - Added StringBox length fast-path (length/len methods)
   - Converts i8* to handle when needed for len_h call
   - Consistent handle-based string operations

2. Array/Map Fast-paths:
   - ArrayBox: get/set/push/length operations
   - MapBox: get/set/has/size with handle-based keys
   - Optimized paths for common collection operations

3. Field Access:
   - getField/setField implementation with handle conversion
   - Proper i64 handle to pointer conversions

4. NewBox Improvements:
   - StringBox/IntegerBox pass-through optimizations
   - Fallback to env.box.new when type_id unavailable
   - Support for dynamic box creation

5. Documentation:
   - Added ARCHITECTURE.md for overall design
   - Added EXTERNCALL.md for external call specs
   - Added LOWERING_LLVM.md for LLVM lowering rules
   - Added PLUGIN_ABI.md for plugin interface

6. Type System:
   - Added UserBox type registration in nyash_box.toml
   - Consistent handle (i64) representation across system

Results: More robust LLVM code generation with proper type handling

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-11 22:30:26 +09:00
82cd2affbc docs: Add architecture decision to CLAUDE.md - Box/ExternCall boundary design summary 2025-09-11 21:42:40 +09:00
0ac22427e5 docs: Architecture decision - Box/ExternCall boundary design
Documented the architectural decision for Nyash runtime design:

1. Core boxes (String/Integer/Array/Map/Bool) built into nyrt
   - Essential for self-hosting
   - Available at boot without plugin loader
   - High performance (no FFI overhead)

2. All other boxes as plugins (File/Net/User-defined)
   - Extensible ecosystem
   - Clear separation of concerns

3. Minimal ExternCall (only 5 functions)
   - print/error (output)
   - panic/exit (process control)
   - now (time)

Key principle: Everything goes through BoxCall interface
- No special fast paths
- Unified architecture
- "Everything is Box" philosophy maintained

This design balances self-hosting requirements with architectural purity.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-11 20:58:18 +09:00
e7ad2191de fix(llvm): String/Plugin return value handling improvements by ChatGPT
Major fixes for LLVM backend string and plugin return value handling:

1. MIR Type Annotations:
   - Added StringBox method return types (substring/concat/replace/trim/toUpper/toLower)
   - Enhanced type inference for BoxCall operations

2. LLVM BinOp String Concatenation:
   - Added safe handle-to-pointer conversion paths
   - Support for ptr+i64 and i64+ptr concatenation patterns
   - Uses nyash.string.concat_hh for handle-based concatenation

3. ExternCall Selection:
   - Smart selection between C string (i8*) and handle (i64) variants
   - Improved print/log function selection based on argument types

4. StringBox Fast-path Optimization:
   - Direct AOT concatenation for StringBox.concat
   - Bypasses plugin path for better performance

5. Consistent String Representation:
   - AOT uses i8* (C string) as primary representation
   - Handles used for print/concat auxiliary paths

6. Build Fix:
   - Removed duplicate plugin.rs to resolve nyrt build conflicts

Results: LLVM plugin return smoke tests now pass (NYASH_LLVM_PLUGIN_RET_SMOKE=1)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-11 20:18:53 +09:00
b9f9e81c72 refactor(llvm): Major progress on codegen modularization by ChatGPT
Major changes:
- Removed 617 lines of duplicate/legacy code from mod.rs (lines 351-967)
- All BoxCall handling now properly delegated to instructions::lower_boxcall
- Updated CURRENT_TASK.md with new findings:
  - String concatenation issue (BinOp type mismatch)
  - Plugin return value smoke test added
  - Clear next steps for fixing return value display

Key improvements:
- Clean separation between dispatch (mod.rs) and implementation (instructions.rs)
- Legacy code marked as unreachable and ready for removal
- Better error visibility with modularized code structure
- llvm_smoke.sh updated with new plugin return value tests

Next steps:
1. Fix BinOp string concatenation type handling
2. Investigate MIR value_types for BoxCall returns
3. Further split lower_boxcall function (still 260+ lines)
2025-09-11 18:16:08 +09:00
89dd518408 refactor(llvm): Further modularization progress by ChatGPT
- BoxCall handling now properly delegated to instructions::lower_boxcall
- Removed duplicate code in mod.rs (lines 351+ were unreachable after continue)
- Clean separation between dispatch (mod.rs) and implementation (instructions.rs)
- Preparing for further BoxCall function breakdown

Work in progress - ChatGPT continuing refactoring efforts
2025-09-11 17:59:51 +09:00
1fd37bf14a refactor(llvm): Complete modularization of codegen.rs by Codex
- Split 2522-line codegen.rs into modular structure:
  - mod.rs (1330 lines) - main compilation flow and instruction dispatch
  - instructions.rs (1266 lines) - all MIR instruction implementations
  - types.rs (189 lines) - type conversion and classification helpers
  - helpers.rs retained for shared utilities

- Preserved all functionality including:
  - Plugin return value handling (BoxCall/ExternCall)
  - Handle-to-pointer conversions for proper value display
  - Type-aware return value processing based on MIR metadata
  - All optimization paths (ArrayBox fast-paths, string concat, etc.)

- Benefits:
  - Better code organization and maintainability
  - Easier to locate specific functionality
  - Reduced cognitive load when working on specific features
  - Cleaner separation of concerns

No functional changes - pure refactoring to improve code structure.
2025-09-11 17:51:43 +09:00
335aebb041 🏗️ Refactor: Split massive codegen.rs (2522 lines) into modular structure
Thanks to Codex's powerful refactoring\!
- codegen.rs → codegen/ directory with 3 focused modules
- mod.rs (1498 lines) - main compilation flow
- instructions.rs (1121 lines) - MIR instruction implementations
- types.rs (189 lines) - type conversion helpers

Benefits:
- Much easier to locate errors and debug
- Better separation of concerns
- Enables parallel development
- Maintains API compatibility

Co-authored-by: Codex <codex@openai.com>
2025-09-11 17:34:30 +09:00
faf79c5d52 🔧 Fix all unused_mut warnings: 16 instances cleaned up
- Remove unnecessary 'mut' from variable declarations
- Clean up code in boxes/, interpreter/, mir/, backend/, and runtime/
- No functional changes, just cleaner code
2025-09-11 16:34:22 +09:00
ba33431f02 🎯 Complete ptr_type deprecation fixes: 200+ → 96 warnings (53% reduction!)
- Fix all deprecated ptr_type() warnings (30+ instances)
- LLVM 15.0 migration: Type::ptr_type() → Context::ptr_type()
- Remove unused i64p variables in codegen.rs and helpers.rs
- Clean up remaining unused imports

Significant milestone achieved in warning cleanup campaign!
2025-09-11 16:30:32 +09:00
c6a8d0b686 🧹 Major warning cleanup: 200+ → 102 warnings (50% reduction)
- Fix TokenType enum naming convention (SNAKE_CASE → CamelCase)
- Remove 60+ unused imports across multiple modules
- Clean up interpreter, backend, and box modules
- LLVM build now passes with significantly fewer warnings
2025-09-11 16:24:18 +09:00
d72015c5c1 Merge pull request #141 - restore LLVM codegen implementation 2025-09-11 12:27:07 +09:00
94156251a5 llvm: restore codegen and interpreter pipeline 2025-09-11 12:14:11 +09:00
d66071e474 llvm: refactor compiler into aot, codegen, interpreter, helpers (#140) 2025-09-11 11:51:06 +09:00
93e571103f fix(llvm): Implement handle-based console.log in MIR interpreter
- MIR interpreter now converts NyashBox to handles for console methods
- Added debug output to trace handle conversion
- Fixed plugin return values not being displayed
- Part of ongoing investigation into LLVM backend plugin issues

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-11 11:38:08 +09:00
a762a3535e nyrt: split plugin module (#139) 2025-09-11 11:19:55 +09:00
f441d84578 nyrt: split lib into modules (#138) 2025-09-11 06:35:36 +09:00
40489a3c97 feat(debug): Add debug logging to console.log_handle for plugin investigation
- Added eprintln! debug messages to trace handle values
- Helps investigate why plugin return values display as blank
- Part of ongoing LLVM backend plugin return value investigation

Related to issue where print(c.get()) shows blank output

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-11 06:03:21 +09:00
4ecba4950f feat(llvm): Complete compiler modularization refactoring (PR #136)
- Split large compiler.rs into modular components
- Extract plugin signature loading to plugin_sigs.rs
- Extract box type ID loading to box_types.rs
- Preserve all PR #134 type information handling
- Update CURRENT_TASK.md with refactoring completion
2025-09-11 05:23:58 +09:00
c4e1728b8e refactor: centralize box type metadata 2025-09-11 05:22:52 +09:00
f124731764 runtime: print raw values for console handle shims (#135) 2025-09-11 03:54:35 +09:00
4201c63001 mir: read plugin method signatures from nyash_box (#134)
* mir: load plugin method signatures

* llvm: read box type ids from nyash_box

* mir: show value types in MIR printer
2025-09-11 03:33:33 +09:00
e114f9bfe3 fix(llvm): Implement handle-based console.log functions for plugin return values
- Add nyash.console.log_handle(i64) -> i64 family functions to nyrt
- Replace invalid int-to-pointer conversion with proper handle-based calls
- Fix bool(i1) -> i64 type conversion in LLVM compiler
- Resolve LLVM function verification errors
- Enable plugin method execution without NYASH_LLVM_ALLOW_BY_NAME
- Merge codex TLV fixes for plugin return value handling (2000+ lines)

Technical Details:
- Root cause: build_int_to_ptr(handle_value, i8*, "arg_i2p") treated
  handle IDs as memory addresses (invalid operation)
- Solution: Direct i64 handle passing to nyrt functions with proper
  handle registry lookup and to_string_box() conversion
- Type safety: Added proper i1/i32/i64 -> i64 conversion handling

Status: Console.log type errors resolved, plugin return value display
still under investigation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-11 00:21:11 +09:00
5dd3227a1a nyrt: handle plugin return primitives 2025-09-10 23:46:53 +09:00
c014e78fb4 feat(llvm): Complete plugin system unification and environment variable elimination
🎉 Major Achievement: LLVM Plugin Environment Variable Problem Completely Resolved

##  Completed Major Features:
1. **Plugin Implementation**  - nyash.plugin.invoke_* functions in nyrt library working
2. **Plugin Calls**  - Method calls working without environment variables
3. **Return Value Type Inference Fix**  - Added plugin method type inference to MIR builder
4. **by-id Unification Complete**  - Removed by-name fallback, unified to method_id system
5. **Environment Variable Elimination**  - Removed NYASH_LLVM_ALLOW_BY_NAME=1 requirement
6. **Simple Execution Achieved**  - ./target/release/nyash --backend llvm program.nyash

## 🔧 Technical Changes:

### Core Fixes:
- **src/mir/builder.rs**: Added plugin method return type inference
  - CounterBox.get() -> Integer
  - MathBox.sqrt() -> Float
  - FileBox.read() -> String
  - FileBox.exists() -> Bool

- **src/backend/llvm/compiler.rs**: Removed by-name fallback completely
  - Deleted NYASH_LLVM_ALLOW_BY_NAME environment variable check
  - Removed ~50 lines of fallback logic
  - Unified to method_id-based calls only

### Documentation Updates:
- **CLAUDE.md**: Updated all plugin examples to remove environment variables
- **README.md/README.ja.md**: Removed environment variable documentation
- **tools/llvm_smoke.sh**: Removed NYASH_LLVM_ALLOW_BY_NAME from all test scripts

### Performance & Maintainability:
- **Better Performance**: method_id calls more efficient than by-name lookups
- **Type Safety**: method_id system provides compile-time guarantees
- **Code Simplification**: Removed complex fallback logic
- **User Experience**: No environment variables to remember

## 🧪 Verification:
-  Plugin execution without environment variables
-  method_id injection working: [LLVM] method_id injected: 4-5 places
-  Type inference working: [BUILDER] Type inference: CounterBox get -> Integer
-  Compilation success with LLVM backend

## 🔍 Remaining Investigation:
Plugin return value display issue identified as separate runtime layer problem
(plugin methods execute and side effects work, but return values not displayed in print())

🚀 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-10 23:24:02 +09:00
3babd8f69c filebox: add path-based I/O and exists 2025-09-10 21:15:50 +09:00
7fc5fef5cc Apply ChatGPT's FileBox method_id fixes and add build scripts
- Add plugin host initialization for LLVM mode (fixes method_id injection)
- Add FileBox method_id injection test
- Enhance MIR14 stability test
- Add warning for Mock LLVM implementation
- Create build scripts for JIT/LLVM with proper settings (24 threads, timeout)
- Update CLAUDE.md with build instructions and FileBox test results

Note: FileBox file I/O still not working in LLVM execution (separate issue)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-10 20:56:14 +09:00
92bbfd90bc Add Gemini compiler discussion summary
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-10 19:58:58 +09:00
1b5a55c795 mir: include continue snapshots in phi sealing (#130) 2025-09-10 17:39:46 +09:00
06c77c1026 chore(mir): prune legacy instruction sets (#129) 2025-09-10 17:23:26 +09:00
370e725926 docs: Update current task and loop builder SSA bug details
- Update dates to 2025-09-08
- Document loop builder SSA scope management issue
- Add notes about continue/break implementation status
- Fix birth/init consistency in CLAUDE.md
2025-09-09 09:31:39 +09:00
62246c76e3 docs(current_task): add structured tasks under docs/current_task/{main,llvm,self_current_task} and unignore path 2025-09-08 11:35:30 +09:00
a45c22891c selfhosting-dev: finalize local changes post-rebase abort (dep_tree tools, runner, grammar); keep tmp deps json in-tree 2025-09-08 11:20:13 +09:00
f22082f67c resolve: apply stashed using/module + deps bridge; remove conflict markers in runner/mod.rs 2025-09-08 04:35:50 +09:00
da5fa600d2 merge: selfhosting-dev <- origin/main; prefer main updates in cranelift builder (ARROW removal/SHR adoption) 2025-09-08 04:33:50 +09:00
17225c29f7 tests: add parser/vm bitops tests; docs: update cheatsheet and language guide for bitwise ops and shift; note Arrow(>>) removal
- Add src/tests/parser_bitops_test.rs and vm_bitops_test.rs
- Update tokenizer unit test to expect SHIFT_RIGHT
- Update quick-reference and language guide to document &,|,^,<<,>> and Arrow deprecation

Known: one unrelated test failing (consolebox println TLV vs typebox) pre-existing.
2025-09-08 04:04:19 +09:00
08d9b71297 grammar: introduce bitwise ops & shifts; retire legacy >> ARROW
- Tokenizer: add tokens for << >> & | ^ (SHIFT_LEFT/RIGHT, BIT_AND/OR/XOR); keep ||, &&, |>
- Parser: precedence layers for bit ops (| ^ &) and shift (<< >>); remove >> Arrow production
- AST: add BitAnd/BitOr/BitXor/Shr with Display
- MIR builder: map bit ops and Shr to MIR BinaryOp
- Interpreter: implement integer-only bit ops and shifts, mask shift count to 0..63

Compatibility: legacy >> Arrow removed from parser; use |> for pipeline.
2025-09-08 03:54:34 +09:00
7c2b09c647 grammar: add '<<' shift-left safely (Phase 1); map to AST/MIR/VM; keep '>>' as legacy ARROW
- Tokenizer: add SHIFT_LEFT for '<<' (before <= detection)
- Parser: introduce parse_shift() layer; left-associative; only '<<' handled in Phase 1
- AST: add BinaryOperator::Shl and display
- MIR builder: map Shl -> BinaryOp::Shl
- Interpreter: implement Shl in both execution paths (integer-only, type error otherwise)

No change to '>>' (legacy ARROW remains). No runtime gate yet for SHL as it does not collide with existing syntax.
2025-09-08 03:44:55 +09:00
2578120b23 docs(phases): restore Phase 18/19 from history (3826089)\n\n- Phase 18: LifeBox principles and LoopForm design doc\n- Phase 19: LoopForm <-> Core-13 PoC and zero-cost CI plan\n\nRecovered from commit 3826089; reintroduce missing directories under docs/development/roadmap/phases/ 2025-09-08 03:30:35 +09:00
10d6b3059a llvm: inline env.box.new_i64x arg conversion; add bitops tests/smoke; update CURRENT_TASK
- Inline-coerce env.box.new_i64x args to i64 handles (int passthrough, f64 via nyash.box.from_f64, i8* via nyash.box.from_i8_string). Removes closure that caused builder lifetime/borrow issues.
- Add unit test for bitwise/shift ops (VM=48; LLVM emit ok; compile_and_execute returns 48).
- Extend tools/llvm_smoke.sh with optional NYASH_LLVM_BITOPS_SMOKE gate; add apps/tests/ny-llvm-bitops (parser currently lacks &|^<<>> so E2E gated).
- Update CURRENT_TASK.md to reflect P1 progress and test strategy.

Build/test:
- LLVM build: LLVM_SYS_180_PREFIX=/usr/lib/llvm-18 cargo build --release --features llvm
- Unit: cargo test --no-run (env-dependent to run)
- Smoke (optional): NYASH_LLVM_BITOPS_SMOKE=1 ./tools/llvm_smoke.sh
2025-09-08 03:27:52 +09:00
6a80ce6c65 docs(current_task): restart notes for Ny syntax alignment and next steps 2025-09-08 02:24:17 +09:00
29e2973a72 docs(current_task): prepend quick self-host plan (dep-tree + bridge) for restart context 2025-09-08 02:15:33 +09:00
f8beebc456 docs(current_task): add self-host dep-tree plan; tasks: add dep_tree; Makefile: add dep-tree target 2025-09-08 01:42:50 +09:00
df88296d97 Merge branch 'main' of https://github.com/moe-charm/nyash 2025-09-08 01:41:05 +09:00
193050cba3 fix: finalize extern registry and docs dir after merge 2025-09-08 01:39:58 +09:00
df21e9e137 runner: align mod.rs with cranelift-dev (remove using/modules flow, JSON v0 via MIR interpreter) 2025-09-08 01:30:59 +09:00
864473336e merge: resolve conflicts (prefer cranelift-dev for Core-13 defaults; drop modules/using, add env.local/env.box shims) 2025-09-08 01:28:39 +09:00
1aad0479e7 merge: bring cranelift-dev into selfhosting-dev (VM stable, AOT/JIT smokes passing) 2025-09-08 01:24:13 +09:00
5ab3cc6688 vm: stabilize plugin-first VM output + TLV decode\n\n- runtime(plugin_loader_v2): decode plugin TLV generically (tag 1/2/3/5/6/7/8/9)\n- vm(print): coerce plugin-backed results via semantics (i64/string) for consistent output\n- vm(plugin_invoke): add decode debug; align return handling\n- bin/lib: expose runner_plugin_init in bin; fix call sites to init plugin host\n- tools(build_aot.sh): disable legacy-forbid diag and toml env for object emit\n- add VM/JIT smoke scripts (counter, filebox, jit-compare)\n\nResult: VM plugin smokes PASS; jit-direct compare PASS. AOT emit still needs CLIF block init tweak. 2025-09-08 01:08:59 +09:00