cgcpu.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. procedure a_label_pascal_goto_target(list : TAsmList;l : tasmlabel);override;
  39. end;
  40. procedure create_codegen;
  41. implementation
  42. uses
  43. globals,verbose,systems,cutils,
  44. paramgr,fmodule,
  45. tgobj,
  46. procinfo,cpupi;
  47. {****************************************************************************
  48. Assembler code
  49. ****************************************************************************}
  50. procedure TCgWasm.init_register_allocators;
  51. begin
  52. inherited init_register_allocators;
  53. {$ifndef cpu64bitaddr}
  54. rg[R_INTREGISTER]:=Trgcpu.create(R_INTREGISTER,R_SUBD,
  55. [RS_R0],first_int_imreg,[]);
  56. {$else not cpu64bitaddr}
  57. rg[R_INTREGISTER]:=Trgcpu.create(R_INTREGISTER,R_SUBQ,
  58. [RS_R0],first_int_imreg,[]);
  59. {$endif not cpu64bitaddr}
  60. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBFS,
  61. [RS_R0],first_fpu_imreg,[]);
  62. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  63. [RS_R0],first_mm_imreg,[]);
  64. rg[R_FUNCREFREGISTER]:=Trgcpu.create(R_FUNCREFREGISTER,R_SUBNONE,
  65. [RS_R0],first_funcref_imreg,[]);
  66. rg[R_EXTERNREFREGISTER]:=Trgcpu.create(R_EXTERNREFREGISTER,R_SUBNONE,
  67. [RS_R0],first_externref_imreg,[]);
  68. end;
  69. procedure TCgWasm.done_register_allocators;
  70. begin
  71. rg[R_INTREGISTER].free;
  72. rg[R_FPUREGISTER].free;
  73. rg[R_MMREGISTER].free;
  74. rg[R_FUNCREFREGISTER].free;
  75. rg[R_EXTERNREFREGISTER].free;
  76. inherited done_register_allocators;
  77. end;
  78. function TCgWasm.getintregister(list:TAsmList;size:Tcgsize):Tregister;
  79. begin
  80. if not(size in [OS_64,OS_S64]) then
  81. result:=rg[R_INTREGISTER].getregister(list,R_SUBD)
  82. else
  83. result:=rg[R_INTREGISTER].getregister(list,R_SUBQ);
  84. end;
  85. function TCgWasm.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  86. begin
  87. if size=OS_F64 then
  88. result:=rg[R_FPUREGISTER].getregister(list,R_SUBFD)
  89. else
  90. result:=rg[R_FPUREGISTER].getregister(list,R_SUBFS);
  91. end;
  92. function tcgwasm.getaddressregister(list:TAsmList):Tregister;
  93. begin
  94. { avoid problems in the compiler where int and addr registers are
  95. mixed for now; we currently don't have to differentiate between the
  96. two as far as the jvm backend is concerned }
  97. result:=rg[R_INTREGISTER].getregister(list,R_SUBD)
  98. end;
  99. function tcgwasm.getfuncrefregister(list:TAsmList):Tregister;
  100. begin
  101. result:=rg[R_FUNCREFREGISTER].getregister(list,R_SUBNONE);
  102. end;
  103. function tcgwasm.getexternrefregister(list:TAsmList):Tregister;
  104. begin
  105. result:=rg[R_EXTERNREFREGISTER].getregister(list,R_SUBNONE);
  106. end;
  107. procedure tcgwasm.do_register_allocation(list:TAsmList;headertai:tai);
  108. begin
  109. { We only run the "register allocation" once for an arbitrary allocator,
  110. which will perform the register->temp mapping for all register types.
  111. This allows us to easily reuse temps. }
  112. trgcpu(rg[R_INTREGISTER]).do_all_register_allocation(list,headertai);
  113. end;
  114. procedure tcgwasm.a_label_pascal_goto_target(list : TAsmList;l : tasmlabel);
  115. begin
  116. tcpuprocinfo(current_procinfo).add_goto_target(l);
  117. inherited;
  118. end;
  119. procedure create_codegen;
  120. begin
  121. cg:=tcgwasm.Create;
  122. end;
  123. end.