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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
|
{
Copyright (c) 2000-2002 by Florian Klaempfl
Generate assembler for nodes that handle type conversions which are
the same for all (most) processors
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 ncgcnv;
{$i fpcdefs.inc}
interface
uses
node,ncnv,defutil,defcmp;
type
{ tcgtypeconvnode }
tcgtypeconvnode = class(ttypeconvnode)
protected
{$ifdef cpuflags}
{ CPUs without flags need a specific implementation of int -> bool }
procedure second_int_to_bool;override;
{$endif cpuflags}
procedure second_int_to_int;override;
procedure second_cstring_to_pchar;override;
procedure second_cstring_to_int;override;
procedure second_string_to_chararray;override;
procedure second_array_to_pointer;override;
procedure second_pointer_to_array;override;
procedure second_char_to_string;override;
procedure second_real_to_real;override;
procedure second_cord_to_pointer;override;
procedure second_proc_to_procvar;override;
procedure second_nil_to_methodprocvar;override;
procedure second_bool_to_int;override;
procedure second_bool_to_bool;override;
procedure second_ansistring_to_pchar;override;
procedure second_class_to_intf;override;
procedure second_char_to_char;override;
procedure second_elem_to_openarray;override;
procedure second_nothing;override;
public
procedure pass_generate_code;override;
end;
tcgasnode = class(tasnode)
procedure pass_generate_code;override;
end;
implementation
uses
cutils,verbose,globtype,globals,
aasmbase,aasmtai,aasmdata,aasmcpu,symconst,symdef,paramgr,
nutils,ncon,ncal,
cpubase,systems,
procinfo,pass_2,
cgbase,
cgutils,cgobj,hlcgobj,
ncgutil,
tgobj
;
procedure tcgtypeconvnode.second_int_to_int;
var
orgsize,
newsize : tcgsize;
ressize,
leftsize : longint;
begin
newsize:=def_cgsize(resultdef);
{ insert range check if not explicit conversion }
if not(nf_explicit in flags) then
hlcg.g_rangecheck(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef);
{ is the result size smaller? when typecasting from void
we always reuse the current location, because there is
nothing that we can load in a register }
ressize := resultdef.size;
leftsize := left.resultdef.size;
if ((ressize<>leftsize) or
is_bitpacked_access(left)) and
not is_void(left.resultdef) then
begin
location_copy(location,left.location);
{ reuse a loc_reference when the newsize is smaller than
than the original, else load it to a register }
if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
(ressize<leftsize) then
begin
location.size:=newsize;
if (target_info.endian = ENDIAN_BIG) then
begin
inc(location.reference.offset,leftsize-ressize);
location.reference.alignment:=newalignment(location.reference.alignment,leftsize-ressize);
end;
end
{$if not defined(cpu16bitalu) and not defined(cpu8bitalu) and not defined(m68k)}
{ FIXME: reg_cgsize incorrectly identifies m68k as "without subregisters" }
{ On targets without 8/16 bit register components, 8/16-bit operations
always adjust high bits of result, see 'maybeadjustresult' method in
respective cgcpu.pas. Therefore 8/16-bit locations are valid as larger
ones (except signed->unsigned, which still needs high bits cleared). }
else if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
(tcgsize2size[(reg_cgsize(left.location.register))]=sizeof(aint)) and
(ressize>leftsize) and
(newsize in [OS_32,OS_S32,OS_16,OS_S16]) and
(not is_signed(left.resultdef) or is_signed(resultdef)) then
location.size:=newsize
{$endif}
else
hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,false);
end
else
begin
{ no special loading is required, reuse current location }
{ that's not true, if you go from signed to unsiged or }
{ vice versa, you need sign extension/removal if the }
{ value is already in a register (at least for archs }
{ which don't have 8bit register components etc) (JM) }
location_copy(location,left.location);
location.size:=newsize;
orgsize := def_cgsize(left.resultdef);
if (ressize < sizeof(aint)) and
(location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
(orgsize <> newsize) then
begin
location.register := cg.getintregister(current_asmdata.CurrAsmList,newsize);
location.loc := LOC_REGISTER;
cg.a_load_reg_reg(current_asmdata.CurrAsmList,orgsize,newsize,left.location.register,location.register);
end;
end;
end;
{$ifdef cpuflags}
procedure tcgtypeconvnode.second_int_to_bool;
var
hregister : tregister;
href : treference;
resflags : tresflags;
hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
newsize : tcgsize;
begin
oldTrueLabel:=current_procinfo.CurrTrueLabel;
oldFalseLabel:=current_procinfo.CurrFalseLabel;
current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
secondpass(left);
if codegenerror then
exit;
{ Explicit typecasts from any ordinal type to a boolean type }
{ must not change the ordinal value }
if (nf_explicit in flags) and
not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
begin
{ overriding methods must be able to know in advance whether this
code path will be taken by checking expectloc, so they can call
the inherited method in that case }
if left.expectloc in [LOC_FLAGS,LOC_JUMP] then
internalerror(2014122901);
location_copy(location,left.location);
newsize:=def_cgsize(resultdef);
{ change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
else
location.size:=newsize;
current_procinfo.CurrTrueLabel:=oldTrueLabel;
current_procinfo.CurrFalseLabel:=oldFalseLabel;
exit;
end;
{ though ppc/ppc64 doesn't use the generic code, we need to ifdef here
because the code is included into the powerpc compilers }
{$if defined(POWERPC) or defined(POWERPC64)}
resflags.cr := RS_CR0;
resflags.flag:=F_NE;
{$elseif defined(mips)}
resflags.reg1:=NR_NO;
resflags.reg2:=NR_NO;
resflags.cond:=OC_NONE;
{$else}
{ Load left node into flag F_NE/F_E }
resflags:=F_NE;
{$endif defined(POWERPC) or defined(POWERPC64)}
case left.location.loc of
LOC_CREFERENCE,
LOC_REFERENCE :
begin
if left.location.size in [OS_64,OS_S64] then
begin
hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
href:=left.location.reference;
inc(href.offset,4);
cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
end
else
begin
hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
end;
end;
LOC_FLAGS :
begin
resflags:=left.location.resflags;
end;
LOC_REGISTER,LOC_CREGISTER :
begin
{$ifndef cpu64bitalu}
if left.location.size in [OS_64,OS_S64] then
begin
hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
end
else
{$endif cpu64bitalu}
begin
cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
end;
end;
LOC_JUMP :
begin
hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
current_asmdata.getjumplabel(hlabel);
cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
cg.a_label(current_asmdata.CurrAsmList,hlabel);
cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
end;
else
internalerror(200311301);
end;
{ load flags to register }
location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
if (is_cbool(resultdef)) then
cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,location.register,location.register);
current_procinfo.CurrTrueLabel:=oldTrueLabel;
current_procinfo.CurrFalseLabel:=oldFalseLabel;
end;
{$endif cpuflags}
procedure tcgtypeconvnode.second_cstring_to_pchar;
var
hr : treference;
begin
if left.nodetype<>stringconstn then
internalerror(200601131);
if not is_pchar(resultdef) and not is_pwidechar(resultdef) then
internalerror(2014032802);
location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
case tstringconstnode(left).cst_type of
cst_conststring :
begin
location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
end;
cst_shortstring :
begin
inc(left.location.reference.offset);
location.reference.alignment:=1;
location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
end;
cst_widestring,
cst_unicodestring,
cst_ansistring :
begin
if tstringconstnode(left).len=0 then
begin
{ FPC_EMPTYCHAR is a widechar -> 2 bytes }
reference_reset(hr,2);
hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR');
location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,hr,location.register);
end
else
begin
location_copy(location,left.location);
end;
end;
cst_longstring:
begin
{!!!!!!!}
internalerror(8888);
end;
else
internalerror(200808241);
end;
end;
procedure tcgtypeconvnode.second_cstring_to_int;
begin
{ this can't happen because constants are already processed in
pass 1 }
internalerror(200510013);
end;
procedure tcgtypeconvnode.second_string_to_chararray;
begin
if is_chararray(left.resultdef) then
begin
location_copy(location,left.location);
exit;
end;
{ should be handled already in resultdef pass (JM) }
internalerror(200108292);
end;
procedure tcgtypeconvnode.second_array_to_pointer;
begin
location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
end;
procedure tcgtypeconvnode.second_pointer_to_array;
begin
{ assume natural alignment }
location_reset_ref(location,LOC_REFERENCE,OS_NO,resultdef.alignment);
case left.location.loc of
LOC_CREGISTER,
LOC_REGISTER :
begin
{$ifdef cpu_uses_separate_address_registers}
if getregtype(left.location.register)<>R_ADDRESSREGISTER then
begin
location.reference.base:=rg.getaddressregister(current_asmdata.CurrAsmList);
cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
left.location.register,location.reference.base);
end
else
{$endif}
hlcg.reference_reset_base(location.reference,left.resultdef,left.location.register,0,location.reference.alignment);
end;
LOC_REFERENCE,
LOC_CREFERENCE,
{ tricky type casting of parameters can cause these locations, see tb0593.pp on x86_64-linux }
LOC_SUBSETREG,
LOC_CSUBSETREG,
LOC_SUBSETREF,
LOC_CSUBSETREF:
begin
hlcg.reference_reset_base(location.reference,left.resultdef,
hlcg.getaddressregister(current_asmdata.CurrAsmList,left.resultdef),0,location.reference.alignment);
hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,left.location,
location.reference.base);
if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
location_freetemp(current_asmdata.CurrAsmList,left.location);
end;
LOC_CONSTANT:
begin
location.reference.offset:=left.location.value;
end
else
internalerror(2002032216);
end;
end;
procedure tcgtypeconvnode.second_char_to_string;
begin
location_reset_ref(location,LOC_REFERENCE,OS_NO,2);
case tstringdef(resultdef).stringtype of
st_shortstring :
begin
tg.gethltemp(current_asmdata.CurrAsmList,cshortstringtype,256,tt_normal,location.reference);
hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,left.location,
location.reference);
location_freetemp(current_asmdata.CurrAsmList,left.location);
end;
{ the rest is removed in the resultdef pass and converted to compilerprocs }
else
internalerror(4179);
end;
end;
procedure tcgtypeconvnode.second_real_to_real;
{$ifdef x86}
var
tr: treference;
{$endif x86}
begin
location_reset(location,expectloc,def_cgsize(resultdef));
{$ifdef x86}
{ extended types in memory which should be loaded into the sse unit
must be converted by the fpu first, so force them to be loaded into
the fpu }
if (expectloc=LOC_MMREGISTER) and
(left.location.size in [OS_F80,OS_C64]) then
begin
if (left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
{ round them down to the proper precision }
tg.gethltemp(current_asmdata.currasmlist,resultdef,resultdef.size,tt_normal,tr);
cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,tr);
location_reset_ref(left.location,LOC_REFERENCE,location.size,tr.alignment);
left.location.reference:=tr;
left.resultdef:=resultdef;
end;
{$endif x86}
{ ARM VFP values are in integer registers when they are function results }
if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
case left.location.loc of
LOC_FPUREGISTER,
LOC_CFPUREGISTER:
begin
case expectloc of
LOC_FPUREGISTER:
begin
{ on sparc a move from double -> single means from two to one register. }
{ On all other platforms it also needs rounding to avoid that }
{ single(double_regvar) = double_regvar is true in all cases }
location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
hlcg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
end;
LOC_MMREGISTER:
begin
hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register,mms_movescalar);
end
else
internalerror(2003012262);
end;
exit
end;
LOC_CREFERENCE,
LOC_REFERENCE:
begin
if expectloc=LOC_MMREGISTER then
begin
location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
hlcg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location,location.register,mms_movescalar)
end
else
begin
hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
hlcg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
end;
location_freetemp(current_asmdata.CurrAsmList,left.location);
end;
LOC_MMREGISTER,
LOC_CMMREGISTER:
begin
case expectloc of
LOC_FPUREGISTER:
begin
hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register);
end;
LOC_MMREGISTER:
begin
location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register,mms_movescalar);
end;
else
internalerror(2003012261);
end;
end;
else
internalerror(2002032215);
end;
end;
procedure tcgtypeconvnode.second_cord_to_pointer;
begin
{ this can't happen because constants are already processed in
pass 1 }
internalerror(47423985);
end;
procedure tcgtypeconvnode.second_proc_to_procvar;
var
tmpreg: tregister;
begin
if tabstractprocdef(resultdef).is_addressonly then
begin
location_reset(location,LOC_REGISTER,def_cgsize(voidcodepointertype));
{ only a code pointer? (when taking the address of classtype.method
we also only get a code pointer even though the resultdef is a
procedure of object, and hence is_addressonly would return false)
}
if left.location.size = def_cgsize(voidcodepointertype) then
begin
case left.location.loc of
LOC_REFERENCE,LOC_CREFERENCE:
begin
{ the procedure symbol is encoded in reference.symbol -> take address }
location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidcodepointertype);
hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,voidcodepointertype,left.location.reference,location.register);
end;
else
internalerror(2013031501)
end;
end
else
begin
{ conversion from a procedure of object/nested procvar to plain procvar }
case left.location.loc of
LOC_REFERENCE,LOC_CREFERENCE:
begin
location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidcodepointertype);
{ code field is the first one }
hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,voidcodepointertype,voidcodepointertype,left.location.reference,location.register);
end;
LOC_REGISTER,LOC_CREGISTER:
begin
if target_info.endian=endian_little then
location.register:=left.location.register
else
location.register:=left.location.registerhi;
end;
else
internalerror(2013031502)
end;
end;
end
else
begin
if not tabstractprocdef(left.resultdef).is_addressonly then
location_copy(location,left.location)
else
begin
{ assigning a global function to a nested procvar -> create
tmethodpointer record and set the "frame pointer" to nil }
if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
internalerror(2013031503);
location_reset_ref(location,LOC_REFERENCE,int_cgsize(resultdef.size),sizeof(pint));
tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,location.reference);
tmpreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidcodepointertype);
hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,voidcodepointertype,left.location.reference,tmpreg);
hlcg.a_load_reg_ref(current_asmdata.CurrAsmList,voidcodepointertype,voidcodepointertype,tmpreg,location.reference);
{ setting the frame pointer to nil is not strictly necessary
since the global procedure won't use it, but it can help with
debugging }
inc(location.reference.offset,voidcodepointertype.size);
if (resultdef.typ=procvardef) and is_nested_pd(tprocvardef(resultdef)) then
hlcg.a_load_const_ref(current_asmdata.CurrAsmList,parentfpvoidpointertype,0,location.reference)
else if tabstractprocdef(resultdef).is_methodpointer then
hlcg.a_load_const_ref(current_asmdata.CurrAsmList,voidpointertype,0,location.reference)
else
internalerror(2014052301);
dec(location.reference.offset,voidcodepointertype.size);
end;
end;
end;
procedure Tcgtypeconvnode.second_nil_to_methodprocvar;
begin
location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
location.registerhi:=hlcg.getaddressregister(current_asmdata.currasmlist,voidpointertype);
hlcg.a_load_const_reg(current_asmdata.currasmlist,voidpointertype,0,location.registerhi);
location.register:=hlcg.getaddressregister(current_asmdata.currasmlist,voidcodepointertype);
hlcg.a_load_const_reg(current_asmdata.currasmlist,voidcodepointertype,0,location.register);
end;
procedure tcgtypeconvnode.second_bool_to_int;
var
newsize: tcgsize;
oldTrueLabel,oldFalseLabel : tasmlabel;
begin
oldTrueLabel:=current_procinfo.CurrTrueLabel;
oldFalseLabel:=current_procinfo.CurrFalseLabel;
current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
secondpass(left);
location_copy(location,left.location);
newsize:=def_cgsize(resultdef);
{ byte(bytebool) or word(wordbool) or longint(longbool) must be }
{ accepted for var parameters and assignments, and must not }
{ change the ordinal value or value location. }
{ htypechk.valid_for_assign ensures that such locations with a }
{ size<sizeof(register) cannot be LOC_CREGISTER (they otherwise }
{ could be in case of a plain assignment), and LOC_REGISTER can }
{ never be an assignment target. The remaining LOC_REGISTER/ }
{ LOC_CREGISTER locations do have to be sign/zero-extended. }
if not(nf_explicit in flags) or
(location.loc in [LOC_FLAGS,LOC_JUMP]) or
{ change of size/signedness? Then we have to sign/ }
{ zero-extend in case of a loc_(c)register }
((newsize<>left.location.size) and
((left.resultdef.size<>resultdef.size) or
not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]))) then
hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
else
{ may differ in sign, e.g. bytebool -> byte }
location.size:=newsize;
current_procinfo.CurrTrueLabel:=oldTrueLabel;
current_procinfo.CurrFalseLabel:=oldFalseLabel;
end;
procedure tcgtypeconvnode.second_bool_to_bool;
begin
{ we can reuse the conversion already available
in bool_to_int to resize the value. But when the
size of the new boolean is smaller we need to calculate
the value as is done in int_to_bool. This is needed because
the bits that define the true status can be outside the limits
of the new size and truncating the register can result in a 0
value }
if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
{ a cbool must be converted to -1/0 }
not is_cbool(resultdef) then
begin
secondpass(left);
if (left.location.loc <> left.expectloc) then
internalerror(2010081601);
location_copy(location,left.location);
end
else if (resultdef.size=left.resultdef.size) and
(is_cbool(resultdef)=is_cbool(left.resultdef)) then
second_bool_to_int
else
begin
if (resultdef.size<>left.resultdef.size) then
{ remove nf_explicit to perform full conversion if boolean sizes are different }
exclude(flags, nf_explicit);
second_int_to_bool;
end;
end;
procedure tcgtypeconvnode.second_ansistring_to_pchar;
var
l1 : tasmlabel;
hr : treference;
begin
location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
current_asmdata.getjumplabel(l1);
location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,
left.location,location.register);
hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,resultdef,OC_NE,0,location.register,l1);
{ FPC_EMPTYCHAR is a widechar -> 2 bytes }
reference_reset(hr,2);
hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR');
hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,hr,location.register);
hlcg.a_label(current_asmdata.CurrAsmList,l1);
end;
procedure tcgtypeconvnode.second_class_to_intf;
var
l1 : tasmlabel;
hd : tobjectdef;
ImplIntf : TImplementedInterface;
begin
l1:=nil;
location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
case left.location.loc of
LOC_CREFERENCE,
LOC_REFERENCE:
begin
location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
location_freetemp(current_asmdata.CurrAsmList,left.location);
end;
LOC_CREGISTER:
begin
location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
end;
LOC_REGISTER:
location.register:=left.location.register;
else
internalerror(121120001);
end;
hd:=tobjectdef(left.resultdef);
while assigned(hd) do
begin
ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
if assigned(ImplIntf) then
begin
case ImplIntf.IType of
etStandard:
begin
current_asmdata.getjumplabel(l1);
hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,resultdef,OC_EQ,0,location.register,l1);
{ todo: consider adding far pointer support to hlcg.a_op_const_reg for i8086 (i.e. perform the
arithmetic operation only on the offset), then the next line can be converted to the
high level code generator as well }
cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,ImplIntf.ioffset,location.register);
break;
end;
else
internalerror(200802163);
end;
end;
hd:=hd.childof;
end;
if hd=nil then
internalerror(2002081301);
if l1=nil then
internalerror(2013120101);
cg.a_label(current_asmdata.CurrAsmList,l1);
end;
procedure tcgtypeconvnode.second_char_to_char;
begin
internalerror(2007081202);
end;
procedure tcgtypeconvnode.second_elem_to_openarray;
begin
{ nothing special to do by default }
second_nothing;
end;
procedure tcgtypeconvnode.second_nothing;
var
newsize : tcgsize;
begin
{ we reuse the old value }
location_copy(location,left.location);
{ Floats should never be returned as LOC_CONSTANT, do the
moving to memory before the new size is set.
Also when converting from a float to a non-float
move to memory first to prevent
invalid LOC_(C)MM/FPUREGISTER locations }
if (
(resultdef.typ=floatdef) and
(location.loc=LOC_CONSTANT)
) or
((resultdef.typ=floatdef) xor (location.loc in [LOC_CFPUREGISTER,LOC_FPUREGISTER,LOC_CMMREGISTER,LOC_MMREGISTER])) then
hlcg.location_force_mem(current_asmdata.CurrAsmList,location,left.resultdef);
{ but use the new size, but we don't know the size of all arrays }
newsize:=def_cgsize(resultdef);
location.size:=newsize;
end;
{$ifdef TESTOBJEXT2}
procedure tcgtypeconvnode.checkobject;
begin
{ no checking by default }
end;
{$endif TESTOBJEXT2}
procedure tcgtypeconvnode.pass_generate_code;
begin
{ the boolean routines can be called with LOC_JUMP and
call secondpass themselves in the helper }
if not(convtype in [tc_bool_2_int,tc_bool_2_bool,tc_int_2_bool]) then
begin
secondpass(left);
if codegenerror then
exit;
end;
second_call_helper(convtype);
{$ifdef TESTOBJEXT2}
{ Check explicit conversions to objects pointers !! }
if p^.explizit and
(p^.resultdef.typ=pointerdef) and
(tpointerdef(p^.resultdef).definition.typ=objectdef) and not
(tobjectdef(tpointerdef(p^.resultdef).definition).isclass) and
((tobjectdef(tpointerdef(p^.resultdef).definition).options and oo_hasvmt)<>0) and
(cs_check_range in current_settings.localswitches) then
checkobject;
{$endif TESTOBJEXT2}
end;
procedure tcgasnode.pass_generate_code;
begin
secondpass(call);
location_copy(location,call.location);
end;
begin
ctypeconvnode := tcgtypeconvnode;
casnode := tcgasnode;
end.
|