cgcpu.pas 4.4 KB

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