hlcgcpu.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit contains routines to create a pass-through high-level code
  5. generator. This is used by most regular code generators.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit hlcgcpu;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. aasmbase, aasmdata,
  25. cgbase, cgutils,
  26. symconst,symtype,symdef,
  27. parabase, hlcgobj, hlcg2ll;
  28. type
  29. thlcgmips = class(thlcg2ll)
  30. function a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef; weak: boolean): tcgpara; override;
  31. procedure a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);override;
  32. protected
  33. procedure a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); override;
  34. end;
  35. procedure create_hlcodegen;
  36. implementation
  37. uses
  38. verbose,
  39. aasmtai,
  40. aasmcpu,
  41. cutils,
  42. globals,
  43. defutil,
  44. cgobj,
  45. cpubase,
  46. cpuinfo,
  47. cgcpu;
  48. function thlcgmips.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef; weak: boolean): tcgpara;
  49. var
  50. ref: treference;
  51. sym: tasmsymbol;
  52. begin
  53. if weak then
  54. sym:=current_asmdata.WeakRefAsmSymbol(s)
  55. else
  56. sym:=current_asmdata.RefAsmSymbol(s);
  57. if (po_external in pd.procoptions) then
  58. begin
  59. if not (cs_create_pic in current_settings.moduleswitches) then
  60. begin
  61. reference_reset_symbol(ref,current_asmdata.RefAsmSymbol('_gp'),0,sizeof(aint));
  62. list.concat(tai_comment.create(strpnew('Using PIC code for a_call_name')));
  63. cg.a_loadaddr_ref_reg(list,ref,NR_GP);
  64. end;
  65. TCGMIPS(cg).a_call_sym_pic(list,sym);
  66. end
  67. else
  68. cg.a_call_name(list,s,weak);
  69. { set the result location }
  70. result:=get_call_result_cgpara(pd,forceresdef);
  71. end;
  72. procedure thlcgmips.a_load_subsetreg_reg(list: TAsmList; subsetsize, tosize: tdef; const sreg: tsubsetregister; destreg: tregister);
  73. var
  74. cgsubsetsize,
  75. cgtosize: tcgsize;
  76. begin
  77. cgsubsetsize:=def_cgsize(subsetsize);
  78. cgtosize:=def_cgsize(tosize);
  79. if (current_settings.cputype<>cpu_mips32r2) and (current_settings.cputype<>cpu_pic32mx) then
  80. inherited a_load_subsetreg_reg(list,subsetsize,tosize,sreg,destreg)
  81. else if (sreg.bitlen>32) then
  82. InternalError(2013070201)
  83. else if (sreg.bitlen<>32) then
  84. begin
  85. list.concat(taicpu.op_reg_reg_const_const(A_EXT,destreg,sreg.subsetreg,
  86. sreg.startbit,sreg.bitlen));
  87. { types with a negative lower bound are always a base type (8, 16, 32 bits) }
  88. if (cgsubsetsize in [OS_S8..OS_S128]) then
  89. if ((sreg.bitlen mod 8) = 0) then
  90. begin
  91. cg.a_load_reg_reg(list,tcgsize2unsigned[cgsubsetsize],cgsubsetsize,destreg,destreg);
  92. cg.a_load_reg_reg(list,cgsubsetsize,cgtosize,destreg,destreg);
  93. end
  94. else
  95. begin
  96. cg.a_op_const_reg(list,OP_SHL,OS_INT,32-sreg.bitlen,destreg);
  97. cg.a_op_const_reg(list,OP_SAR,OS_INT,32-sreg.bitlen,destreg);
  98. end;
  99. end
  100. else
  101. cg.a_load_reg_reg(list,cgsubsetsize,cgtosize,sreg.subsetreg,destreg);
  102. end;
  103. procedure thlcgmips.a_load_regconst_subsetreg_intern(list: TAsmList; fromsize, subsetsize: tdef; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  104. begin
  105. if (current_settings.cputype<>cpu_mips32r2) and (current_settings.cputype<>cpu_pic32mx) then
  106. inherited a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,slopt)
  107. else if (sreg.bitlen>32) then
  108. InternalError(2013070202)
  109. else if (sreg.bitlen<>32) then
  110. begin
  111. case slopt of
  112. SL_SETZERO:
  113. fromreg:=NR_R0;
  114. SL_SETMAX:
  115. begin
  116. fromreg:=cg.getintregister(list,OS_INT);
  117. cg.a_load_const_reg(list,OS_INT,-1,fromreg);
  118. end;
  119. end;
  120. list.concat(taicpu.op_reg_reg_const_const(A_INS,sreg.subsetreg,fromreg,
  121. sreg.startbit,sreg.bitlen));
  122. end
  123. else if not (slopt in [SL_SETZERO,SL_SETMAX]) then
  124. cg.a_load_reg_reg(list,def_cgsize(fromsize),def_cgsize(subsetsize),fromreg,sreg.subsetreg)
  125. else
  126. inherited a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,slopt);
  127. end;
  128. procedure create_hlcodegen;
  129. begin
  130. hlcg:=thlcgmips.create;
  131. create_codegen;
  132. end;
  133. end.