File: sparcJumps.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (304 lines) | stat: -rw-r--r-- 12,583 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
(* sparcJumps.sml --- information to resolve jumps. 
 *
 * COPYRIGHT (c) 1996 Bell Laboratories.
 *
 *)
functor SparcJumps
  (structure Instr:SPARCINSTR
   structure Shuffle:SPARCSHUFFLE (* where I = Instr *)
                     where type I.Constant.const = Instr.Constant.const
                       and type I.Region.region = Instr.Region.region
                       and type I.T.Basis.cond = Instr.T.Basis.cond
                       and type I.T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
                       and type I.T.Basis.ext = Instr.T.Basis.ext
                       and type I.T.Basis.fcond = Instr.T.Basis.fcond
                       and type I.T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
                       and type ('s,'r,'f,'c) I.T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
                       and type ('s,'r,'f,'c) I.T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
                       and type ('s,'r,'f,'c) I.T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
                       and type ('s,'r,'f,'c) I.T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
                       and type I.T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
                       and type I.T.ccexp = Instr.T.ccexp
                       and type I.T.fexp = Instr.T.fexp
                       (* and type I.T.labexp = Instr.T.labexp *)
                       and type I.T.mlrisc = Instr.T.mlrisc
                       and type I.T.oper = Instr.T.oper
                       and type I.T.rep = Instr.T.rep
                       and type I.T.rexp = Instr.T.rexp
                       and type I.T.stm = Instr.T.stm
                       and type I.arith = Instr.arith
                       and type I.branch = Instr.branch
                       and type I.cc = Instr.cc
                       and type I.ea = Instr.ea
                       and type I.farith1 = Instr.farith1
                       and type I.farith2 = Instr.farith2
                       and type I.fbranch = Instr.fbranch
                       and type I.fcmp = Instr.fcmp
                       and type I.fload = Instr.fload
                       and type I.fsize = Instr.fsize
                       and type I.fstore = Instr.fstore
                       and type I.instr = Instr.instr
                       and type I.instruction = Instr.instruction
                       and type I.load = Instr.load
                       and type I.operand = Instr.operand
                       and type I.prediction = Instr.prediction
                       and type I.rcond = Instr.rcond
                       and type I.shift = Instr.shift
                       and type I.store = Instr.store
   structure MLTreeEval : MLTREE_EVAL (* where T = Instr.T *)
                          where type T.Basis.cond = Instr.T.Basis.cond
                            and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
                            and type T.Basis.ext = Instr.T.Basis.ext
                            and type T.Basis.fcond = Instr.T.Basis.fcond
                            and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
                            and type T.Constant.const = Instr.T.Constant.const
                            and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
                            and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
                            and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
                            and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
                            and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
                            and type T.Region.region = Instr.T.Region.region
                            and type T.ccexp = Instr.T.ccexp
                            and type T.fexp = Instr.T.fexp
                            (* and type T.labexp = Instr.T.labexp *)
                            and type T.mlrisc = Instr.T.mlrisc
                            and type T.oper = Instr.T.oper
                            and type T.rep = Instr.T.rep
                            and type T.rexp = Instr.T.rexp
                            and type T.stm = Instr.T.stm
  ) : SDI_JUMPS = 
struct
  structure I = Instr
  structure C = Instr.C
  structure Const = I.Constant
  structure CB = CellsBasis

  fun error msg = MLRiscErrorMsg.error("SparcJumps",msg)

  val branchDelayedArch = true


  fun isSdi(I.ANNOTATION{i,...}) = isSdi i
    | isSdi(I.LIVE _)		  = true
    | isSdi(I.KILL _)		  = true
    | isSdi(I.COPY _)		  = true
    | isSdi(I.INSTR instr) = let
	fun oper(I.IMMED n) = false
	      | oper(I.REG _) = false
	      | oper(I.HI _) = false
	      | oper(I.LO _) = false
	      | oper(I.LAB _) = true
	in  case instr of 
	      I.ARITH{i,...} => oper i
	    | I.SHIFT{i,...} => oper i
	    | I.LOAD{i,...} => oper i
	    | I.STORE{i,...} => oper i
	    | I.FLOAD{i,...} => oper i
	    | I.FSTORE{i,...} => oper i
	    | I.JMPL{i,...} => oper i
	    | I.JMP{i,...} => oper i
	    | I.MOVicc{i,...} => oper i
	    | I.MOVfcc{i,...} => oper i
	    | I.MOVR{i,...} => oper i
	    | I.CALL _ => true
	    | I.Bicc _ => true
	    | I.FBfcc _ => true
	    | I.BR _ => true
	    | I.BP _ => true
	    | I.Ticc{i,...} => oper i
	    | I.WRY{i,...} => oper i
	    | I.SAVE{i,...} => oper i
	    | I.RESTORE{i,...} => oper i
	    (* The following is only true of Version 8 *)
	    | I.FPop1{a=(I.FMOVd | I.FNEGd | I.FABSd), ...} => true
	    | _ => false
	end

  fun minSize(I.ANNOTATION{i,...}) = minSize i
    | minSize(I.LIVE _)  = 0
    | minSize(I.KILL _)  = 0
    | minSize (I.COPY _) = 0		(* ? *)
    | minSize(I.INSTR instr) = 
      (case instr
	of (I.Bicc{nop=true,...}) => 8
	 | (I.FBfcc{nop=true,...}) => 8
	 | (I.JMP{nop=true,...}) => 8
	 | (I.JMPL{nop=true,...}) => 8
	 | (I.CALL{nop=true,...}) => 8
	 | (I.BR{nop=true,...}) => 8
	 | (I.BP{nop=true,...}) => 8
	 | (I.RET{nop=true,...}) => 8
	 | (I.FCMP{nop=true,...}) => 8
	 | (I.FPop1{a=(I.FMOVd | I.FNEGd | I.FABSd),...}) => 8
	 |  _          => 4
      (*esac*))

  fun maxSize (I.INSTR(I.FPop1{a=(I.FMOVd | I.FNEGd | I.FABSd),...})) = 8
    | maxSize (I.ANNOTATION{i,...}) = maxSize i
    | maxSize _		   = 4

  fun immed13 n = ~4096 <= n andalso n < 4096
  fun immed22 n = ~0x200000 <= n andalso n < 0x1fffff
  fun immed16 n = ~0x8000 <= n andalso n < 0x8000
  fun immed19 n = ~0x40000 <= n andalso n < 0x40000
  fun immed30 n = ~0x4000000 <= n andalso n < 0x3ffffff

  fun instrLength([],n) = n
    | instrLength(I.INSTR(I.FPop1{a=(I.FMOVd | I.FNEGd | I.FABSd),...})::is,n) =
        instrLength(is,n+8)
    | instrLength(_::is,n) = instrLength(is,n+4)

  fun sdiSize(I.ANNOTATION{i, ...}, labmap, loc) = sdiSize(i, labmap, loc)
    | sdiSize(I.LIVE _, _, _) = 0
    | sdiSize(I.KILL _, _, _) = 0
    | sdiSize(I.COPY{k=CB.GP, src, dst, tmp, ...}, _, _) =
	      4 * length(Shuffle.shuffle{tmp=tmp, dst=dst, src=src})
    | sdiSize(I.COPY{k=CB.FP, src, dst, tmp, ...}, _, _) = let
		val instrs = Shuffle.shufflefp{src=src, dst=dst, tmp=tmp}
            in instrLength(instrs, 0)
            end
    | sdiSize(instr as I.INSTR i, labMap, loc) = let
	  fun oper(I.IMMED n,_) = 4
	    | oper(I.REG _,_) = 4
	    | oper(I.HI _,_) = 4
	    | oper(I.LO _,_) = 4
	    | oper(I.LAB lexp,hi) = if immed13(MLTreeEval.valueOf lexp) then 4 else hi
	  fun displacement lab = ((labMap lab) - loc) div 4
	  fun branch22 lab = if immed22(displacement lab) then 4 else 16
	  fun branch19 lab = if immed19(displacement lab) then 4 else 16
	  fun branch16 lab = if immed16(displacement lab) then 4 else 16
	  fun call lab = if immed30(displacement lab) then 4 else 20
	  fun delaySlot false = 0
	    | delaySlot true = 4
      in  
	 case i 
	 of  I.ARITH{a=I.OR,r,i,...} => 
	       if CellsBasis.cellId r = 0 then oper(i,8) else oper(i,12)
	  | I.ARITH{i,...} => oper(i,12)
	  | I.SHIFT{i,...} => oper(i,12)
	  | I.LOAD{i,...} => oper(i,12)
	  | I.STORE{i,...} => oper(i,12)
	  | I.FLOAD{i,...} => oper(i,12)
	  | I.FSTORE{i,...} => oper(i,12)
	  | I.Ticc{i,...} => oper(i,12)
	  | I.SAVE{i,...} => oper(i,12)
	  | I.RESTORE{i,...} => oper(i,12)
	  | I.MOVicc{i,...} => oper(i,12)
	  | I.MOVfcc{i,...} => oper(i,12)
	  | I.MOVR{i,...} => oper(i,12)
	  | I.JMPL{i,nop,...} => oper(i,12) + delaySlot nop
	  | I.JMP{i,nop,...} => oper(i,12) + delaySlot nop
	  | I.Bicc{label,nop,...} => branch22 label + delaySlot nop
	  | I.FBfcc{label,nop,...} => branch22 label + delaySlot nop
	  | I.BR{label,nop,...} => branch16 label + delaySlot nop
	  | I.BP{label,nop,...} => branch19 label + delaySlot nop
	  | I.CALL{label,...} => call label
	  | I.WRY{i,...} => oper(i,12)
 	  | I.FPop1{a=(I.FMOVd | I.FNEGd | I.FABSd),...} => 8	    
	  | _ => error "sdiSize"
      end
    | sdiSize _ = error "sdiSize"

  fun split22_10 n =
  let val w = Word32.fromInt n
  in  {hi=Word32.toInt(Word32.>>(w,0w10)),
       lo=Word32.toInt(Word32.andb(w,0wx3ff))
      }
  end

  fun split(I.LAB lexp) = split22_10(MLTreeEval.valueOf lexp)
    | split _ = error "split"

  (* Expand the immediate constant into two instructions *)
  fun expandImm(immed,instr) = 
      let val {lo,hi} = split immed
      in  
	  [I.sethi{i=hi,d=C.asmTmpR},
           I.arith{a=I.OR,r=C.asmTmpR,i=I.IMMED lo,d=C.asmTmpR},
           I.INSTR instr
          ]
      end

  (* Expand a span dependent instruction *)
  fun expand(I.ANNOTATION{i, ...}, size, pos) = expand(i, size, pos)
    | expand(I.LIVE _, _, _) = []
    | expand(I.KILL _, _, _) = []
    | expand(I.COPY{k=CB.GP, src, tmp, dst, ...}, _, _)  = 
       Shuffle.shuffle{src=src, dst=dst, tmp=tmp}
    | expand(I.COPY{k=CB.FP, src, tmp, dst, ...}, _, _)  = 
       Shuffle.shufflefp{src=src, dst=dst, tmp=tmp}
    | expand(instr as (I.INSTR i), size, pos) = 
      (case (i,size) 
       of (_,4) => [instr]
	| (I.ARITH{a=I.OR,r,i,d},8) =>
	    if CellsBasis.cellId r = 0 then
	    let val {lo,hi} = split i
	    in  [I.sethi{i=hi,d=C.asmTmpR},
		 I.arith{a=I.OR,r=C.asmTmpR,i=I.IMMED lo,d=d}
		]
	    end
	    else error "ARITH"
	| (I.ARITH{a,r,i,d},12) =>
	    expandImm(i,I.ARITH{a=a,r=r,i=I.REG C.asmTmpR,d=d})
	| (I.SHIFT{s,r,i,d},12) =>
	    expandImm(i,I.SHIFT{s=s,r=r,i=I.REG C.asmTmpR,d=d})
	| (I.SAVE{r,i,d},12) =>
	    expandImm(i,I.SAVE{r=r,i=I.REG C.asmTmpR,d=d})
	| (I.RESTORE{r,i,d},12) =>
	    expandImm(i,I.RESTORE{r=r,i=I.REG C.asmTmpR,d=d})
	| (I.LOAD{l,r,i,d,mem},12) =>  
	    expandImm(i,I.LOAD{l=l,r=r,i=I.REG C.asmTmpR,d=d,mem=mem})
	| (I.STORE{s,r,i,d,mem},12) =>
	    expandImm(i,I.STORE{s=s,r=r,i=I.REG C.asmTmpR,d=d,mem=mem})
	| (I.FLOAD{l,r,i,d,mem},12) =>
	    expandImm(i,I.FLOAD{l=l,r=r,i=I.REG C.asmTmpR,d=d,mem=mem})
	| (I.FSTORE{s,r,i,d,mem},12) =>
	    expandImm(i,I.FSTORE{s=s,r=r,i=I.REG C.asmTmpR,d=d,mem=mem})
	| (I.MOVicc{b,i,d},12) =>
	    expandImm(i,I.MOVicc{b=b,i=I.REG C.asmTmpR,d=d})
	| (I.MOVfcc{b,i,d},12) =>
	    expandImm(i,I.MOVfcc{b=b,i=I.REG C.asmTmpR,d=d})
	| (I.MOVR{rcond,r,i,d},12) =>
	    expandImm(i,I.MOVR{rcond=rcond,r=r,i=I.REG C.asmTmpR,d=d})
	| (I.JMPL _,8) => [instr]
	| (I.JMP _,8) => [instr]
	| (I.Bicc _,8) => [instr]
	| (I.FBfcc _,8) => [instr]
	| (I.BR _,8) => [instr]
	| (I.BP _,8) => [instr]
	| (I.JMPL{r,i,d,defs,uses,cutsTo,nop,mem},(12 | 16)) => 
	    expandImm(i,I.JMPL{r=r,i=I.REG C.asmTmpR,d=d,defs=defs,uses=uses,
			       cutsTo=cutsTo,nop=nop,mem=mem})
	| (I.JMP{r,i,labs,nop},(12 | 16)) => 
	    expandImm(i,I.JMP{r=r,i=I.REG C.asmTmpR,labs=labs,nop=nop})
	| (I.Ticc{t,cc,r,i},12) =>
	    expandImm(i,I.Ticc{t=t,cc=cc,r=r,i=I.REG C.asmTmpR})
	    (* 
	     * The sparc uses 22bits signed extended displacement offsets
	     * Let's hope it's enough
	     *)
	| (I.Bicc{b,a,label,nop},_) => error "Bicc"  
	| (I.FBfcc{b,a,label,nop},_) => error "FBfcc" 
	| (I.FPop1{a, r, d}, _) => let
	    fun nextRegNum c = C.FPReg(CellsBasis.registerNum c + 1)
	    (* Note: if r=d then the move is not required.
	     * This needs to be factored into the size before it
	     * can be done here.
	     *)
	    fun doDouble(oper) = 
	      [I.fpop1{a=oper, r=r, d=d},
	       I.fpop1{a=I.FMOVs, r=nextRegNum r, d=nextRegNum d}]
          in
	    case a 
             of I.FMOVd => doDouble(I.FMOVs)
	      | I.FNEGd => doDouble(I.FNEGs)
	      | I.FABSd => doDouble(I.FABSs)
	      | _ => error "expand: FPop1"
          end
	| (I.WRY{r,i},12) => expandImm(i,I.WRY{r=r,i=I.REG C.asmTmpR})
	|  _ => error "expand"
      (*esac*))
    | expand _ = error "expand"

end