summaryrefslogtreecommitdiff
path: root/compiler/arm/narmset.pas
blob: 9c0d66813487230fdb7f62358ed4dc1f0af5bb58 (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
{
   Copyright (c) 1998-2002 by Florian Klaempfl

    Generate arm assembler for in set/case nodes

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************
}
unit narmset;

{$i fpcdefs.inc}

interface

    uses
      globtype,constexp,
      symtype,
      cgbase,
      node,nset,pass_1,ncgset;

    type

       { tarminnode }

       tarminnode = class(tcginnode)
         function pass_1: tnode; override;
         procedure in_smallset(opdef: tdef; setbase: aint); override;
       end;

      tarmcasenode = class(tcgcasenode)
         procedure optimizevalues(var max_linear_list:int64;var max_dist:qword);override;
         function  has_jumptable : boolean;override;
         procedure genjumptable(hp : pcaselabel;min_,max_ : int64);override;
         procedure genlinearlist(hp : pcaselabel);override;
         procedure genjmptreeentry(p : pcaselabel;parentvalue : TConstExprInt);override;
      end;


implementation

    uses
      verbose,globals,defutil,systems,
      aasmbase,aasmtai,aasmdata,aasmcpu,
      cpubase,cpuinfo,
      cgutils,cgobj,ncgutil,
      cgcpu,hlcgobj;

{*****************************************************************************
                            TARMINNODE
*****************************************************************************}

    function tarminnode.pass_1: tnode;
      var
        setparts: Tsetparts;
        numparts: byte;
        use_small: boolean;
      begin
        result:=inherited pass_1;

        if not(assigned(result)) then
          begin
            if not(checkgenjumps(setparts,numparts,use_small)) and
              use_small and
              (target_info.endian=endian_little) then
              expectloc:=LOC_FLAGS;
          end;
      end;

    procedure tarminnode.in_smallset(opdef: tdef; setbase: aint);
      var
        so : tshifterop;
        hregister : tregister;
      begin
        { the code below needs changes for big endian targets (they start
          counting from the most significant bit)
        }
        if target_info.endian=endian_big then
          begin
            inherited;
            exit;
          end;
        location_reset(location,LOC_FLAGS,OS_NO);
        location.resflags:=F_NE;
        if (left.location.loc=LOC_CONSTANT) and not(GenerateThumbCode) then
          begin
            hlcg.location_force_reg(current_asmdata.CurrAsmList, right.location,
              right.resultdef, right.resultdef, true);

            cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
            current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_TST,right.location.register,1 shl (left.location.value-setbase)));
          end
        else
          begin
            hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location,
             left.resultdef, opdef, true);
            register_maybe_adjust_setbase(current_asmdata.CurrAsmList, opdef,
             left.location, setbase);
            hlcg.location_force_reg(current_asmdata.CurrAsmList, right.location,
             right.resultdef, right.resultdef, true);

            hregister:=hlcg.getintregister(current_asmdata.CurrAsmList, opdef);
            hlcg.a_load_const_reg(current_asmdata.CurrAsmList,opdef,1,hregister);

            if GenerateThumbCode or GenerateThumb2Code then
              begin
                hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_SHL,opdef,left.location.register,hregister);
                cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
                current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_TST,right.location.register,hregister));
              end
            else
              begin
                shifterop_reset(so);
                so.rs:=left.location.register;
                so.shiftmode:=SM_LSL;
                cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
                current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_shifterop(A_TST,right.location.register,hregister,so));
              end;
          end;
      end;


{*****************************************************************************
                            TARMCASENODE
*****************************************************************************}

    procedure tarmcasenode.optimizevalues(var max_linear_list:int64;var max_dist:qword);
      begin
        inc(max_linear_list,2)
      end;


    function tarmcasenode.has_jumptable : boolean;
      begin
        has_jumptable:=true;
      end;


    procedure tarmcasenode.genjumptable(hp : pcaselabel;min_,max_ : int64);
      var
        last : TConstExprInt;
        tmpreg,
        basereg,
        indexreg : tregister;
        href : treference;
        tablelabel, piclabel : TAsmLabel;
        opcgsize : tcgsize;
        picoffset : int64;

        procedure genitem(list:TAsmList;t : pcaselabel);
          var
            i : int64;
          begin
            if assigned(t^.less) then
              genitem(list,t^.less);
            { fill possible hole }
            i:=last+1;
            while i<=t^._low-1 do
              begin
                if cs_create_pic in current_settings.moduleswitches then
                  list.concat(Tai_const.Create_rel_sym_offset(aitconst_ptr,piclabel,elselabel,picoffset))
                else
                  list.concat(Tai_const.Create_sym(elselabel));
                i:=i+1;
              end;
            i:=t^._low;
            while i<=t^._high do
              begin
                if cs_create_pic in current_settings.moduleswitches then
                  list.concat(Tai_const.Create_rel_sym_offset(aitconst_ptr,piclabel,blocklabel(t^.blockid),picoffset))
                else
                  list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
                i:=i+1;
              end;
            last:=t^._high;
            if assigned(t^.greater) then
              genitem(list,t^.greater);
          end;

        procedure genitem_thumb2(list:TAsmList;t : pcaselabel);
          var
            i : int64;
          begin
            if assigned(t^.less) then
              genitem_thumb2(list,t^.less);
            { fill possible hole }
            i:=last.svalue+1;
            while i<=t^._low.svalue-1 do
              begin
                list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,elselabel));
                i:=i+1;
              end;
            i:=t^._low.svalue;
            while i<=t^._high.svalue do
              begin
                list.concat(Tai_const.Create_rel_sym(aitconst_half16bit,tablelabel,blocklabel(t^.blockid)));
                i:=i+1;
              end;
            last:=t^._high.svalue;
            if assigned(t^.greater) then
              genitem_thumb2(list,t^.greater);
          end;

      begin
        opcgsize:=def_cgsize(opsize);
        if not(jumptable_no_range) then
          begin
             { case expr less than min_ => goto elselabel }
             cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(min_),hregister,elselabel);
             { case expr greater than max_ => goto elselabel }
             cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_gt,aint(max_),hregister,elselabel);
          end;
        { make it a 32bit register }
        indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_INT);
        cg.a_load_reg_reg(current_asmdata.CurrAsmList,opcgsize,OS_INT,hregister,indexreg);

        if GenerateThumb2Code then
          begin
            if cs_create_pic in current_settings.moduleswitches then
              internalerror(2013082101);
            { adjust index }
            cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_,indexreg,indexreg);
            { create reference and generate jump table }
            reference_reset(href,4,[]);
            href.base:=NR_PC;
            href.index:=indexreg;
            href.shiftmode:=SM_LSL;
            href.shiftimm:=1;
            current_asmdata.CurrAsmList.Concat(taicpu.op_ref(A_TBH,href));
            { generate jump table }
            current_asmdata.getjumplabel(tablelabel);
            cg.a_label(current_asmdata.CurrAsmList,tablelabel);
            last:=min_;
            genitem_thumb2(current_asmdata.CurrAsmList,hp);
          end
        else if GenerateThumbCode then
          begin
            if cs_create_pic in current_settings.moduleswitches then
              internalerror(2013082102);
            cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,min_,indexreg,indexreg);
            current_asmdata.getaddrlabel(tablelabel);

            cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,indexreg);

            basereg:=cg.getintregister(current_asmdata.CurrAsmList, OS_ADDR);
            reference_reset_symbol(href,tablelabel,0,4,[]);
            cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList, href, basereg);

            reference_reset(href,0,[]);
            href.base:=basereg;
            href.index:=indexreg;

            tmpreg:=cg.getintregister(current_asmdata.CurrAsmList, OS_ADDR);
            cg.a_load_ref_reg(current_asmdata.CurrAsmList, OS_ADDR, OS_ADDR, href, tmpreg);

            { do not use BX here to avoid switching into arm mode }
            current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(A_MOV, NR_PC, tmpreg));

            current_asmdata.CurrAsmList.Concat(tai_align.Create(4));
            cg.a_label(current_asmdata.CurrAsmList,tablelabel);
            { generate jump table }
            last:=min_;
            genitem(current_asmdata.CurrAsmList,hp);
          end
        else
          begin
            { adjust index }
            cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_ADDR,
              min_+ord(not(cs_create_pic in current_settings.moduleswitches)),
              indexreg,indexreg);
            { create reference and generate jump table }
            reference_reset(href,4,[]);
            href.base:=NR_PC;
            href.index:=indexreg;
            href.shiftmode:=SM_LSL;
            href.shiftimm:=2;
            if cs_create_pic in current_settings.moduleswitches then
              begin
                picoffset:=-8;
                current_asmdata.getaddrlabel(piclabel);
                indexreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
                cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,indexreg);
                cg.a_label(current_asmdata.CurrAsmList,piclabel);
                cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,indexreg,NR_PC);
              end
            else
              cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,NR_PC);
            { generate jump table }
            last:=min_;
            genitem(current_asmdata.CurrAsmList,hp);
          end;
      end;


    procedure tarmcasenode.genlinearlist(hp : pcaselabel);
      var
        first : boolean;
        lastrange : boolean;
        last : TConstExprInt;
        cond_lt,cond_le : tresflags;
        opcgsize : tcgsize;

        procedure genitem(t : pcaselabel);
          begin
             if assigned(t^.less) then
               genitem(t^.less);
             { need we to test the first value }
             if first and (t^._low>get_min_value(left.resultdef)) then
               begin
                 cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
               end;
             if t^._low=t^._high then
               begin
                  if t^._low-last=0 then
                    cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
                  else
                    begin
                      tbasecgarm(cg).cgsetflags:=true;
                      { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
                        then genlinearlist wouldn't be used }
                      cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
                      tbasecgarm(cg).cgsetflags:=false;
                      cg.a_jmp_flags(current_asmdata.CurrAsmList,F_EQ,blocklabel(t^.blockid));
                    end;
                  last:=t^._low;
                  lastrange:=false;
               end
             else
               begin
                  { it begins with the smallest label, if the value }
                  { is even smaller then jump immediately to the    }
                  { ELSE-label                                }
                  if first then
                    begin
                       { have we to ajust the first value ? }
                       if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
                         begin
                           tbasecgarm(cg).cgsetflags:=true;
                           { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
                             then genlinearlist wouldn't be use }
                           cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low)), hregister);
                           tbasecgarm(cg).cgsetflags:=false;
                         end;
                    end
                  else
                    begin
                      { if there is no unused label between the last and the }
                      { present label then the lower limit can be checked    }
                      { immediately. else check the range in between:       }

                      tbasecgarm(cg).cgsetflags:=true;
                      { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
                        then genlinearlist wouldn't be use }
                      cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, OS_32, aint(int64(t^._low-last)), hregister);
                      tbasecgarm(cg).cgsetflags:=false;
                      { no jump necessary here if the new range starts at }
                      { at the value following the previous one           }
                      if ((t^._low-last) <> 1) or
                         (not lastrange) then
                        cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
                    end;
                  tbasecgarm(cg).cgsetflags:=true;
                  { use OS_32 here to avoid uncessary sign extensions, at this place hregister will never be negative, because
                    then genlinearlist wouldn't be use }
                  cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_32,aint(int64(t^._high-t^._low)),hregister);
                  tbasecgarm(cg).cgsetflags:=false;
                  cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));

                  last:=t^._high;
                  lastrange:=true;
               end;
             first:=false;
             if assigned(t^.greater) then
               genitem(t^.greater);
          end;

        begin
           opcgsize:=def_cgsize(opsize);
           if with_sign then
             begin
                cond_lt:=F_LT;
                cond_le:=F_LE;
             end
           else
              begin
                cond_lt:=F_CC;
                cond_le:=F_LS;
             end;
           { do we need to generate cmps? }
           if (with_sign and (min_label<0)) then
             genlinearcmplist(hp)
           else
             begin
                last:=0;
                lastrange:=false;
                first:=true;
                genitem(hp);
                cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
             end;
        end;


      procedure tarmcasenode.genjmptreeentry(p : pcaselabel;parentvalue : TConstExprInt);
        var
          lesslabel,greaterlabel : tasmlabel;
          cond_gt: TResFlags;
          cmplow : Boolean;
        begin
           if with_sign then
             cond_gt:=F_GT
           else
             cond_gt:=F_HI;
          current_asmdata.CurrAsmList.concat(cai_align.Create(current_settings.alignment.jumpalign));
          cg.a_label(current_asmdata.CurrAsmList,p^.labellabel);

          { calculate labels for left and right }
          if p^.less=nil then
            lesslabel:=elselabel
          else
            lesslabel:=p^.less^.labellabel;
          if p^.greater=nil then
            greaterlabel:=elselabel
          else
            greaterlabel:=p^.greater^.labellabel;

          { calculate labels for left and right }
          { no range label: }
          if p^._low=p^._high then
            begin
              if greaterlabel=lesslabel then
                hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_NE,p^._low,hregister,lesslabel)
              else
                begin
                  cmplow:=p^._low-1<>parentvalue;
                  if cmplow then
                    hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,p^._low,hregister,lesslabel);
                  if p^._high+1<>parentvalue then
                    begin
                      if cmplow then
                        hlcg.a_jmp_flags(current_asmdata.CurrAsmList,cond_gt,greaterlabel)
                      else
                        hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,p^._low,hregister,greaterlabel);
                    end;
                end;
              hlcg.a_jmp_always(current_asmdata.CurrAsmList,blocklabel(p^.blockid));
            end
          else
            begin
              if p^._low-1<>parentvalue then
                hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,p^._low,hregister,lesslabel);
              if p^._high+1<>parentvalue then
                hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,p^._high,hregister,greaterlabel);
              hlcg.a_jmp_always(current_asmdata.CurrAsmList,blocklabel(p^.blockid));
            end;
           if assigned(p^.less) then
             genjmptreeentry(p^.less,p^._low);
           if assigned(p^.greater) then
             genjmptreeentry(p^.greater,p^._high);
        end;

begin
  cinnode:=tarminnode;
  ccasenode:=tarmcasenode;
end.