// LLVMV0BuilderBox — v0: 最小 MIR(JSON v0) を直接生成する軽量ビルダ（依存レス）
// 目的: スモークでの AOT 経路確認を最小依存で実現する。

static box LLVMV0BuilderBox {
  _q(s){ return "\"" + s + "\"" }
  _i(n){ return "" + n }
  _inst_const(dst, val){
    return "{\"op\":\"const\",\"dst\":" + me._i(dst) + ",\"value\":{\"type\":\"i64\",\"value\":" + me._i(val) + "}}"
  }
  _inst_ret(val){ return "{\"op\":\"ret\",\"value\":" + me._i(val) + "}" }
  _map_binop_kind(opk){
    if opk == "+" { return "Add" }
    if opk == "-" { return "Sub" }
    if opk == "*" { return "Mul" }
    if opk == "/" { return "Div" }
    if opk == "%" { return "Mod" }
    return opk
  }
  _inst_binop(kind, lhs, rhs, dst){
    return "{\"op\":\"binop\",\"op_kind\":" + me._q(kind) + ",\"lhs\":" + me._i(lhs) + ",\"rhs\":" + me._i(rhs) + ",\"dst\":" + me._i(dst) + "}"
  }
  _wrap_fn(body_json){
    return "{ \"version\": 0, \"functions\": [ { \"name\": \"main\", \"params\": [], \"blocks\": [ { \"id\": 0, \"instructions\": [" + body_json + "] } ] } ] }"
  }

  // return 0（v0代表）
  ret0(){
    local body = me._inst_const(1, 0) + "," + me._inst_ret(1)
    return me._wrap_fn(body)
  }

  // return <v>
  ret_i64(v){
    local body = me._inst_const(1, v) + "," + me._inst_ret(1)
    return me._wrap_fn(body)
  }

  // (<lhs> <op> <rhs>) → return （op: +|-|*|/|%）
  binop_i64(lhs, rhs, opk){
    local kind = me._map_binop_kind(opk)
    local body = me._inst_const(1, lhs) + "," + me._inst_const(2, rhs) + "," + me._inst_binop(kind, 1, 2, 3) + "," + me._inst_ret(3)
    return me._wrap_fn(body)
  }
}
