Skip to content

Merge upstream changes with comprehensive conflict resolution#36

Merged
mengzhuo merged 1 commit intoplctlab:mainfrom
DzmingLi:main
Sep 19, 2025
Merged

Merge upstream changes with comprehensive conflict resolution#36
mengzhuo merged 1 commit intoplctlab:mainfrom
DzmingLi:main

Conversation

@DzmingLi
Copy link

This pull request merges the latest features, performance optimizations, and architectural improvements from the upstream branch while carefully preserving and integrating PLCTLab's unique enhancements, such as RISC-V backend support, robust error handling, and advanced trait analysis.

Major Architectural Upgrades & Feature Enhancements

1. Pattern Matching Engine Overhaul (check_match.ml)

  • Adopted Upstream Architecture: The upstream version provides a complete architectural rewrite of the pattern matching engine, offering a cleaner structure and more powerful features.
  • New Features:
    • Optimization for string array patterns (try_combine_array_pat_to_string).
    • Support for Texpr_guard_let and Texpr_is expressions.
    • Better separation of concerns: analyze_cases for analysis and check_match for reporting.
  • Benefit: This provides a functional superset, enhancing all of PLCTLab's original functionality while adding support for new language constructs.

2. Hybrid Compilation Architecture (driver_util.ml, moon0_main.ml)

  • Dual-Target Support Preserved: A hybrid compilation pipeline was created to preserve both PLCTLab's RISC-V backend (riscv_gen) and upstream's unified WASM backend (wasm_gen with dwarfsm_callback).
  • Modernized Package Management: Adopted upstream's package table system for better package isolation and management.
  • Backward-Compatible CLI: Retained the wat_gen function for text format (.wat) output while integrating dwarfsm_callback for modern binary (.wasm) output.
  • Benefit: This approach adopts the modern upstream WASM pipeline and Shrink_wasmir optimizer without losing critical RISC-V target support.

3. Trait Analysis System Integration (placeholder_env.ml, toplevel_typer.ml)

  • Hybrid Trait Analysis: Fully preserved PLCTLab's powerful trait object safety analysis and compile-time processing for foreign traits.
  • Integrated Upstream Enhancements: Seamlessly integrated upstream's trait alias support and handling for new types like Ptd_extern and Ptype_object.
  • Benefit: Achieved zero functionality loss while gaining all trait-related features from both branches, maximizing functionality.

4. Dependency Analysis Algorithm Upgrade (topo_sort.ml)

  • Adopted Superior Upstream Implementation: The upstream version uses an efficient set-based algorithm and performs a comprehensive dependency analysis on entire implementations, not just expressions.
  • Functional Enhancements:
    • More efficient variable tracking.
    • More robust self-reference and dependency cycle detection.
    • Compatibility with the modern type system, including trait_alias.
  • Benefit: Gained a more accurate and efficient topological sorting algorithm capable of handling more complex code structures.

5. Async Function Support (parsing_ast_lint.ml)

  • Adopted Upstream Implementation: Added full async support to the function desugaring process.
  • Guard Expression Support: Pattern matching now supports guard conditions, enhancing the language's expressive power.
  • Benefit: Aligns the compiler with the latest MoonBit language features, especially the asynchronous programming model.

Performance Optimizations

  • List Operations (basic_lst.ml): Adopted upstream's highly optimized implementations for core list functions (e.g., map, fold_right, map2, iteri). These implementations leverage techniques like special casing for small lists (1-10 elements), tail-recursion optimization, and chunked processing to significantly improve performance while maintaining API compatibility. PLCTLab's unique functions like group and stable_group were preserved.

Robustness, Correctness & Error Handling

  • File I/O Error Handling (core_format.ml, parsing_parse.ml): Kept PLCTLab's comprehensive try...with Sys_error pattern, which is more robust than upstream's file existence checks as it handles permissions, disk errors, and other scenarios while providing more detailed error messages.
  • DWARF Debug Information (dwarfsm_encode_wasm.ml): Preserved PLCTLab's tracking of low_pc and high_pc, ensuring the integrity of DWARF debugging information, which is essential for debugging.
  • Package Import System (pkg.ml): Adopted upstream's safer import mechanism, which detects and prevents name collisions between imported and local definitions, improving code reliability.
  • Label Semantics (label.ml): Maintained PLCTLab's safer approach to label comparison/hashing, which considers both name and stamp fields, making it more robust than upstream's stamp-only method.
  • Type System Completeness (mtype.ml, name_mangle.ml): Ensured that all types (including new additions like T_int16, T_uint16, T_raw_func) are handled correctly during serialization and name mangling, preventing potential runtime errors from incomplete pattern matches.

Code Quality & Consistency

  • Code Style Unification: In multiple modules (e.g., basic_core_ident.ml, object_util.ml, trait_impl.ml), preferred PLCTLab's clean, manually written implementations over verbose, auto-generated code from upstream for better readability.
  • Code Deduplication (typer.ml): Eliminated over 270 lines of duplicated code (for record updates, field access, etc.) in PLCTLab's assignment operator logic, as the functionality already existed elsewhere in the file.
  • Parameter Order Correction (basic_lst.ml): Corrected the parameter order of the take function from take n xs to take xs n to align with all other call sites in the project and OCaml community conventions. All relevant call sites were updated.
  • API Modernization (mcore_util.ml, monofy_worklist.ml): Standardized the use of the internal Lst module and adopted modern function signature styles (e.g., labeled arguments ~f:), improving code consistency and readability across the project.

Resolved 50+ merge conflicts systematically, preserving all PLCTLab functionality while adopting upstream enhancements.

## Key Conflict Resolutions:

**Type System**
- Extended support for T_int16, T_uint16, T_raw_func, T_local types
- Integrated trait alias support with PLCTLab's object safety analysis

**Compilation Architecture**
- Hybrid RISC-V (PLCTLab) + modern WASM (upstream) pipeline support
- Combined clam_callback and dwarfsm_callback systems
- Target support: wasm-gc, wasm, js, riscv, native, llvm

**Performance Optimizations**
- Adopted upstream's optimized list operations (special cases for small lists)
- Upgraded topological sorting to comprehensive set-based dependency analysis
- Enhanced pattern matching with async and guard expression support

**Code Quality**
- Preserved PLCTLab's clean manual implementations over auto-generated code
- Maintained robust error handling patterns
- Ensured backward compatibility for CLI interfaces

## Critical Integrations:
- Foreign trait compile-time processing (placeholder_env.ml + toplevel_typer.ml)
- Pattern matching architectural rewrite (check_match.ml)
- Function desugaring with async support (parsing_ast_lint.ml)
@mengzhuo mengzhuo merged commit f39bce3 into plctlab:main Sep 19, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants