Skip to content

Commit 5872ab1

Browse files
committed
[herd,litmus,RISCV] Implement the li pseudo-insrcution
This direct implementation is motivated by the sise of its argument (64bits).
1 parent ef60a60 commit 5872ab1

File tree

8 files changed

+34
-13
lines changed

8 files changed

+34
-13
lines changed

herd/RISCVArch_herd.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module Make (C:Arch_herd.Config) (V:Value.S) =
2424

2525
let is_amo = function
2626
| Amo _ -> true
27-
| INop|Ret|J _|Bcc _|Load _|Store _|LoadReserve _
27+
| INop|Ret|Li _|J _|Bcc _|Load _|Store _|LoadReserve _
2828
| OpI _|OpI2 _|OpIW _|Op _|OpW _|OpA _
2929
|StoreConditional _|FenceIns _
3030
|AUIPC _| Ext _
@@ -106,7 +106,7 @@ module Make (C:Arch_herd.Config) (V:Value.S) =
106106
module V = V
107107

108108
let mem_access_size = function
109-
| INop | Ret | OpI _ | OpI2 _ | OpIW _ | Op _ | OpW _
109+
| INop | Ret | Li _ | OpI _ | OpI2 _ | OpIW _ | Op _ | OpW _
110110
| J _ | Bcc _ | FenceIns _ | OpA _ | AUIPC _
111111
| Ext _
112112
-> None

herd/RISCVSem.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ module
271271
begin match ii.A.inst with
272272
| RISCV.INop-> B.next1T ()
273273
| RISCV.Ret when O.variant Variant.Telechat -> M.unitT () >>! B.Exit
274+
| RISCV.Li (r,k) ->
275+
let v = V.Cst.Scalar.of_int64 k |> V.scalarToV in
276+
write_reg r v ii >>= B.next1T
274277
| RISCV.OpI2 (RISCV.LUI,r1,k) ->
275278
(* put k into upper half of r1*)
276279
M.op (Op.ShiftLeft) (V.intToV k)

jingle/RISCVArch_jingle.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ include Arch.MakeArch(struct
7979
and find_lab = find_lab subs
8080
and find_cst = find_cst subs in
8181
function
82+
| Li (r1,k) ->
83+
conv_reg r1 >! fun r1 ->
84+
Li (r1,k)
8285
| OpI (op,r1,r2,k) ->
8386
conv_reg r1 >> fun r1 ->
8487
conv_reg r2 >> fun r2 ->

lib/RISCVBase.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ type signed = Sign.t
314314
type 'k kinstruction =
315315
| INop
316316
| Ret
317+
| Li of reg * Int64.t
317318
| OpI of opi * reg * reg * 'k
318319
| OpI2 of opi2 * reg * 'k
319320
| OpA of opa * reg * lbl
@@ -348,6 +349,7 @@ type 'k basic_pp = { pp_k : 'k -> string; is_zero : 'k -> bool }
348349
let do_pp_instruction m = function
349350
| INop -> "nop"
350351
| Ret -> "ret"
352+
| Li (r,k) -> sprintf "li %s,%Li" (pp_reg r) k
351353
(* MV is a pseudoinstruction - special case of ADDI*)
352354
| OpI (ADDI, r1,r2,k) when m.is_zero k ->
353355
sprintf "mv %s,%s" (pp_reg r1) (pp_reg r2)
@@ -430,6 +432,7 @@ let fold_regs (f_reg,f_sreg) =
430432

431433
fun c ins -> match ins with
432434
| INop|Ret|J _ | FenceIns _ -> c
435+
| Li (r1,_)
433436
| OpA (_,r1,_) | AUIPC (r1,_) | OpI2 (_,r1,_) -> fold_reg r1 c
434437
| OpI (_,r1,r2,_) | OpIW (_,r1,r2,_)
435438
| Bcc (_,r1,r2,_)
@@ -451,6 +454,7 @@ let map_regs f_reg f_symb =
451454

452455
function ins -> match ins with
453456
| INop|Ret|J _ | FenceIns _ -> ins
457+
| Li (r,k) -> Li (map_reg r,k)
454458
| OpI (op,r1,r2,k) ->
455459
OpI (op,map_reg r1,map_reg r2,k)
456460
| OpI2 (op,r1,k) ->
@@ -488,7 +492,7 @@ let map_addrs _f ins = ins
488492
let get_next = function
489493
| J lbl -> [Label.To lbl;]
490494
| Bcc (_,_,_,lbl) -> [Label.Next; Label.To lbl;]
491-
| INop|Ret|OpI (_, _, _, _)|OpIW (_, _, _, _)|Op (_, _, _, _)|OpW (_, _, _, _) | OpA (_,_,_) | AUIPC (_,_)
495+
| INop|Ret|Li _|OpI (_, _, _, _)|OpIW (_, _, _, _)|Op (_, _, _, _)|OpW (_, _, _, _) | OpA (_,_,_) | AUIPC (_,_)
492496
| OpI2 (_,_,_)
493497
| Load (_,_, _, _, _, _)|Store (_,_, _, _, _)|LoadReserve (_, _, _, _)
494498
| StoreConditional (_, _, _, _, _)|FenceIns _|Amo _|Ext (_,_,_,_)
@@ -510,7 +514,7 @@ include Pseudo.Make
510514
| OpIW (op,r1,r2,k) -> OpIW (op,r1,r2,k_tr k)
511515
|AUIPC (r1, k) -> AUIPC (r1,k_tr k)
512516
| Op (_, _, _, _)|OpW (_, _, _, _)|J _|Bcc (_, _, _, _)
513-
|INop | Ret | OpA (_,_,_)
517+
|INop | Ret | Li _ | OpA (_,_,_)
514518
|Load (_, _, _, _, _, _)|Store (_, _ ,_ , _, _)
515519
| Ext (_,_,_,_)
516520
|LoadReserve (_, _, _, _)|StoreConditional (_, _, _, _, _)|Amo _|FenceIns _
@@ -524,6 +528,7 @@ include Pseudo.Make
524528
| Amo _ -> 2
525529
| INop
526530
| Ret
531+
| Li _
527532
| OpI (_, _, _, _)|OpIW (_, _, _, _)|Op (_, _, _, _)
528533
| OpI2 (_,_,_)
529534
| OpA (_,_,_) | AUIPC (_,_) | Ext (_,_,_,_)
@@ -538,6 +543,7 @@ include Pseudo.Make
538543
-> f k lbl
539544
| INop
540545
| Ret
546+
| Li _
541547
|OpI (_, _, _, _)|OpIW (_, _, _, _)|Op (_, _, _, _) | AUIPC (_,_)
542548
| OpI2 (_,_,_)
543549
|OpW (_, _, _, _)|Load (_, _, _, _, _, _)|Store (_, _, _, _, _)
@@ -550,6 +556,7 @@ include Pseudo.Make
550556
| OpA (op,r1,lbl) -> OpA (op,r1,BranchTarget.as_string_fun f lbl)
551557
|INop
552558
|Ret
559+
|Li _
553560
|OpI (_, _, _, _)|OpIW (_, _, _, _)|Op (_, _, _, _)
554561
| OpI2 (_,_,_)
555562
|OpW (_, _, _, _)|Load _|Store _| AUIPC (_,_)

lib/RISCVLexer.mll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ rule token = parse
219219
| [' ''\t''\r'] { token lexbuf }
220220
| '\n' { incr_lineno lexbuf; token lexbuf }
221221
| "(*" { LU.skip_comment lexbuf ; token lexbuf }
222-
| '-' ? num as x { NUM (int_of_string x) }
222+
| '-' ? num as x { NUM x }
223223
| 'P' (num as x)
224224
{ PROC (int_of_string x) }
225225
| '&' (name as x) { META x }

lib/RISCVParser.mly

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ let tr_rw = function
3131
%token EOF
3232
%token <RISCVBase.reg> ARCH_REG
3333
%token <string> SYMB_REG
34-
%token <int> NUM
34+
%token <string> NUM
3535
%token <string> NAME
3636
%token <int> PROC
3737

@@ -96,18 +96,18 @@ reg:
9696
| ARCH_REG { $1 }
9797

9898
k:
99-
| NUM { MetaConst.Int $1 }
99+
| NUM { MetaConst.Int (int_of_string $1) }
100100
| META { MetaConst.Meta $1 }
101101

102102
addr:
103103
| NUM LPAR reg RPAR
104-
{ $1,$3 }
104+
{ int_of_string $1,$3 }
105105
| LPAR reg RPAR
106106
{ 0,$2 }
107107

108108
addr0:
109109
| NUM LPAR reg RPAR
110-
{ if $1 <> 0 then raise Parsing.Parse_error;
110+
{ if $1 <> "0" then raise Parsing.Parse_error;
111111
$3 }
112112
| LPAR reg RPAR
113113
{ $2 }
@@ -118,8 +118,8 @@ instr:
118118
| RET
119119
{ A.Ret }
120120
/* OPs */
121-
| LI reg COMMA k
122-
{ A.OpI (A.ORI,$2,A.Ireg A.X0,$4) }
121+
| LI reg COMMA NUM
122+
{ A.Li ($2,Int64.of_string $4) }
123123
| LA reg COMMA NAME
124124
{ A.OpA (A.LA,$2,$4) }
125125
| LUI reg COMMA k

lib/genParserUtils.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ let call_parser name lexbuf lex parse =
3737
| Misc.Timeout as e -> raise e
3838
| e ->
3939
Printf.eprintf
40-
"%a: Uncaught exception %s (in %s)\n"
40+
"%a: Exception raised in parser %s (in %s)\n"
4141
Pos.pp_pos lexbuf.lex_curr_p
4242
(Printexc.to_string e) name ;
43-
assert false
43+
raise e
4444

4545

4646
(************************)

litmus/RISCVCompile_litmus.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ module Make(V:Constant.S)(C:Arch_litmus.Config) =
5757
memo = sprintf "%s %s,%s,%i" memo fmt1 fmt2 k ;
5858
inputs=r2; outputs=r1; }
5959

60+
let li r k =
61+
let fmt,r = tr_1o r in
62+
{ empty_ins with
63+
memo = sprintf "li %s,%Li" fmt k ;
64+
outputs=r; }
65+
6066
let lui r1 k =
6167
let fmt1,r1 = tr_1o r1 in
6268
{ empty_ins with
@@ -90,6 +96,8 @@ module Make(V:Constant.S)(C:Arch_litmus.Config) =
9096
let compile_ins tr_lab ins k = match ins with
9197
| A.INop -> { empty_ins with memo="nop"; }::k
9298
| A.Ret -> { empty_ins with memo="ret"; }::k
99+
| A.Li (r,i) ->
100+
li r i::k
93101
| A.OpI2 (A.LUI,r1,i) ->
94102
lui r1 i::k
95103
| A.OpI (op,r1,r2,i) ->

0 commit comments

Comments
 (0)