2
0

nrvcnv.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate Risc-V assembler for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nrvcnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncnv,ncgcnv;
  22. type
  23. trvtypeconvnode = class(tcgtypeconvnode)
  24. protected
  25. { procedure second_int_to_int;override; }
  26. { procedure second_string_to_string;override; }
  27. { procedure second_cstring_to_pchar;override; }
  28. { procedure second_string_to_chararray;override; }
  29. { procedure second_array_to_pointer;override; }
  30. { function first_int_to_real: tnode; override; }
  31. { procedure second_pointer_to_array;override; }
  32. { procedure second_chararray_to_string;override; }
  33. { procedure second_char_to_string;override; }
  34. { procedure second_int_to_real;override; }
  35. { procedure second_real_to_real;override; }
  36. { procedure second_cord_to_pointer;override; }
  37. { procedure second_proc_to_procvar;override; }
  38. { procedure second_bool_to_int;override; }
  39. procedure second_int_to_bool;override;
  40. { procedure second_load_smallset;override; }
  41. { procedure second_ansistring_to_pchar;override; }
  42. { procedure second_pchar_to_string;override; }
  43. { procedure second_class_to_intf;override; }
  44. { procedure second_char_to_char;override; }
  45. end;
  46. implementation
  47. uses
  48. verbose,globtype,globals,systems,
  49. symconst,symdef,aasmbase,aasmtai,aasmdata,
  50. defutil,
  51. cgbase,cgutils,pass_1,pass_2,
  52. ncgutil,procinfo,
  53. cpubase,aasmcpu,
  54. rgobj,tgobj,cgobj,hlcgobj;
  55. procedure trvtypeconvnode.second_int_to_bool;
  56. var
  57. hreg1, hreg2: tregister;
  58. opsize: tcgsize;
  59. hlabel: tasmlabel;
  60. newsize : tcgsize;
  61. href: treference;
  62. begin
  63. secondpass(left);
  64. if codegenerror then
  65. exit;
  66. { Explicit typecasts from any ordinal type to a boolean type }
  67. { must not change the ordinal value }
  68. if (nf_explicit in flags) and
  69. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  70. begin
  71. location_copy(location,left.location);
  72. newsize:=def_cgsize(resultdef);
  73. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  74. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  75. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  76. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  77. else
  78. location.size:=newsize;
  79. exit;
  80. end;
  81. location_reset(location, LOC_REGISTER, def_cgsize(resultdef));
  82. opsize := def_cgsize(left.resultdef);
  83. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  84. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  85. case left.location.loc of
  86. LOC_CREFERENCE, LOC_REFERENCE, LOC_REGISTER, LOC_CREGISTER:
  87. begin
  88. if left.location.loc in [LOC_CREFERENCE, LOC_REFERENCE] then
  89. begin
  90. hreg2 := cg.getintregister(current_asmdata.CurrAsmList, opsize);
  91. {$ifndef cpu64bitalu}
  92. if left.location.size in [OS_64,OS_S64] then
  93. begin
  94. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,left.location.reference,hreg2);
  95. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  96. href:=left.location.reference;
  97. inc(href.offset,4);
  98. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,href,hreg1);
  99. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hreg1,hreg2,hreg2);
  100. end
  101. else
  102. {$endif not cpu64bitalu}
  103. cg.a_load_ref_reg(current_asmdata.CurrAsmList, opsize, opsize, left.location.reference, hreg2);
  104. end
  105. else
  106. begin
  107. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  108. {$ifndef cpu64bitalu}
  109. if left.location.size in [OS_64,OS_S64] then
  110. begin
  111. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  112. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,left.location.register64.reglo,hreg2);
  113. end
  114. else
  115. {$endif not cpu64bitalu}
  116. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,hreg2);
  117. end;
  118. hreg1 := cg.getintregister(current_asmdata.CurrAsmList, opsize);
  119. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SLTU, hreg1, NR_X0, hreg2));
  120. end;
  121. LOC_JUMP:
  122. begin
  123. hreg1 := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  124. current_asmdata.getjumplabel(hlabel);
  125. cg.a_label(current_asmdata.CurrAsmList, left.location.truelabel);
  126. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, 1, hreg1);
  127. cg.a_jmp_always(current_asmdata.CurrAsmList, hlabel);
  128. cg.a_label(current_asmdata.CurrAsmList, left.location.falselabel);
  129. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, 0, hreg1);
  130. cg.a_label(current_asmdata.CurrAsmList, hlabel);
  131. end;
  132. LOC_FLAGS:
  133. begin
  134. Internalerror(2016060403);
  135. end
  136. else
  137. internalerror(10062);
  138. end;
  139. { Now hreg1 is either 0 or 1. For C booleans it must be 0 or -1. }
  140. if is_cbool(resultdef) then
  141. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_SINT,hreg1,hreg1);
  142. {$ifndef cpu64bitalu}
  143. if (location.size in [OS_64,OS_S64]) then
  144. begin
  145. location.register64.reglo:=hreg1;
  146. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  147. if (is_cbool(resultdef)) then
  148. { reglo is either 0 or -1 -> reghi has to become the same }
  149. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  150. else
  151. { unsigned }
  152. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  153. end
  154. else
  155. {$endif not cpu64bitalu}
  156. location.Register := hreg1;
  157. end;
  158. end.