nppccal.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. Implements the PowerPC specific part of call nodes
  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 bymethodpointer
  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 nppccal;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symdef,node,ncal,ncgcal;
  22. type
  23. tppccallnode = class(tcgcallnode)
  24. procedure extra_call_code;override;
  25. procedure do_syscall;override;
  26. end;
  27. implementation
  28. uses
  29. globtype,systems,
  30. cutils,verbose,globals,
  31. symconst,symbase,symsym,symtable,defutil,paramgr,parabase,
  32. cgbase,pass_2,
  33. cpuinfo,cpubase,aasmbase,aasmtai,aasmcpu,
  34. nmem,nld,ncnv,
  35. ncgutil,cgutils,cgobj,tgobj,regvars,rgobj,rgcpu,
  36. cg64f32,cgcpu,cpupi,procinfo;
  37. procedure tppccallnode.extra_call_code;
  38. begin
  39. if assigned(varargsparas) then
  40. begin
  41. if (target_info.abi = abi_powerpc_sysv) then
  42. begin
  43. if va_uses_float_reg in varargsparas.varargsinfo then
  44. exprasmlist.concat(taicpu.op_const_const_const(A_CREQV,6,6,6))
  45. else
  46. exprasmlist.concat(taicpu.op_const_const_const(A_CRXOR,6,6,6));
  47. end;
  48. end;
  49. end;
  50. procedure tppccallnode.do_syscall;
  51. var
  52. tmpref: treference;
  53. begin
  54. case target_info.system of
  55. system_powerpc_amiga:
  56. begin
  57. // one syscall convention for Amiga/PowerPC
  58. // which is quite standard
  59. extra_call_code;
  60. cg.a_call_name(exprasmlist,tprocdef(procdefinition).mangledname);
  61. extra_post_call_code;
  62. end;
  63. system_powerpc_morphos:
  64. begin
  65. if (po_syscall_sysv in tprocdef(procdefinition).procoptions) or
  66. (po_syscall_sysvbase in tprocdef(procdefinition).procoptions) then
  67. begin
  68. cg.getcpuregister(exprasmlist,NR_R0);
  69. cg.getcpuregister(exprasmlist,NR_R31);
  70. reference_reset(tmpref);
  71. tmpref.symbol:=objectlibrary.newasmsymbol(tglobalvarsym(tprocdef(procdefinition).libsym).mangledname,AB_EXTERNAL,AT_DATA);
  72. tmpref.refaddr:=addr_hi;
  73. exprasmlist.concat(taicpu.op_reg_ref(A_LIS,NR_R31,tmpref));
  74. tmpref.base:=NR_R31;
  75. tmpref.refaddr:=addr_lo;
  76. exprasmlist.concat(taicpu.op_reg_ref(A_LWZ,NR_R31,tmpref));
  77. exprasmlist.concat(taicpu.op_reg_reg_const(A_ADDI,NR_R31,NR_R31,-tprocdef(procdefinition).extnumber));
  78. reference_reset_base(tmpref,NR_R31,0);
  79. exprasmlist.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,tmpref));
  80. exprasmlist.concat(taicpu.op_reg(A_MTCTR,NR_R0));
  81. exprasmlist.concat(taicpu.op_none(A_BCTRL));
  82. cg.ungetcpuregister(exprasmlist,NR_R31);
  83. cg.ungetcpuregister(exprasmlist,NR_R0);
  84. end
  85. else if (po_syscall_basesysv in tprocdef(procdefinition).procoptions) or
  86. (po_syscall_r12base in tprocdef(procdefinition).procoptions) then
  87. begin
  88. cg.getcpuregister(exprasmlist,NR_R0);
  89. cg.getcpuregister(exprasmlist,NR_R31);
  90. if (po_syscall_basesysv in tprocdef(procdefinition).procoptions) then
  91. exprasmlist.concat(taicpu.op_reg_reg_const(A_ADDI,NR_R31,NR_R3,-tprocdef(procdefinition).extnumber))
  92. else
  93. exprasmlist.concat(taicpu.op_reg_reg_const(A_ADDI,NR_R31,NR_R12,-tprocdef(procdefinition).extnumber));
  94. reference_reset_base(tmpref,NR_R31,0);
  95. exprasmlist.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,tmpref));
  96. exprasmlist.concat(taicpu.op_reg(A_MTCTR,NR_R0));
  97. exprasmlist.concat(taicpu.op_none(A_BCTRL));
  98. cg.ungetcpuregister(exprasmlist,NR_R31);
  99. cg.ungetcpuregister(exprasmlist,NR_R0);
  100. end
  101. else if po_syscall_legacy in tprocdef(procdefinition).procoptions then
  102. begin
  103. cg.getcpuregister(exprasmlist,NR_R0);
  104. cg.getcpuregister(exprasmlist,NR_R3);
  105. { store call offset into R3 }
  106. exprasmlist.concat(taicpu.op_reg_const(A_LI,NR_R3,-tprocdef(procdefinition).extnumber));
  107. { prepare LR, and call function }
  108. reference_reset_base(tmpref,NR_R2,100); { 100 ($64) is EmulDirectCallOS offset }
  109. exprasmlist.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,tmpref));
  110. exprasmlist.concat(taicpu.op_reg(A_MTLR,NR_R0));
  111. exprasmlist.concat(taicpu.op_none(A_BLRL));
  112. cg.ungetcpuregister(exprasmlist,NR_R0);
  113. cg.ungetcpuregister(exprasmlist,NR_R3);
  114. end
  115. else
  116. internalerror(2005010403);
  117. end;
  118. else
  119. internalerror(2004042901);
  120. end;
  121. end;
  122. begin
  123. ccallnode:=tppccallnode;
  124. end.