ncpuutil.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. {
  2. Copyright (c) 2015 by Jeppe Johansen
  3. Xtensa 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 ncpuutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. node,nbas,
  23. ngenutil,
  24. symtype,symconst,symsym,symdef,fmodule;
  25. type
  26. txtensanodeutils = class(tnodeutils)
  27. protected
  28. class procedure insert_init_final_table(main: tmodule; entries:tfplist); override;
  29. end;
  30. implementation
  31. uses
  32. verbose,cutils,globtype,globals,constexp,
  33. aasmdata,aasmtai,aasmcpu,aasmcnst,aasmbase,
  34. cpubase,
  35. symbase,symcpu,symtable,defutil,
  36. ncnv,ncon,ninl,ncal,nld,nmem,
  37. systems,
  38. CPUInfo,
  39. ppu,
  40. pass_1;
  41. class procedure txtensanodeutils.insert_init_final_table(main: tmodule; entries:tfplist);
  42. var
  43. callop, retop: TAsmOp;
  44. initList, finalList, header: TAsmList;
  45. entry : pinitfinalentry;
  46. i : longint;
  47. begin
  48. initList:=TAsmList.create;
  49. finalList:=TAsmList.create;
  50. initList.Concat(tai_align.Create(target_info.alignment.procalign));
  51. finalList.Concat(tai_align.Create(target_info.alignment.procalign));
  52. case target_info.abi of
  53. abi_xtensa_call0:
  54. begin
  55. // Store return address on stack
  56. initlist.concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,-16));
  57. initlist.concat(taicpu.op_reg_reg_const(A_S32I, NR_A0, NR_A1, 12));
  58. finalList.concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,-16));
  59. finalList.concat(taicpu.op_reg_reg_const(A_S32I, NR_A0, NR_A1, 12));
  60. callop:=A_CALL0;
  61. retop:=A_RET;
  62. end;
  63. abi_xtensa_windowed:
  64. begin
  65. initList.Concat(taicpu.op_reg_const(A_ENTRY,NR_A1,16));
  66. finalList.Concat(taicpu.op_reg_const(A_ENTRY,NR_A1,16));
  67. callop:=A_CALL4;
  68. retop:=A_RETW;
  69. end;
  70. else
  71. Internalerror(2020031501);
  72. end;
  73. for i:=0 to entries.count-1 do
  74. begin
  75. entry:=pinitfinalentry(entries[i]);
  76. if entry^.finifunc<>'' then
  77. finalList.Concat(taicpu.op_sym(callop,current_asmdata.RefAsmSymbol(entry^.finifunc,AT_FUNCTION)));
  78. if entry^.initfunc<>'' then
  79. initList.Concat(taicpu.op_sym(callop,current_asmdata.RefAsmSymbol(entry^.initfunc,AT_FUNCTION)));
  80. end;
  81. // Restore return address for call0 ABI
  82. if target_info.abi = abi_xtensa_call0 then
  83. begin
  84. initlist.concat(taicpu.op_reg_reg_const(A_L32I, NR_A0, NR_A1, 12));
  85. initlist.concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,16));
  86. finalList.concat(taicpu.op_reg_reg_const(A_L32I, NR_A0, NR_A1, 12));
  87. finalList.concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,16));
  88. end;
  89. initList.Concat(taicpu.op_none(retop));
  90. finalList.Concat(taicpu.op_none(retop));
  91. header:=TAsmList.create;
  92. new_section(header, sec_code, 'FPC_INIT_FUNC_TABLE', 1);
  93. header.concat(tai_symbol.Createname_global('FPC_INIT_FUNC_TABLE',AT_FUNCTION,0,voidcodepointertype));
  94. initList.insertList(header);
  95. header.free;
  96. current_asmdata.AsmLists[al_procedures].concatList(initList);
  97. header:=TAsmList.create;
  98. new_section(header, sec_code, 'FPC_FINALIZE_FUNC_TABLE', 1);
  99. header.concat(tai_symbol.Createname_global('FPC_FINALIZE_FUNC_TABLE',AT_FUNCTION,0,voidcodepointertype));
  100. finalList.insertList(header);
  101. header.free;
  102. current_asmdata.AsmLists[al_procedures].concatList(finalList);
  103. initList.Free;
  104. finalList.Free;
  105. inherited insert_init_final_table(main,entries);
  106. end;
  107. begin
  108. cnodeutils:=txtensanodeutils;
  109. end.