nrv32util.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. {
  2. Copyright (c) 2024
  3. RISCV32 version of some node tree helper routines
  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 nrv32util;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. fmodule,
  23. node,nbas,
  24. nrvutil,
  25. symtype,symconst,symsym,symdef;
  26. type
  27. trv32nodeutils = class(trvnodeutils)
  28. protected
  29. class procedure insert_init_final_table(main: tmodule; entries:tfplist); override;
  30. end;
  31. implementation
  32. uses
  33. verbose,cutils,globtype,globals,constexp,
  34. aasmdata,aasmtai,aasmcpu,aasmcnst,aasmbase,
  35. cpubase,
  36. cgutils,
  37. symbase,symcpu,symtable,defutil,
  38. ncnv,ncon,ninl,ncal,nld,nmem,
  39. systems,
  40. CPUInfo,
  41. ppu,
  42. pass_1,
  43. ngenutil;
  44. class procedure trv32nodeutils.insert_init_final_table(main: tmodule; entries:tfplist);
  45. procedure genentry(list : TAsmList);
  46. var
  47. ref: treference;
  48. begin
  49. // addi sp,sp,-16
  50. list.Concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,-16));
  51. // sw ra,12(sp)
  52. reference_reset(ref,4,[]);
  53. ref.base:=NR_STACK_POINTER_REG;
  54. ref.offset:=12;
  55. list.Concat(taicpu.op_reg_ref(A_SW,NR_RETURN_ADDRESS_REG,ref));
  56. end;
  57. procedure genexit(list : TAsmList);
  58. var
  59. ref: treference;
  60. begin
  61. // lw ra,12(sp)
  62. reference_reset(ref,4,[]);
  63. ref.base:=NR_STACK_POINTER_REG;
  64. ref.offset:=12;
  65. list.Concat(taicpu.op_reg_ref(A_LW,NR_RETURN_ADDRESS_REG,ref));
  66. // addi sp,sp,16
  67. list.Concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,16));
  68. // ret
  69. list.Concat(taicpu.op_none(A_RET));
  70. end;
  71. var
  72. initList, finalList, header: TAsmList;
  73. entry : pinitfinalentry;
  74. i : longint;
  75. ref:treference;
  76. begin
  77. if not(tf_init_final_units_by_calls in target_info.flags) then
  78. begin
  79. inherited insert_init_final_table(main,entries);
  80. exit;
  81. end;
  82. initList:=TAsmList.create;
  83. finalList:=TAsmList.create;
  84. genentry(initList);
  85. genentry(finalList);
  86. for i:=0 to entries.count-1 do
  87. begin
  88. entry:=pinitfinalentry(entries[i]);
  89. if entry^.finifunc<>'' then
  90. finalList.Concat(taicpu.op_sym(A_CALL,current_asmdata.RefAsmSymbol(entry^.finifunc,AT_FUNCTION)));
  91. if entry^.initfunc<>'' then
  92. initList.Concat(taicpu.op_sym(A_CALL,current_asmdata.RefAsmSymbol(entry^.initfunc,AT_FUNCTION)));
  93. end;
  94. genexit(initList);
  95. genexit(finalList);
  96. begin
  97. header:=TAsmList.create;
  98. new_section(header, sec_code, 'FPC_INIT_FUNC_TABLE', 1);
  99. header.concat(tai_symbol.Createname_global('FPC_INIT_FUNC_TABLE',AT_FUNCTION,0,voidcodepointertype));
  100. initList.insertList(header);
  101. header.free;
  102. current_asmdata.AsmLists[al_procedures].concatList(initList);
  103. end;
  104. begin
  105. header:=TAsmList.create;
  106. new_section(header, sec_code, 'FPC_FINALIZE_FUNC_TABLE', 1);
  107. header.concat(tai_symbol.Createname_global('FPC_FINALIZE_FUNC_TABLE',AT_FUNCTION,0,voidcodepointertype));
  108. finalList.insertList(header);
  109. header.free;
  110. current_asmdata.AsmLists[al_procedures].concatList(finalList);
  111. end;
  112. initList.Free;
  113. finalList.Free;
  114. inherited insert_init_final_table(main,entries);
  115. end;
  116. begin
  117. cnodeutils:=trv32nodeutils;
  118. end.