cgcpu.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. {
  2. Copyright (c) 2019 by Dmitry Boyarintsev
  3. This unit implements the code generator for the WebAssembly
  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 cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,parabase,
  22. cgbase,cgutils,cgobj,cghlcpu,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,
  24. cpubase,cpuinfo,
  25. node,symconst,SymType,symdef,
  26. rgcpu;
  27. type
  28. TCgWasm=class(thlbasecgcpu)
  29. public
  30. procedure init_register_allocators;override;
  31. procedure done_register_allocators;override;
  32. function getintregister(list:TAsmList;size:Tcgsize):Tregister;override;
  33. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
  34. function getaddressregister(list:TAsmList):Tregister;override;
  35. function getfuncrefregister(list:TAsmList):Tregister;
  36. function getexternrefregister(list:TAsmList):Tregister;
  37. procedure do_register_allocation(list:TAsmList;headertai:tai);override;
  38. end;
  39. procedure create_codegen;
  40. implementation
  41. uses
  42. globals,verbose,systems,cutils,
  43. paramgr,fmodule,
  44. tgobj,
  45. procinfo,cpupi;
  46. {****************************************************************************
  47. Assembler code
  48. ****************************************************************************}
  49. procedure TCgWasm.init_register_allocators;
  50. begin
  51. inherited init_register_allocators;
  52. {$ifndef cpu64bitaddr}
  53. rg[R_INTREGISTER]:=Trgcpu.create(R_INTREGISTER,R_SUBD,
  54. [RS_R0],first_int_imreg,[]);
  55. {$else not cpu64bitaddr}
  56. rg[R_INTREGISTER]:=Trgcpu.create(R_INTREGISTER,R_SUBQ,
  57. [RS_R0],first_int_imreg,[]);
  58. {$endif not cpu64bitaddr}
  59. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBFS,
  60. [RS_R0],first_fpu_imreg,[]);
  61. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  62. [RS_R0],first_mm_imreg,[]);
  63. rg[R_FUNCREFREGISTER]:=Trgcpu.create(R_FUNCREFREGISTER,R_SUBNONE,
  64. [RS_R0],first_funcref_imreg,[]);
  65. rg[R_EXTERNREFREGISTER]:=Trgcpu.create(R_EXTERNREFREGISTER,R_SUBNONE,
  66. [RS_R0],first_externref_imreg,[]);
  67. end;
  68. procedure TCgWasm.done_register_allocators;
  69. begin
  70. rg[R_INTREGISTER].free;
  71. rg[R_FPUREGISTER].free;
  72. rg[R_MMREGISTER].free;
  73. rg[R_FUNCREFREGISTER].free;
  74. rg[R_EXTERNREFREGISTER].free;
  75. inherited done_register_allocators;
  76. end;
  77. function TCgWasm.getintregister(list:TAsmList;size:Tcgsize):Tregister;
  78. begin
  79. if not(size in [OS_64,OS_S64]) then
  80. result:=rg[R_INTREGISTER].getregister(list,R_SUBD)
  81. else
  82. result:=rg[R_INTREGISTER].getregister(list,R_SUBQ);
  83. end;
  84. function TCgWasm.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  85. begin
  86. if size=OS_F64 then
  87. result:=rg[R_FPUREGISTER].getregister(list,R_SUBFD)
  88. else
  89. result:=rg[R_FPUREGISTER].getregister(list,R_SUBFS);
  90. end;
  91. function tcgwasm.getaddressregister(list:TAsmList):Tregister;
  92. begin
  93. { avoid problems in the compiler where int and addr registers are
  94. mixed for now; we currently don't have to differentiate between the
  95. two as far as the jvm backend is concerned }
  96. result:=rg[R_INTREGISTER].getregister(list,R_SUBD)
  97. end;
  98. function tcgwasm.getfuncrefregister(list:TAsmList):Tregister;
  99. begin
  100. result:=rg[R_FUNCREFREGISTER].getregister(list,R_SUBNONE);
  101. end;
  102. function tcgwasm.getexternrefregister(list:TAsmList):Tregister;
  103. begin
  104. result:=rg[R_EXTERNREFREGISTER].getregister(list,R_SUBNONE);
  105. end;
  106. procedure tcgwasm.do_register_allocation(list:TAsmList;headertai:tai);
  107. begin
  108. { We only run the "register allocation" once for an arbitrary allocator,
  109. which will perform the register->temp mapping for all register types.
  110. This allows us to easily reuse temps. }
  111. trgcpu(rg[R_INTREGISTER]).do_all_register_allocation(list,headertai);
  112. end;
  113. procedure create_codegen;
  114. begin
  115. cg:=tcgwasm.Create;
  116. end;
  117. end.