|
| 1 | +(* JSON graph decoders for herd's JSON output. *) |
| 2 | + |
| 3 | +module Decoders = struct |
| 4 | + module D = Decoders_yojson.Basic.Decode |
| 5 | + open D.Infix |
| 6 | + open Execution |
| 7 | + |
| 8 | + let d_dir : dir D.decoder = |
| 9 | + let* s = D.string in |
| 10 | + match s with |
| 11 | + | "R" -> D.succeed R |
| 12 | + | "W" -> D.succeed W |
| 13 | + | _ -> D.fail "invalid direction" |
| 14 | + |
| 15 | + let d_event_ref : event_ref D.decoder = |
| 16 | + let+ eiid = D.field "eiid" D.int in |
| 17 | + { eiid } |
| 18 | + |
| 19 | + let d_rel_edge : rel_edge D.decoder = |
| 20 | + let* src = D.field "src" d_event_ref in |
| 21 | + let+ tgt = D.field "tgt" d_event_ref in |
| 22 | + { src; tgt } |
| 23 | + |
| 24 | + let d_location : location D.decoder = |
| 25 | + let* ty = D.field "type" D.string in |
| 26 | + match ty with |
| 27 | + | "reg" -> |
| 28 | + let* proc = D.field "proc" D.int in |
| 29 | + let+ reg_pretty = D.field "reg_pretty" D.string in |
| 30 | + Reg { proc; reg_pretty } |
| 31 | + | "global" -> |
| 32 | + let+ global_pretty = D.field "global_pretty" D.string in |
| 33 | + Global global_pretty |
| 34 | + | _ -> D.fail "unknown location type" |
| 35 | + |
| 36 | + let d_cst : cst D.decoder = |
| 37 | + let* ty = D.field "type" D.string in |
| 38 | + match ty with |
| 39 | + | "concrete" -> |
| 40 | + let+ scalar_pretty = D.field "scalar_pretty" D.string in |
| 41 | + Concrete { scalar_pretty } |
| 42 | + | "symbolic" -> |
| 43 | + let+ symbol_pretty = D.field "symbol_pretty" D.string in |
| 44 | + Symbolic { symbol_pretty } |
| 45 | + | "pteval" -> |
| 46 | + let* pteval_pretty = D.field "pteval_pretty" D.string in |
| 47 | + let+ as_physical = D.maybe (D.field "as_physical" D.string) in |
| 48 | + Pteval { pteval_pretty; as_physical } |
| 49 | + | _ -> D.fail "unknown constant value type" |
| 50 | + |
| 51 | + let d_value : value D.decoder = |
| 52 | + let* ty = D.field "type" D.string in |
| 53 | + match ty with |
| 54 | + | "var" -> |
| 55 | + let+ v = D.field "var" D.int in |
| 56 | + Var v |
| 57 | + | "val" -> |
| 58 | + let d_val_pretty = |
| 59 | + let+ vp = D.field "val_pretty" D.string in |
| 60 | + Val_pretty vp |
| 61 | + in |
| 62 | + let d_val = |
| 63 | + let+ v = D.field "val" d_cst in |
| 64 | + Val v |
| 65 | + in |
| 66 | + D.one_of [ ("val_pretty", d_val_pretty); ("val", d_val) ] |
| 67 | + | _ -> D.fail "unknown value type" |
| 68 | + |
| 69 | + let d_mem_action : mem_action D.decoder = |
| 70 | + let+ dir = D.field "dir" d_dir |
| 71 | + and+ loc = D.field "loc" d_location |
| 72 | + and+ value = D.field "value" d_value |
| 73 | + and+ is_implicit = D.field_opt_or ~default:false "is_implicit" D.bool |
| 74 | + and+ is_atomic = D.field_opt_or ~default:false "is_atomic" D.bool in |
| 75 | + { dir; loc; value; is_implicit; is_atomic } |
| 76 | + |
| 77 | + let d_reg_action : reg_action D.decoder = |
| 78 | + let+ dir = D.field "dir" d_dir |
| 79 | + and+ loc = D.field "loc" d_location |
| 80 | + and+ value = D.field "value" d_value in |
| 81 | + { dir; loc; value } |
| 82 | + |
| 83 | + let d_action : action D.decoder = |
| 84 | + let* ty = D.field "type" D.string in |
| 85 | + match ty with |
| 86 | + | "mem" -> |
| 87 | + let+ c = d_mem_action in |
| 88 | + Memory c |
| 89 | + | "reg" -> |
| 90 | + let+ c = d_reg_action in |
| 91 | + Register c |
| 92 | + | "barrier" -> |
| 93 | + let+ barrier_pretty = D.field "barrier_pretty" D.string in |
| 94 | + Barrier { barrier_pretty } |
| 95 | + | "commit" -> |
| 96 | + let+ commit = D.field "commit" D.string in |
| 97 | + Commit { commit } |
| 98 | + | "fault" -> D.succeed Fault |
| 99 | + | _ -> D.fail "unknown action type" |
| 100 | + |
| 101 | + let d_iiid : iiid D.decoder = |
| 102 | + let d_id = |
| 103 | + let* proc = D.field "proc" D.int in |
| 104 | + let* poi = D.field "poi" D.int in |
| 105 | + let+ inst_pretty = D.field "inst_pretty" D.string in |
| 106 | + Index { proc; poi; inst_pretty } |
| 107 | + in |
| 108 | + let d_init_spurious = |
| 109 | + let* s = D.string in |
| 110 | + match s with |
| 111 | + | "init" -> D.succeed Init |
| 112 | + | "spurious" -> D.succeed Spurious |
| 113 | + | _ -> D.fail "not a simple iiid string" |
| 114 | + in |
| 115 | + D.one_of [ ("init_spurious", d_init_spurious); ("id", d_id) ] |
| 116 | + |
| 117 | + let d_event : event D.decoder = |
| 118 | + let* act = D.field "act" d_action in |
| 119 | + let* eiid = D.field "eiid" D.int in |
| 120 | + let+ iiid = D.field "iiid" d_iiid in |
| 121 | + { act; eiid; iiid } |
| 122 | + |
| 123 | + open struct |
| 124 | + open RFMap |
| 125 | + |
| 126 | + let d_rfmap_src : src D.decoder = |
| 127 | + let* ty = D.field "type" D.string in |
| 128 | + match ty with |
| 129 | + | "final" -> |
| 130 | + let+ loc = D.field "loc" d_location in |
| 131 | + Final loc |
| 132 | + | "load" -> |
| 133 | + let+ event = D.field "event" d_event_ref in |
| 134 | + Load event |
| 135 | + | _ -> D.fail "unknown rfmap src" |
| 136 | + |
| 137 | + let d_rfmap_tgt : tgt D.decoder = |
| 138 | + let* ty = D.field "type" D.string in |
| 139 | + match ty with |
| 140 | + | "init" -> D.succeed Init |
| 141 | + | "store" -> |
| 142 | + let+ event = D.field "event" d_event_ref in |
| 143 | + Store event |
| 144 | + | _ -> D.fail "unknown rfmap tgt" |
| 145 | + |
| 146 | + let d_rfmap_edge : edge D.decoder = |
| 147 | + let* src = D.field "src" d_rfmap_src in |
| 148 | + let+ tgt = D.field "tgt" d_rfmap_tgt in |
| 149 | + { src; tgt } |
| 150 | + end |
| 151 | + |
| 152 | + let d_viewed_before : viewed_before D.decoder = |
| 153 | + D.key_value_pairs (D.list d_rel_edge) |
| 154 | + |
| 155 | + let decode : t D.decoder = |
| 156 | + let* events = D.field "events" (D.list d_event) in |
| 157 | + let* iico_data = D.field "iico_data" (D.list d_rel_edge) in |
| 158 | + let* iico_ctrl = D.field "iico_ctrl" (D.list d_rel_edge) in |
| 159 | + let* speculated = D.field "speculated" (D.list d_event_ref) in |
| 160 | + let* rfmap = D.field "rfmap" (D.list d_rfmap_edge) in |
| 161 | + let* viewed_before = D.field "viewed_before" d_viewed_before in |
| 162 | + let+ visible_po = D.maybe (D.field "visible_po" (D.list d_rel_edge)) in |
| 163 | + { |
| 164 | + events; |
| 165 | + iico_data; |
| 166 | + iico_ctrl; |
| 167 | + speculated; |
| 168 | + rfmap; |
| 169 | + viewed_before; |
| 170 | + visible_po; |
| 171 | + } |
| 172 | + |
| 173 | + let decode_list : t list D.decoder = D.list decode |
| 174 | + |
| 175 | + let decode_string (s : string) : (t list, string) result = |
| 176 | + D.decode_string decode_list s |> Result.map_error D.string_of_error |
| 177 | +end |
| 178 | + |
| 179 | +let decode_json_string = Decoders.decode_string |
0 commit comments