cgcpu.pas 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,symtype,
  23. cgbase,cgobj,
  24. aasmbase,aasmcpu,aasmtai,
  25. cpubase,cpuinfo,cgutils,cg64f32,rgcpu,
  26. parabase;
  27. type
  28. tcgppc = class(tcg)
  29. procedure init_register_allocators;override;
  30. procedure done_register_allocators;override;
  31. { passing parameters, per default the parameter is pushed }
  32. { nr gives the number of the parameter (enumerated from }
  33. { left to right), this allows to move the parameter to }
  34. { register, if the cpu supports register calling }
  35. { conventions }
  36. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aint;const paraloc : tcgpara);override;
  37. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const paraloc : tcgpara);override;
  38. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const paraloc : tcgpara);override;
  39. procedure a_call_name(list : taasmoutput;const s : string);override;
  40. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  41. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  42. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  43. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  44. size: tcgsize; a: aint; src, dst: tregister); override;
  45. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  46. size: tcgsize; src1, src2, dst: tregister); override;
  47. { move instructions }
  48. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aint;reg : tregister);override;
  49. procedure a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  50. procedure a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  51. procedure a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  52. { fpu move instructions }
  53. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  54. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  55. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  56. { comparison operations }
  57. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  58. l : tasmlabel);override;
  59. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  60. procedure a_jmp_name(list : taasmoutput;const s : string); override;
  61. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  62. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  63. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  64. procedure g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);override;
  65. procedure g_proc_exit(list : taasmoutput;parasize : longint;nostackframe:boolean); override;
  66. procedure g_save_standard_registers(list:Taasmoutput); override;
  67. procedure g_restore_standard_registers(list:Taasmoutput); override;
  68. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  69. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);override;
  70. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef); override;
  71. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  72. { that's the case, we can use rlwinm to do an AND operation }
  73. function get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  74. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  75. private
  76. (* NOT IN USE: *)
  77. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  78. (* NOT IN USE: *)
  79. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aint);
  80. { Make sure ref is a valid reference for the PowerPC and sets the }
  81. { base to the value of the index if (base = R_NO). }
  82. { Returns true if the reference contained a base, index and an }
  83. { offset or symbol, in which case the base will have been changed }
  84. { to a tempreg (which has to be freed by the caller) containing }
  85. { the sum of part of the original reference }
  86. function fixref(list: taasmoutput; var ref: treference): boolean;
  87. { returns whether a reference can be used immediately in a powerpc }
  88. { instruction }
  89. function issimpleref(const ref: treference): boolean;
  90. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  91. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  92. ref: treference);
  93. { creates the correct branch instruction for a given combination }
  94. { of asmcondflags and destination addressing mode }
  95. procedure a_jmp(list: taasmoutput; op: tasmop;
  96. c: tasmcondflag; crval: longint; l: tasmlabel);
  97. function save_regs(list : taasmoutput):longint;
  98. procedure restore_regs(list : taasmoutput);
  99. function get_darwin_call_stub(const s: string): tasmsymbol;
  100. end;
  101. tcg64fppc = class(tcg64f32)
  102. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  103. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : int64;reg : tregister64);override;
  104. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64;regsrc,regdst : tregister64);override;
  105. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  106. end;
  107. const
  108. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  109. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  110. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  111. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  112. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  113. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  114. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  115. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  116. implementation
  117. uses
  118. globals,verbose,systems,cutils,
  119. symconst,symdef,symsym,
  120. rgobj,tgobj,cpupi,procinfo,paramgr;
  121. procedure tcgppc.init_register_allocators;
  122. begin
  123. inherited init_register_allocators;
  124. if target_info.system=system_powerpc_darwin then
  125. begin
  126. if pi_needs_got in current_procinfo.flags then
  127. begin
  128. current_procinfo.got:=NR_R31;
  129. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  130. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  131. RS_R9,RS_R10,RS_R11,RS_R12,RS_R30,RS_R29,
  132. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  133. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  134. RS_R14,RS_R13],first_int_imreg,[]);
  135. end
  136. else
  137. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  138. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  139. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  140. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  141. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  142. RS_R14,RS_R13],first_int_imreg,[]);
  143. end
  144. else
  145. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  146. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  147. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  148. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  149. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  150. RS_R14,RS_R13],first_int_imreg,[]);
  151. case target_info.abi of
  152. abi_powerpc_aix:
  153. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  154. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  155. RS_F10,RS_F11,RS_F12,RS_F13,RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,
  156. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,
  157. RS_F17,RS_F16,RS_F15,RS_F14],first_fpu_imreg,[]);
  158. abi_powerpc_sysv:
  159. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  160. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  161. RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,RS_F26,RS_F25,RS_F24,RS_F23,
  162. RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,RS_F17,RS_F16,RS_F15,RS_F14,
  163. RS_F13,RS_F12,RS_F11,RS_F10],first_fpu_imreg,[]);
  164. else
  165. internalerror(2003122903);
  166. end;
  167. {$warning FIX ME}
  168. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  169. [RS_M0,RS_M1,RS_M2],first_mm_imreg,[]);
  170. end;
  171. procedure tcgppc.done_register_allocators;
  172. begin
  173. rg[R_INTREGISTER].free;
  174. rg[R_FPUREGISTER].free;
  175. rg[R_MMREGISTER].free;
  176. inherited done_register_allocators;
  177. end;
  178. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aint;const paraloc : tcgpara);
  179. var
  180. ref: treference;
  181. begin
  182. paraloc.check_simple_location;
  183. case paraloc.location^.loc of
  184. LOC_REGISTER,LOC_CREGISTER:
  185. a_load_const_reg(list,size,a,paraloc.location^.register);
  186. LOC_REFERENCE:
  187. begin
  188. reference_reset(ref);
  189. ref.base:=paraloc.location^.reference.index;
  190. ref.offset:=paraloc.location^.reference.offset;
  191. a_load_const_ref(list,size,a,ref);
  192. end;
  193. else
  194. internalerror(2002081101);
  195. end;
  196. end;
  197. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const paraloc : tcgpara);
  198. var
  199. ref: treference;
  200. tmpreg: tregister;
  201. begin
  202. paraloc.check_simple_location;
  203. case paraloc.location^.loc of
  204. LOC_REGISTER,LOC_CREGISTER:
  205. a_load_ref_reg(list,size,size,r,paraloc.location^.register);
  206. LOC_REFERENCE:
  207. begin
  208. reference_reset_base(ref,paraloc.location^.reference.index,paraloc.location^.reference.offset);
  209. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  210. a_load_ref_reg(list,size,size,r,tmpreg);
  211. a_load_reg_ref(list,size,size,tmpreg,ref);
  212. end;
  213. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  214. case size of
  215. OS_F32, OS_F64:
  216. a_loadfpu_ref_reg(list,size,r,paraloc.location^.register);
  217. else
  218. internalerror(2002072801);
  219. end;
  220. else
  221. internalerror(2002081103);
  222. end;
  223. end;
  224. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const paraloc : tcgpara);
  225. var
  226. ref: treference;
  227. tmpreg: tregister;
  228. begin
  229. paraloc.check_simple_location;
  230. case paraloc.location^.loc of
  231. LOC_REGISTER,LOC_CREGISTER:
  232. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  233. LOC_REFERENCE:
  234. begin
  235. reference_reset(ref);
  236. ref.base := paraloc.location^.reference.index;
  237. ref.offset := paraloc.location^.reference.offset;
  238. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  239. a_loadaddr_ref_reg(list,r,tmpreg);
  240. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  241. end;
  242. else
  243. internalerror(2002080701);
  244. end;
  245. end;
  246. function tcgppc.get_darwin_call_stub(const s: string): tasmsymbol;
  247. var
  248. stubname: string;
  249. href: treference;
  250. l1: tasmsymbol;
  251. begin
  252. { function declared in the current unit? }
  253. result := objectlibrary.getasmsymbol(s);
  254. if not(assigned(result)) then
  255. begin
  256. stubname := 'L'+s+'$stub';
  257. result := objectlibrary.getasmsymbol(stubname);
  258. end;
  259. if assigned(result) then
  260. exit;
  261. if not(assigned(importssection)) then
  262. importssection:=TAAsmoutput.create;
  263. importsSection.concat(Tai_section.Create(sec_data,'',0));
  264. importsSection.concat(Tai_direct.create(strpnew('.section __TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16')));
  265. importsSection.concat(Tai_align.Create(4));
  266. result := objectlibrary.newasmsymbol(stubname,AB_EXTERNAL,AT_FUNCTION);
  267. importsSection.concat(Tai_symbol.Create(result,0));
  268. importsSection.concat(Tai_direct.create(strpnew((#9+'.indirect_symbol ')+s)));
  269. l1 := objectlibrary.newasmsymbol('L'+s+'$lazy_ptr',AB_EXTERNAL,AT_FUNCTION);
  270. reference_reset_symbol(href,l1,0);
  271. {$ifdef powerpc}
  272. href.refaddr := addr_hi;
  273. importsSection.concat(taicpu.op_reg_ref(A_LIS,NR_R11,href));
  274. href.refaddr := addr_lo;
  275. href.base := NR_R11;
  276. importsSection.concat(taicpu.op_reg_ref(A_LWZU,NR_R12,href));
  277. importsSection.concat(taicpu.op_reg(A_MTCTR,NR_R12));
  278. importsSection.concat(taicpu.op_none(A_BCTR));
  279. {$else powerpc}
  280. internalerror(2004010502);
  281. {$endif powerpc}
  282. importsSection.concat(Tai_section.Create(sec_data,'',0));
  283. importsSection.concat(Tai_direct.create(strpnew('.lazy_symbol_pointer')));
  284. importsSection.concat(Tai_symbol.Create(l1,0));
  285. importsSection.concat(Tai_direct.create(strpnew((#9+'.indirect_symbol ')+s)));
  286. importsSection.concat(tai_const.createname(strpnew('dyld_stub_binding_helper'),AT_FUNCTION,0));
  287. end;
  288. { calling a procedure by name }
  289. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  290. var
  291. href : treference;
  292. begin
  293. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  294. if it is a cross-TOC call. If so, it also replaces the NOP
  295. with some restore code.}
  296. if (target_info.system <> system_powerpc_darwin) then
  297. begin
  298. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION)));
  299. if target_info.system=system_powerpc_macos then
  300. list.concat(taicpu.op_none(A_NOP));
  301. end
  302. else
  303. begin
  304. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s)));
  305. end;
  306. {
  307. the compiler does not properly set this flag anymore in pass 1, and
  308. for now we only need it after pass 2 (I hope) (JM)
  309. if not(pi_do_call in current_procinfo.flags) then
  310. internalerror(2003060703);
  311. }
  312. include(current_procinfo.flags,pi_do_call);
  313. end;
  314. { calling a procedure by address }
  315. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  316. var
  317. tmpreg : tregister;
  318. tmpref : treference;
  319. begin
  320. if target_info.system=system_powerpc_macos then
  321. begin
  322. {Generate instruction to load the procedure address from
  323. the transition vector.}
  324. //TODO: Support cross-TOC calls.
  325. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  326. reference_reset(tmpref);
  327. tmpref.offset := 0;
  328. //tmpref.symaddr := refs_full;
  329. tmpref.base:= reg;
  330. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  331. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  332. end
  333. else
  334. list.concat(taicpu.op_reg(A_MTCTR,reg));
  335. list.concat(taicpu.op_none(A_BCTRL));
  336. //if target_info.system=system_powerpc_macos then
  337. // //NOP is not needed here.
  338. // list.concat(taicpu.op_none(A_NOP));
  339. include(current_procinfo.flags,pi_do_call);
  340. {
  341. if not(pi_do_call in current_procinfo.flags) then
  342. internalerror(2003060704);
  343. }
  344. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  345. end;
  346. {********************** load instructions ********************}
  347. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aint; reg : TRegister);
  348. begin
  349. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  350. internalerror(2002090902);
  351. if (a >= low(smallint)) and
  352. (a <= high(smallint)) then
  353. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  354. else if ((a and $ffff) <> 0) then
  355. begin
  356. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  357. if ((a shr 16) <> 0) or
  358. (smallint(a and $ffff) < 0) then
  359. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  360. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  361. end
  362. else
  363. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  364. end;
  365. procedure tcgppc.a_load_reg_ref(list : taasmoutput; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  366. const
  367. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  368. { indexed? updating?}
  369. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  370. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  371. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  372. var
  373. op: TAsmOp;
  374. ref2: TReference;
  375. begin
  376. ref2 := ref;
  377. fixref(list,ref2);
  378. if tosize in [OS_S8..OS_S16] then
  379. { storing is the same for signed and unsigned values }
  380. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  381. { 64 bit stuff should be handled separately }
  382. if tosize in [OS_64,OS_S64] then
  383. internalerror(200109236);
  384. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  385. a_load_store(list,op,reg,ref2);
  386. End;
  387. procedure tcgppc.a_load_ref_reg(list : taasmoutput; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  388. const
  389. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  390. { indexed? updating?}
  391. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  392. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  393. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  394. { 64bit stuff should be handled separately }
  395. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  396. { 128bit stuff too }
  397. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  398. { there's no load-byte-with-sign-extend :( }
  399. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  400. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  401. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  402. var
  403. op: tasmop;
  404. tmpreg: tregister;
  405. ref2, tmpref: treference;
  406. begin
  407. { TODO: optimize/take into consideration fromsize/tosize. Will }
  408. { probably only matter for OS_S8 loads though }
  409. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  410. internalerror(2002090902);
  411. ref2 := ref;
  412. fixref(list,ref2);
  413. { the caller is expected to have adjusted the reference already }
  414. { in this case }
  415. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  416. fromsize := tosize;
  417. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  418. a_load_store(list,op,reg,ref2);
  419. { sign extend shortint if necessary, since there is no }
  420. { load instruction that does that automatically (JM) }
  421. if fromsize = OS_S8 then
  422. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  423. end;
  424. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  425. var
  426. instr: taicpu;
  427. begin
  428. case tosize of
  429. OS_8:
  430. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  431. reg2,reg1,0,31-8+1,31);
  432. OS_S8:
  433. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  434. OS_16:
  435. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  436. reg2,reg1,0,31-16+1,31);
  437. OS_S16:
  438. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  439. OS_32,OS_S32:
  440. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  441. else internalerror(2002090901);
  442. end;
  443. list.concat(instr);
  444. rg[R_INTREGISTER].add_move_instruction(instr);
  445. end;
  446. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  447. var
  448. instr: taicpu;
  449. begin
  450. instr := taicpu.op_reg_reg(A_FMR,reg2,reg1);
  451. list.concat(instr);
  452. rg[R_FPUREGISTER].add_move_instruction(instr);
  453. end;
  454. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  455. const
  456. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  457. { indexed? updating?}
  458. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  459. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  460. var
  461. op: tasmop;
  462. ref2: treference;
  463. begin
  464. { several functions call this procedure with OS_32 or OS_64 }
  465. { so this makes life easier (FK) }
  466. case size of
  467. OS_32,OS_F32:
  468. size:=OS_F32;
  469. OS_64,OS_F64,OS_C64:
  470. size:=OS_F64;
  471. else
  472. internalerror(200201121);
  473. end;
  474. ref2 := ref;
  475. fixref(list,ref2);
  476. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  477. a_load_store(list,op,reg,ref2);
  478. end;
  479. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  480. const
  481. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  482. { indexed? updating?}
  483. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  484. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  485. var
  486. op: tasmop;
  487. ref2: treference;
  488. begin
  489. if not(size in [OS_F32,OS_F64]) then
  490. internalerror(200201122);
  491. ref2 := ref;
  492. fixref(list,ref2);
  493. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  494. a_load_store(list,op,reg,ref2);
  495. end;
  496. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  497. begin
  498. a_op_const_reg_reg(list,op,size,a,reg,reg);
  499. end;
  500. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  501. begin
  502. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  503. end;
  504. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  505. size: tcgsize; a: aint; src, dst: tregister);
  506. var
  507. l1,l2: longint;
  508. oplo, ophi: tasmop;
  509. scratchreg: tregister;
  510. useReg, gotrlwi: boolean;
  511. procedure do_lo_hi;
  512. begin
  513. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  514. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  515. end;
  516. begin
  517. if op = OP_SUB then
  518. begin
  519. a_op_const_reg_reg(list,OP_ADD,size,-a,src,dst);
  520. exit;
  521. end;
  522. ophi := TOpCG2AsmOpConstHi[op];
  523. oplo := TOpCG2AsmOpConstLo[op];
  524. gotrlwi := get_rlwi_const(a,l1,l2);
  525. if (op in [OP_AND,OP_OR,OP_XOR]) then
  526. begin
  527. if (a = 0) then
  528. begin
  529. if op = OP_AND then
  530. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  531. else
  532. a_load_reg_reg(list,size,size,src,dst);
  533. exit;
  534. end
  535. else if (a = -1) then
  536. begin
  537. case op of
  538. OP_OR:
  539. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  540. OP_XOR:
  541. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  542. OP_AND:
  543. a_load_reg_reg(list,size,size,src,dst);
  544. end;
  545. exit;
  546. end
  547. else if (aword(a) <= high(word)) and
  548. ((op <> OP_AND) or
  549. not gotrlwi) then
  550. begin
  551. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  552. exit;
  553. end;
  554. { all basic constant instructions also have a shifted form that }
  555. { works only on the highest 16bits, so if lo(a) is 0, we can }
  556. { use that one }
  557. if (word(a) = 0) and
  558. (not(op = OP_AND) or
  559. not gotrlwi) then
  560. begin
  561. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  562. exit;
  563. end;
  564. end
  565. else if (op = OP_ADD) then
  566. if a = 0 then
  567. begin
  568. a_load_reg_reg(list,size,size,src,dst);
  569. exit
  570. end
  571. else if (a >= low(smallint)) and
  572. (a <= high(smallint)) then
  573. begin
  574. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  575. exit;
  576. end;
  577. { otherwise, the instructions we can generate depend on the }
  578. { operation }
  579. useReg := false;
  580. case op of
  581. OP_DIV,OP_IDIV:
  582. if (a = 0) then
  583. internalerror(200208103)
  584. else if (a = 1) then
  585. begin
  586. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  587. exit
  588. end
  589. else if ispowerof2(a,l1) then
  590. begin
  591. case op of
  592. OP_DIV:
  593. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  594. OP_IDIV:
  595. begin
  596. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  597. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  598. end;
  599. end;
  600. exit;
  601. end
  602. else
  603. usereg := true;
  604. OP_IMUL, OP_MUL:
  605. if (a = 0) then
  606. begin
  607. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  608. exit
  609. end
  610. else if (a = 1) then
  611. begin
  612. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  613. exit
  614. end
  615. else if ispowerof2(a,l1) then
  616. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  617. else if (longint(a) >= low(smallint)) and
  618. (longint(a) <= high(smallint)) then
  619. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  620. else
  621. usereg := true;
  622. OP_ADD:
  623. begin
  624. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  625. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  626. smallint((a shr 16) + ord(smallint(a) < 0))));
  627. end;
  628. OP_OR:
  629. { try to use rlwimi }
  630. if gotrlwi and
  631. (src = dst) then
  632. begin
  633. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  634. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  635. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  636. scratchreg,0,l1,l2));
  637. end
  638. else
  639. do_lo_hi;
  640. OP_AND:
  641. { try to use rlwinm }
  642. if gotrlwi then
  643. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  644. src,0,l1,l2))
  645. else
  646. useReg := true;
  647. OP_XOR:
  648. do_lo_hi;
  649. OP_SHL,OP_SHR,OP_SAR:
  650. begin
  651. if (a and 31) <> 0 Then
  652. list.concat(taicpu.op_reg_reg_const(
  653. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  654. else
  655. a_load_reg_reg(list,size,size,src,dst);
  656. if (a shr 5) <> 0 then
  657. internalError(68991);
  658. end
  659. else
  660. internalerror(200109091);
  661. end;
  662. { if all else failed, load the constant in a register and then }
  663. { perform the operation }
  664. if useReg then
  665. begin
  666. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  667. a_load_const_reg(list,OS_32,a,scratchreg);
  668. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  669. end;
  670. end;
  671. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  672. size: tcgsize; src1, src2, dst: tregister);
  673. const
  674. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  675. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  676. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  677. begin
  678. case op of
  679. OP_NEG,OP_NOT:
  680. begin
  681. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  682. if (op = OP_NOT) and
  683. not(size in [OS_32,OS_S32]) then
  684. { zero/sign extend result again }
  685. a_load_reg_reg(list,OS_32,size,dst,dst);
  686. end;
  687. else
  688. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  689. end;
  690. end;
  691. {*************** compare instructructions ****************}
  692. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  693. l : tasmlabel);
  694. var
  695. p: taicpu;
  696. scratch_register: TRegister;
  697. signed: boolean;
  698. begin
  699. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  700. { in the following case, we generate more efficient code when }
  701. { signed is true }
  702. if (cmp_op in [OC_EQ,OC_NE]) and
  703. (aword(a) > $ffff) then
  704. signed := true;
  705. if signed then
  706. if (a >= low(smallint)) and (a <= high(smallint)) Then
  707. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  708. else
  709. begin
  710. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  711. a_load_const_reg(list,OS_32,a,scratch_register);
  712. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  713. end
  714. else
  715. if (aword(a) <= $ffff) then
  716. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  717. else
  718. begin
  719. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  720. a_load_const_reg(list,OS_32,a,scratch_register);
  721. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  722. end;
  723. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  724. end;
  725. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  726. reg1,reg2 : tregister;l : tasmlabel);
  727. var
  728. p: taicpu;
  729. op: tasmop;
  730. begin
  731. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  732. op := A_CMPW
  733. else
  734. op := A_CMPLW;
  735. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  736. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  737. end;
  738. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  739. begin
  740. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  741. end;
  742. procedure tcgppc.a_jmp_name(list : taasmoutput;const s : string);
  743. var
  744. p : taicpu;
  745. begin
  746. if (target_info.system = system_powerpc_darwin) then
  747. p := taicpu.op_sym(A_B,get_darwin_call_stub(s))
  748. else
  749. p := taicpu.op_sym(A_B,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION));
  750. p.is_jmp := true;
  751. list.concat(p)
  752. end;
  753. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  754. begin
  755. a_jmp(list,A_B,C_None,0,l);
  756. end;
  757. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  758. var
  759. c: tasmcond;
  760. begin
  761. c := flags_to_cond(f);
  762. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  763. end;
  764. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  765. var
  766. testbit: byte;
  767. bitvalue: boolean;
  768. begin
  769. { get the bit to extract from the conditional register + its }
  770. { requested value (0 or 1) }
  771. testbit := ((f.cr-RS_CR0) * 4);
  772. case f.flag of
  773. F_EQ,F_NE:
  774. begin
  775. inc(testbit,2);
  776. bitvalue := f.flag = F_EQ;
  777. end;
  778. F_LT,F_GE:
  779. begin
  780. bitvalue := f.flag = F_LT;
  781. end;
  782. F_GT,F_LE:
  783. begin
  784. inc(testbit);
  785. bitvalue := f.flag = F_GT;
  786. end;
  787. else
  788. internalerror(200112261);
  789. end;
  790. { load the conditional register in the destination reg }
  791. list.concat(taicpu.op_reg(A_MFCR,reg));
  792. { we will move the bit that has to be tested to bit 0 by rotating }
  793. { left }
  794. testbit := (testbit + 1) and 31;
  795. { extract bit }
  796. list.concat(taicpu.op_reg_reg_const_const_const(
  797. A_RLWINM,reg,reg,testbit,31,31));
  798. { if we need the inverse, xor with 1 }
  799. if not bitvalue then
  800. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  801. end;
  802. (*
  803. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  804. var
  805. testbit: byte;
  806. bitvalue: boolean;
  807. begin
  808. { get the bit to extract from the conditional register + its }
  809. { requested value (0 or 1) }
  810. case f.simple of
  811. false:
  812. begin
  813. { we don't generate this in the compiler }
  814. internalerror(200109062);
  815. end;
  816. true:
  817. case f.cond of
  818. C_None:
  819. internalerror(200109063);
  820. C_LT..C_NU:
  821. begin
  822. testbit := (ord(f.cr) - ord(R_CR0))*4;
  823. inc(testbit,AsmCondFlag2BI[f.cond]);
  824. bitvalue := AsmCondFlagTF[f.cond];
  825. end;
  826. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  827. begin
  828. testbit := f.crbit
  829. bitvalue := AsmCondFlagTF[f.cond];
  830. end;
  831. else
  832. internalerror(200109064);
  833. end;
  834. end;
  835. { load the conditional register in the destination reg }
  836. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  837. { we will move the bit that has to be tested to bit 31 -> rotate }
  838. { left by bitpos+1 (remember, this is big-endian!) }
  839. if bitpos <> 31 then
  840. inc(bitpos)
  841. else
  842. bitpos := 0;
  843. { extract bit }
  844. list.concat(taicpu.op_reg_reg_const_const_const(
  845. A_RLWINM,reg,reg,bitpos,31,31));
  846. { if we need the inverse, xor with 1 }
  847. if not bitvalue then
  848. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  849. end;
  850. *)
  851. { *********** entry/exit code and address loading ************ }
  852. procedure tcgppc.g_save_standard_registers(list:Taasmoutput);
  853. begin
  854. { this work is done in g_proc_entry }
  855. end;
  856. procedure tcgppc.g_restore_standard_registers(list:Taasmoutput);
  857. begin
  858. { this work is done in g_proc_exit }
  859. end;
  860. procedure tcgppc.g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);
  861. { generated the entry code of a procedure/function. Note: localsize is the }
  862. { sum of the size necessary for local variables and the maximum possible }
  863. { combined size of ALL the parameters of a procedure called by the current }
  864. { one. }
  865. { This procedure may be called before, as well as after g_return_from_proc }
  866. { is called. NOTE registers are not to be allocated through the register }
  867. { allocator here, because the register colouring has already occured !! }
  868. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  869. href,href2 : treference;
  870. usesfpr,usesgpr,gotgot : boolean;
  871. parastart : aint;
  872. l : tasmlabel;
  873. regcounter2, firstfpureg: Tsuperregister;
  874. i : integer;
  875. hp: tparavarsym;
  876. cond : tasmcond;
  877. instr : taicpu;
  878. size: tcgsize;
  879. begin
  880. { CR and LR only have to be saved in case they are modified by the current }
  881. { procedure, but currently this isn't checked, so save them always }
  882. { following is the entry code as described in "Altivec Programming }
  883. { Interface Manual", bar the saving of AltiVec registers }
  884. a_reg_alloc(list,NR_STACK_POINTER_REG);
  885. a_reg_alloc(list,NR_R0);
  886. usesfpr:=false;
  887. if not (po_assembler in current_procinfo.procdef.procoptions) then
  888. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  889. case target_info.abi of
  890. abi_powerpc_aix:
  891. firstfpureg := RS_F14;
  892. abi_powerpc_sysv:
  893. firstfpureg := RS_F9;
  894. else
  895. internalerror(2003122903);
  896. end;
  897. for regcounter:=firstfpureg to RS_F31 do
  898. begin
  899. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  900. begin
  901. usesfpr:= true;
  902. firstregfpu:=regcounter;
  903. break;
  904. end;
  905. end;
  906. usesgpr:=false;
  907. if not (po_assembler in current_procinfo.procdef.procoptions) then
  908. for regcounter2:=RS_R13 to RS_R31 do
  909. begin
  910. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  911. begin
  912. usesgpr:=true;
  913. firstreggpr:=regcounter2;
  914. break;
  915. end;
  916. end;
  917. { save link register? }
  918. if not (po_assembler in current_procinfo.procdef.procoptions) then
  919. if (pi_do_call in current_procinfo.flags) then
  920. begin
  921. { save return address... }
  922. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  923. { ... in caller's frame }
  924. case target_info.abi of
  925. abi_powerpc_aix:
  926. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  927. abi_powerpc_sysv:
  928. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  929. end;
  930. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  931. a_reg_dealloc(list,NR_R0);
  932. end;
  933. { save the CR if necessary in callers frame. }
  934. if not (po_assembler in current_procinfo.procdef.procoptions) then
  935. if target_info.abi = abi_powerpc_aix then
  936. if false then { Not needed at the moment. }
  937. begin
  938. a_reg_alloc(list,NR_R0);
  939. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  940. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  941. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  942. a_reg_dealloc(list,NR_R0);
  943. end;
  944. { !!! always allocate space for all registers for now !!! }
  945. if not (po_assembler in current_procinfo.procdef.procoptions) then
  946. { if usesfpr or usesgpr then }
  947. begin
  948. a_reg_alloc(list,NR_R12);
  949. { save end of fpr save area }
  950. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  951. end;
  952. if (not nostackframe) and
  953. (localsize <> 0) then
  954. begin
  955. if (localsize <= high(smallint)) then
  956. begin
  957. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  958. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  959. end
  960. else
  961. begin
  962. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  963. { can't use getregisterint here, the register colouring }
  964. { is already done when we get here }
  965. href.index := NR_R11;
  966. a_reg_alloc(list,href.index);
  967. a_load_const_reg(list,OS_S32,-localsize,href.index);
  968. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  969. a_reg_dealloc(list,href.index);
  970. end;
  971. end;
  972. { no GOT pointer loaded yet }
  973. gotgot:=false;
  974. if usesfpr then
  975. begin
  976. { save floating-point registers
  977. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  978. begin
  979. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  980. gotgot:=true;
  981. end
  982. else
  983. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14),AB_EXTERNAL,AT_FUNCTION));
  984. }
  985. reference_reset_base(href,NR_R12,-8);
  986. for regcounter:=firstregfpu to RS_F31 do
  987. begin
  988. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  989. begin
  990. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  991. dec(href.offset,8);
  992. end;
  993. end;
  994. { compute end of gpr save area }
  995. a_op_const_reg(list,OP_ADD,OS_ADDR,href.offset+8,NR_R12);
  996. end;
  997. { save gprs and fetch GOT pointer }
  998. if usesgpr then
  999. begin
  1000. {
  1001. if cs_create_pic in aktmoduleswitches then
  1002. begin
  1003. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  1004. gotgot:=true;
  1005. end
  1006. else
  1007. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14),AB_EXTERNAL,AT_FUNCTION))
  1008. }
  1009. reference_reset_base(href,NR_R12,-4);
  1010. for regcounter2:=RS_R13 to RS_R31 do
  1011. begin
  1012. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1013. begin
  1014. usesgpr:=true;
  1015. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter2,R_SUBNONE),href);
  1016. dec(href.offset,4);
  1017. end;
  1018. end;
  1019. {
  1020. r.enum:=R_INTREGISTER;
  1021. r.:=;
  1022. reference_reset_base(href,NR_R12,-((NR_R31-firstreggpr) shr 8+1)*4);
  1023. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1024. }
  1025. end;
  1026. { see "!!! always allocate space for all registers for now !!!" above }
  1027. { done in ncgutil because it may only be released after the parameters }
  1028. { have been moved to their final resting place }
  1029. { if usesfpr or usesgpr then }
  1030. { a_reg_dealloc(list,NR_R12); }
  1031. { if we didn't get the GOT pointer till now, we've to calculate it now }
  1032. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  1033. case target_info.system of
  1034. system_powerpc_darwin:
  1035. begin
  1036. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1037. fillchar(cond,sizeof(cond),0);
  1038. cond.simple:=false;
  1039. cond.bo:=20;
  1040. cond.bi:=31;
  1041. instr:=taicpu.op_sym(A_BCL,current_procinfo.gotlabel);
  1042. instr.setcondition(cond);
  1043. list.concat(instr);
  1044. a_label(list,current_procinfo.gotlabel);
  1045. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  1046. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  1047. end;
  1048. else
  1049. begin
  1050. a_reg_alloc(list,NR_R31);
  1051. { place GOT ptr in r31 }
  1052. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  1053. end;
  1054. end;
  1055. { save the CR if necessary ( !!! always done currently ) }
  1056. { still need to find out where this has to be done for SystemV
  1057. a_reg_alloc(list,R_0);
  1058. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  1059. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  1060. new_reference(STACK_POINTER_REG,LA_CR)));
  1061. a_reg_dealloc(list,R_0); }
  1062. { now comes the AltiVec context save, not yet implemented !!! }
  1063. end;
  1064. procedure tcgppc.g_proc_exit(list : taasmoutput;parasize : longint;nostackframe:boolean);
  1065. { This procedure may be called before, as well as after g_stackframe_entry }
  1066. { is called. NOTE registers are not to be allocated through the register }
  1067. { allocator here, because the register colouring has already occured !! }
  1068. var
  1069. regcounter,firstregfpu,firstreggpr: TsuperRegister;
  1070. href : treference;
  1071. usesfpr,usesgpr,genret : boolean;
  1072. regcounter2, firstfpureg:Tsuperregister;
  1073. localsize: aint;
  1074. begin
  1075. { AltiVec context restore, not yet implemented !!! }
  1076. usesfpr:=false;
  1077. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1078. begin
  1079. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1080. case target_info.abi of
  1081. abi_powerpc_aix:
  1082. firstfpureg := RS_F14;
  1083. abi_powerpc_sysv:
  1084. firstfpureg := RS_F9;
  1085. else
  1086. internalerror(2003122903);
  1087. end;
  1088. for regcounter:=firstfpureg to RS_F31 do
  1089. begin
  1090. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1091. begin
  1092. usesfpr:=true;
  1093. firstregfpu:=regcounter;
  1094. break;
  1095. end;
  1096. end;
  1097. end;
  1098. usesgpr:=false;
  1099. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1100. for regcounter2:=RS_R13 to RS_R31 do
  1101. begin
  1102. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1103. begin
  1104. usesgpr:=true;
  1105. firstreggpr:=regcounter2;
  1106. break;
  1107. end;
  1108. end;
  1109. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1110. { no return (blr) generated yet }
  1111. genret:=true;
  1112. if usesgpr or usesfpr then
  1113. begin
  1114. { address of gpr save area to r11 }
  1115. { (register allocator is no longer valid at this time and an add of 0 }
  1116. { is translated into a move, which is then registered with the register }
  1117. { allocator, causing a crash }
  1118. if (localsize <> 0) then
  1119. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,localsize,NR_STACK_POINTER_REG,NR_R12)
  1120. else
  1121. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  1122. if usesfpr then
  1123. begin
  1124. reference_reset_base(href,NR_R12,-8);
  1125. for regcounter := firstregfpu to RS_F31 do
  1126. begin
  1127. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1128. begin
  1129. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1130. dec(href.offset,8);
  1131. end;
  1132. end;
  1133. inc(href.offset,4);
  1134. end
  1135. else
  1136. reference_reset_base(href,NR_R12,-4);
  1137. for regcounter2:=RS_R13 to RS_R31 do
  1138. begin
  1139. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1140. begin
  1141. usesgpr:=true;
  1142. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter2,R_SUBNONE));
  1143. dec(href.offset,4);
  1144. end;
  1145. end;
  1146. (*
  1147. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr)) shr 8+1)*4);
  1148. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1149. *)
  1150. end;
  1151. (*
  1152. { restore fprs and return }
  1153. if usesfpr then
  1154. begin
  1155. { address of fpr save area to r11 }
  1156. r:=NR_R12;
  1157. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1158. {
  1159. if (pi_do_call in current_procinfo.flags) then
  1160. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1161. '_x',AB_EXTERNAL,AT_FUNCTION))
  1162. else
  1163. { leaf node => lr haven't to be restored }
  1164. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1165. '_l');
  1166. genret:=false;
  1167. }
  1168. end;
  1169. *)
  1170. { if we didn't generate the return code, we've to do it now }
  1171. if genret then
  1172. begin
  1173. { adjust r1 }
  1174. { (register allocator is no longer valid at this time and an add of 0 }
  1175. { is translated into a move, which is then registered with the register }
  1176. { allocator, causing a crash }
  1177. if (not nostackframe) and
  1178. (localsize <> 0) then
  1179. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1180. { load link register? }
  1181. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1182. begin
  1183. if (pi_do_call in current_procinfo.flags) then
  1184. begin
  1185. case target_info.abi of
  1186. abi_powerpc_aix:
  1187. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1188. abi_powerpc_sysv:
  1189. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1190. end;
  1191. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1192. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1193. end;
  1194. { restore the CR if necessary from callers frame}
  1195. if target_info.abi = abi_powerpc_aix then
  1196. if false then { Not needed at the moment. }
  1197. begin
  1198. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1199. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1200. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1201. a_reg_dealloc(list,NR_R0);
  1202. end;
  1203. end;
  1204. list.concat(taicpu.op_none(A_BLR));
  1205. end;
  1206. end;
  1207. function tcgppc.save_regs(list : taasmoutput):longint;
  1208. {Generates code which saves used non-volatile registers in
  1209. the save area right below the address the stackpointer point to.
  1210. Returns the actual used save area size.}
  1211. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1212. usesfpr,usesgpr: boolean;
  1213. href : treference;
  1214. offset: aint;
  1215. regcounter2, firstfpureg: Tsuperregister;
  1216. begin
  1217. usesfpr:=false;
  1218. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1219. begin
  1220. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1221. case target_info.abi of
  1222. abi_powerpc_aix:
  1223. firstfpureg := RS_F14;
  1224. abi_powerpc_sysv:
  1225. firstfpureg := RS_F9;
  1226. else
  1227. internalerror(2003122903);
  1228. end;
  1229. for regcounter:=firstfpureg to RS_F31 do
  1230. begin
  1231. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1232. begin
  1233. usesfpr:=true;
  1234. firstregfpu:=regcounter;
  1235. break;
  1236. end;
  1237. end;
  1238. end;
  1239. usesgpr:=false;
  1240. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1241. for regcounter2:=RS_R13 to RS_R31 do
  1242. begin
  1243. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1244. begin
  1245. usesgpr:=true;
  1246. firstreggpr:=regcounter2;
  1247. break;
  1248. end;
  1249. end;
  1250. offset:= 0;
  1251. { save floating-point registers }
  1252. if usesfpr then
  1253. for regcounter := firstregfpu to RS_F31 do
  1254. begin
  1255. offset:= offset - 8;
  1256. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1257. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1258. end;
  1259. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1260. { save gprs in gpr save area }
  1261. if usesgpr then
  1262. if firstreggpr < RS_R30 then
  1263. begin
  1264. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1265. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1266. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1267. {STMW stores multiple registers}
  1268. end
  1269. else
  1270. begin
  1271. for regcounter := firstreggpr to RS_R31 do
  1272. begin
  1273. offset:= offset - 4;
  1274. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1275. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1276. end;
  1277. end;
  1278. { now comes the AltiVec context save, not yet implemented !!! }
  1279. save_regs:= -offset;
  1280. end;
  1281. procedure tcgppc.restore_regs(list : taasmoutput);
  1282. {Generates code which restores used non-volatile registers from
  1283. the save area right below the address the stackpointer point to.}
  1284. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1285. usesfpr,usesgpr: boolean;
  1286. href : treference;
  1287. offset: integer;
  1288. regcounter2, firstfpureg: Tsuperregister;
  1289. begin
  1290. usesfpr:=false;
  1291. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1292. begin
  1293. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1294. case target_info.abi of
  1295. abi_powerpc_aix:
  1296. firstfpureg := RS_F14;
  1297. abi_powerpc_sysv:
  1298. firstfpureg := RS_F9;
  1299. else
  1300. internalerror(2003122903);
  1301. end;
  1302. for regcounter:=firstfpureg to RS_F31 do
  1303. begin
  1304. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1305. begin
  1306. usesfpr:=true;
  1307. firstregfpu:=regcounter;
  1308. break;
  1309. end;
  1310. end;
  1311. end;
  1312. usesgpr:=false;
  1313. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1314. for regcounter2:=RS_R13 to RS_R31 do
  1315. begin
  1316. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1317. begin
  1318. usesgpr:=true;
  1319. firstreggpr:=regcounter2;
  1320. break;
  1321. end;
  1322. end;
  1323. offset:= 0;
  1324. { restore fp registers }
  1325. if usesfpr then
  1326. for regcounter := firstregfpu to RS_F31 do
  1327. begin
  1328. offset:= offset - 8;
  1329. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1330. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1331. end;
  1332. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1333. { restore gprs }
  1334. if usesgpr then
  1335. if firstreggpr < RS_R30 then
  1336. begin
  1337. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1338. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1339. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1340. {LMW loads multiple registers}
  1341. end
  1342. else
  1343. begin
  1344. for regcounter := firstreggpr to RS_R31 do
  1345. begin
  1346. offset:= offset - 4;
  1347. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1348. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1349. end;
  1350. end;
  1351. { now comes the AltiVec context restore, not yet implemented !!! }
  1352. end;
  1353. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1354. (* NOT IN USE *)
  1355. { generated the entry code of a procedure/function. Note: localsize is the }
  1356. { sum of the size necessary for local variables and the maximum possible }
  1357. { combined size of ALL the parameters of a procedure called by the current }
  1358. { one }
  1359. const
  1360. macosLinkageAreaSize = 24;
  1361. var regcounter: TRegister;
  1362. href : treference;
  1363. registerSaveAreaSize : longint;
  1364. begin
  1365. if (localsize mod 8) <> 0 then
  1366. internalerror(58991);
  1367. { CR and LR only have to be saved in case they are modified by the current }
  1368. { procedure, but currently this isn't checked, so save them always }
  1369. { following is the entry code as described in "Altivec Programming }
  1370. { Interface Manual", bar the saving of AltiVec registers }
  1371. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1372. a_reg_alloc(list,NR_R0);
  1373. { save return address in callers frame}
  1374. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1375. { ... in caller's frame }
  1376. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1377. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1378. a_reg_dealloc(list,NR_R0);
  1379. { save non-volatile registers in callers frame}
  1380. registerSaveAreaSize:= save_regs(list);
  1381. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1382. a_reg_alloc(list,NR_R0);
  1383. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1384. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1385. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1386. a_reg_dealloc(list,NR_R0);
  1387. (*
  1388. { save pointer to incoming arguments }
  1389. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1390. *)
  1391. (*
  1392. a_reg_alloc(list,R_12);
  1393. { 0 or 8 based on SP alignment }
  1394. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1395. R_12,STACK_POINTER_REG,0,28,28));
  1396. { add in stack length }
  1397. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1398. -localsize));
  1399. { establish new alignment }
  1400. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1401. a_reg_dealloc(list,R_12);
  1402. *)
  1403. { allocate stack frame }
  1404. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1405. inc(localsize,tg.lasttemp);
  1406. localsize:=align(localsize,16);
  1407. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1408. if (localsize <> 0) then
  1409. begin
  1410. if (localsize <= high(smallint)) then
  1411. begin
  1412. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1413. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1414. end
  1415. else
  1416. begin
  1417. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1418. href.index := NR_R11;
  1419. a_reg_alloc(list,href.index);
  1420. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1421. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1422. a_reg_dealloc(list,href.index);
  1423. end;
  1424. end;
  1425. end;
  1426. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aint);
  1427. (* NOT IN USE *)
  1428. var
  1429. href : treference;
  1430. begin
  1431. a_reg_alloc(list,NR_R0);
  1432. { restore stack pointer }
  1433. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1434. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1435. (*
  1436. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1437. *)
  1438. { restore the CR if necessary from callers frame
  1439. ( !!! always done currently ) }
  1440. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1441. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1442. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1443. a_reg_dealloc(list,NR_R0);
  1444. (*
  1445. { restore return address from callers frame }
  1446. reference_reset_base(href,STACK_POINTER_REG,8);
  1447. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1448. *)
  1449. { restore non-volatile registers from callers frame }
  1450. restore_regs(list);
  1451. (*
  1452. { return to caller }
  1453. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1454. list.concat(taicpu.op_none(A_BLR));
  1455. *)
  1456. { restore return address from callers frame }
  1457. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1458. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1459. { return to caller }
  1460. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1461. list.concat(taicpu.op_none(A_BLR));
  1462. end;
  1463. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1464. var
  1465. ref2, tmpref: treference;
  1466. tmpreg:Tregister;
  1467. begin
  1468. ref2 := ref;
  1469. fixref(list,ref2);
  1470. if assigned(ref2.symbol) then
  1471. begin
  1472. if target_info.system = system_powerpc_macos then
  1473. begin
  1474. if macos_direct_globals then
  1475. begin
  1476. reference_reset(tmpref);
  1477. tmpref.offset := ref2.offset;
  1478. tmpref.symbol := ref2.symbol;
  1479. tmpref.base := NR_NO;
  1480. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1481. end
  1482. else
  1483. begin
  1484. reference_reset(tmpref);
  1485. tmpref.symbol := ref2.symbol;
  1486. tmpref.offset := 0;
  1487. tmpref.base := NR_RTOC;
  1488. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1489. if ref2.offset <> 0 then
  1490. begin
  1491. reference_reset(tmpref);
  1492. tmpref.offset := ref2.offset;
  1493. tmpref.base:= r;
  1494. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1495. end;
  1496. end;
  1497. if ref2.base <> NR_NO then
  1498. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1499. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1500. end
  1501. else
  1502. begin
  1503. { add the symbol's value to the base of the reference, and if the }
  1504. { reference doesn't have a base, create one }
  1505. reference_reset(tmpref);
  1506. tmpref.offset := ref2.offset;
  1507. tmpref.symbol := ref2.symbol;
  1508. tmpref.relsymbol := ref2.relsymbol;
  1509. tmpref.refaddr := addr_hi;
  1510. if ref2.base<> NR_NO then
  1511. begin
  1512. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1513. ref2.base,tmpref));
  1514. end
  1515. else
  1516. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1517. tmpref.base := NR_NO;
  1518. tmpref.refaddr := addr_lo;
  1519. { can be folded with one of the next instructions by the }
  1520. { optimizer probably }
  1521. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1522. end
  1523. end
  1524. else if ref2.offset <> 0 Then
  1525. if ref2.base <> NR_NO then
  1526. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1527. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1528. { occurs, so now only ref.offset has to be loaded }
  1529. else
  1530. a_load_const_reg(list,OS_32,ref2.offset,r)
  1531. else if ref.index <> NR_NO Then
  1532. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1533. else if (ref2.base <> NR_NO) and
  1534. (r <> ref2.base) then
  1535. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  1536. else
  1537. list.concat(taicpu.op_reg_const(A_LI,r,0));
  1538. end;
  1539. { ************* concatcopy ************ }
  1540. {$ifndef ppc603}
  1541. const
  1542. maxmoveunit = 8;
  1543. {$else ppc603}
  1544. const
  1545. maxmoveunit = 4;
  1546. {$endif ppc603}
  1547. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);
  1548. var
  1549. countreg: TRegister;
  1550. src, dst: TReference;
  1551. lab: tasmlabel;
  1552. count, count2: aint;
  1553. orgsrc, orgdst: boolean;
  1554. size: tcgsize;
  1555. begin
  1556. {$ifdef extdebug}
  1557. if len > high(longint) then
  1558. internalerror(2002072704);
  1559. {$endif extdebug}
  1560. { make sure short loads are handled as optimally as possible }
  1561. if (len <= maxmoveunit) and
  1562. (byte(len) in [1,2,4,8]) then
  1563. begin
  1564. if len < 8 then
  1565. begin
  1566. size := int_cgsize(len);
  1567. a_load_ref_ref(list,size,size,source,dest);
  1568. end
  1569. else
  1570. begin
  1571. a_reg_alloc(list,NR_F0);
  1572. a_loadfpu_ref_reg(list,OS_F64,source,NR_F0);
  1573. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dest);
  1574. a_reg_dealloc(list,NR_F0);
  1575. end;
  1576. exit;
  1577. end;
  1578. count := len div maxmoveunit;
  1579. reference_reset(src);
  1580. reference_reset(dst);
  1581. { load the address of source into src.base }
  1582. if (count > 4) or
  1583. not issimpleref(source) or
  1584. ((source.index <> NR_NO) and
  1585. ((source.offset + longint(len)) > high(smallint))) then
  1586. begin
  1587. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1588. a_loadaddr_ref_reg(list,source,src.base);
  1589. orgsrc := false;
  1590. end
  1591. else
  1592. begin
  1593. src := source;
  1594. orgsrc := true;
  1595. end;
  1596. { load the address of dest into dst.base }
  1597. if (count > 4) or
  1598. not issimpleref(dest) or
  1599. ((dest.index <> NR_NO) and
  1600. ((dest.offset + longint(len)) > high(smallint))) then
  1601. begin
  1602. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1603. a_loadaddr_ref_reg(list,dest,dst.base);
  1604. orgdst := false;
  1605. end
  1606. else
  1607. begin
  1608. dst := dest;
  1609. orgdst := true;
  1610. end;
  1611. {$ifndef ppc603}
  1612. if count > 4 then
  1613. { generate a loop }
  1614. begin
  1615. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1616. { have to be set to 8. I put an Inc there so debugging may be }
  1617. { easier (should offset be different from zero here, it will be }
  1618. { easy to notice in the generated assembler }
  1619. inc(dst.offset,8);
  1620. inc(src.offset,8);
  1621. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1622. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1623. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1624. a_load_const_reg(list,OS_32,count,countreg);
  1625. { explicitely allocate R_0 since it can be used safely here }
  1626. { (for holding date that's being copied) }
  1627. a_reg_alloc(list,NR_F0);
  1628. objectlibrary.getlabel(lab);
  1629. a_label(list, lab);
  1630. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1631. list.concat(taicpu.op_reg_ref(A_LFDU,NR_F0,src));
  1632. list.concat(taicpu.op_reg_ref(A_STFDU,NR_F0,dst));
  1633. a_jmp(list,A_BC,C_NE,0,lab);
  1634. a_reg_dealloc(list,NR_F0);
  1635. len := len mod 8;
  1636. end;
  1637. count := len div 8;
  1638. if count > 0 then
  1639. { unrolled loop }
  1640. begin
  1641. a_reg_alloc(list,NR_F0);
  1642. for count2 := 1 to count do
  1643. begin
  1644. a_loadfpu_ref_reg(list,OS_F64,src,NR_F0);
  1645. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dst);
  1646. inc(src.offset,8);
  1647. inc(dst.offset,8);
  1648. end;
  1649. a_reg_dealloc(list,NR_F0);
  1650. len := len mod 8;
  1651. end;
  1652. if (len and 4) <> 0 then
  1653. begin
  1654. a_reg_alloc(list,NR_R0);
  1655. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1656. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1657. inc(src.offset,4);
  1658. inc(dst.offset,4);
  1659. a_reg_dealloc(list,NR_R0);
  1660. end;
  1661. {$else not ppc603}
  1662. if count > 4 then
  1663. { generate a loop }
  1664. begin
  1665. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1666. { have to be set to 4. I put an Inc there so debugging may be }
  1667. { easier (should offset be different from zero here, it will be }
  1668. { easy to notice in the generated assembler }
  1669. inc(dst.offset,4);
  1670. inc(src.offset,4);
  1671. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1672. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1673. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1674. a_load_const_reg(list,OS_32,count,countreg);
  1675. { explicitely allocate R_0 since it can be used safely here }
  1676. { (for holding date that's being copied) }
  1677. a_reg_alloc(list,NR_R0);
  1678. objectlibrary.getlabel(lab);
  1679. a_label(list, lab);
  1680. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1681. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1682. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1683. a_jmp(list,A_BC,C_NE,0,lab);
  1684. a_reg_dealloc(list,NR_R0);
  1685. len := len mod 4;
  1686. end;
  1687. count := len div 4;
  1688. if count > 0 then
  1689. { unrolled loop }
  1690. begin
  1691. a_reg_alloc(list,NR_R0);
  1692. for count2 := 1 to count do
  1693. begin
  1694. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1695. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1696. inc(src.offset,4);
  1697. inc(dst.offset,4);
  1698. end;
  1699. a_reg_dealloc(list,NR_R0);
  1700. len := len mod 4;
  1701. end;
  1702. {$endif not ppc603}
  1703. { copy the leftovers }
  1704. if (len and 2) <> 0 then
  1705. begin
  1706. a_reg_alloc(list,NR_R0);
  1707. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1708. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1709. inc(src.offset,2);
  1710. inc(dst.offset,2);
  1711. a_reg_dealloc(list,NR_R0);
  1712. end;
  1713. if (len and 1) <> 0 then
  1714. begin
  1715. a_reg_alloc(list,NR_R0);
  1716. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1717. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1718. a_reg_dealloc(list,NR_R0);
  1719. end;
  1720. end;
  1721. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1722. var
  1723. hl : tasmlabel;
  1724. begin
  1725. if not(cs_check_overflow in aktlocalswitches) then
  1726. exit;
  1727. objectlibrary.getlabel(hl);
  1728. if not ((def.deftype=pointerdef) or
  1729. ((def.deftype=orddef) and
  1730. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1731. bool8bit,bool16bit,bool32bit]))) then
  1732. begin
  1733. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1734. a_jmp(list,A_BC,C_NO,7,hl)
  1735. end
  1736. else
  1737. a_jmp_cond(list,OC_AE,hl);
  1738. a_call_name(list,'FPC_OVERFLOW');
  1739. a_label(list,hl);
  1740. end;
  1741. {***************** This is private property, keep out! :) *****************}
  1742. function tcgppc.issimpleref(const ref: treference): boolean;
  1743. begin
  1744. if (ref.base = NR_NO) and
  1745. (ref.index <> NR_NO) then
  1746. internalerror(200208101);
  1747. result :=
  1748. not(assigned(ref.symbol)) and
  1749. (((ref.index = NR_NO) and
  1750. (ref.offset >= low(smallint)) and
  1751. (ref.offset <= high(smallint))) or
  1752. ((ref.index <> NR_NO) and
  1753. (ref.offset = 0)));
  1754. end;
  1755. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1756. var
  1757. tmpreg: tregister;
  1758. orgindex: tregister;
  1759. begin
  1760. result := false;
  1761. if (ref.base = NR_NO) then
  1762. begin
  1763. ref.base := ref.index;
  1764. ref.base := NR_NO;
  1765. end;
  1766. if (ref.base <> NR_NO) then
  1767. begin
  1768. if (ref.index <> NR_NO) and
  1769. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1770. begin
  1771. result := true;
  1772. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1773. list.concat(taicpu.op_reg_reg_reg(
  1774. A_ADD,tmpreg,ref.base,ref.index));
  1775. ref.index := NR_NO;
  1776. ref.base := tmpreg;
  1777. end
  1778. end
  1779. else
  1780. if ref.index <> NR_NO then
  1781. internalerror(200208102);
  1782. end;
  1783. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1784. { that's the case, we can use rlwinm to do an AND operation }
  1785. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1786. var
  1787. temp : longint;
  1788. testbit : aint;
  1789. compare: boolean;
  1790. begin
  1791. get_rlwi_const := false;
  1792. if (a = 0) or (a = -1) then
  1793. exit;
  1794. { start with the lowest bit }
  1795. testbit := 1;
  1796. { check its value }
  1797. compare := boolean(a and testbit);
  1798. { find out how long the run of bits with this value is }
  1799. { (it's impossible that all bits are 1 or 0, because in that case }
  1800. { this function wouldn't have been called) }
  1801. l1 := 31;
  1802. while (((a and testbit) <> 0) = compare) do
  1803. begin
  1804. testbit := testbit shl 1;
  1805. dec(l1);
  1806. end;
  1807. { check the length of the run of bits that comes next }
  1808. compare := not compare;
  1809. l2 := l1;
  1810. while (((a and testbit) <> 0) = compare) and
  1811. (l2 >= 0) do
  1812. begin
  1813. testbit := testbit shl 1;
  1814. dec(l2);
  1815. end;
  1816. { and finally the check whether the rest of the bits all have the }
  1817. { same value }
  1818. compare := not compare;
  1819. temp := l2;
  1820. if temp >= 0 then
  1821. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1822. exit;
  1823. { we have done "not(not(compare))", so compare is back to its }
  1824. { initial value. If the lowest bit was 0, a is of the form }
  1825. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1826. { because l2 now contains the position of the last zero of the }
  1827. { first run instead of that of the first 1) so switch l1 and l2 }
  1828. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1829. if not compare then
  1830. begin
  1831. temp := l1;
  1832. l1 := l2+1;
  1833. l2 := temp;
  1834. end
  1835. else
  1836. { otherwise, l1 currently contains the position of the last }
  1837. { zero instead of that of the first 1 of the second run -> +1 }
  1838. inc(l1);
  1839. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1840. l1 := l1 and 31;
  1841. l2 := l2 and 31;
  1842. get_rlwi_const := true;
  1843. end;
  1844. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1845. ref: treference);
  1846. var
  1847. tmpreg: tregister;
  1848. tmpref: treference;
  1849. largeOffset: Boolean;
  1850. begin
  1851. tmpreg := NR_NO;
  1852. if target_info.system = system_powerpc_macos then
  1853. begin
  1854. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1855. high(smallint)-low(smallint));
  1856. if assigned(ref.symbol) then
  1857. begin {Load symbol's value}
  1858. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1859. reference_reset(tmpref);
  1860. tmpref.symbol := ref.symbol;
  1861. tmpref.base := NR_RTOC;
  1862. if macos_direct_globals then
  1863. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1864. else
  1865. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1866. end;
  1867. if largeOffset then
  1868. begin {Add hi part of offset}
  1869. reference_reset(tmpref);
  1870. if Smallint(Lo(ref.offset)) < 0 then
  1871. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  1872. else
  1873. tmpref.offset := Hi(ref.offset);
  1874. if (tmpreg <> NR_NO) then
  1875. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  1876. else
  1877. begin
  1878. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1879. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1880. end;
  1881. end;
  1882. if (tmpreg <> NR_NO) then
  1883. begin
  1884. {Add content of base register}
  1885. if ref.base <> NR_NO then
  1886. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1887. ref.base,tmpreg));
  1888. {Make ref ready to be used by op}
  1889. ref.symbol:= nil;
  1890. ref.base:= tmpreg;
  1891. if largeOffset then
  1892. ref.offset := Smallint(Lo(ref.offset));
  1893. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1894. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1895. end
  1896. else
  1897. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1898. end
  1899. else {if target_info.system <> system_powerpc_macos}
  1900. begin
  1901. if assigned(ref.symbol) or
  1902. (cardinal(ref.offset-low(smallint)) >
  1903. high(smallint)-low(smallint)) then
  1904. begin
  1905. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1906. reference_reset(tmpref);
  1907. tmpref.symbol := ref.symbol;
  1908. tmpref.relsymbol := ref.relsymbol;
  1909. tmpref.offset := ref.offset;
  1910. tmpref.refaddr := addr_hi;
  1911. if ref.base <> NR_NO then
  1912. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1913. ref.base,tmpref))
  1914. else
  1915. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1916. ref.base := tmpreg;
  1917. ref.refaddr := addr_lo;
  1918. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1919. end
  1920. else
  1921. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1922. end;
  1923. end;
  1924. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1925. crval: longint; l: tasmlabel);
  1926. var
  1927. p: taicpu;
  1928. begin
  1929. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name,AB_EXTERNAL,AT_FUNCTION));
  1930. if op <> A_B then
  1931. create_cond_norm(c,crval,p.condition);
  1932. p.is_jmp := true;
  1933. list.concat(p)
  1934. end;
  1935. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  1936. begin
  1937. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  1938. end;
  1939. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : int64;reg : tregister64);
  1940. begin
  1941. a_op64_const_reg_reg(list,op,value,reg,reg);
  1942. end;
  1943. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  1944. begin
  1945. case op of
  1946. OP_AND,OP_OR,OP_XOR:
  1947. begin
  1948. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1949. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1950. end;
  1951. OP_ADD:
  1952. begin
  1953. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1954. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1955. end;
  1956. OP_SUB:
  1957. begin
  1958. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1959. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1960. end;
  1961. else
  1962. internalerror(2002072801);
  1963. end;
  1964. end;
  1965. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64;regsrc,regdst : tregister64);
  1966. const
  1967. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1968. (A_SUBIC,A_SUBC,A_ADDME));
  1969. var
  1970. tmpreg: tregister;
  1971. tmpreg64: tregister64;
  1972. issub: boolean;
  1973. begin
  1974. case op of
  1975. OP_AND,OP_OR,OP_XOR:
  1976. begin
  1977. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  1978. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1979. regdst.reghi);
  1980. end;
  1981. OP_ADD, OP_SUB:
  1982. begin
  1983. if (value < 0) then
  1984. begin
  1985. if op = OP_ADD then
  1986. op := OP_SUB
  1987. else
  1988. op := OP_ADD;
  1989. value := -value;
  1990. end;
  1991. if (longint(value) <> 0) then
  1992. begin
  1993. issub := op = OP_SUB;
  1994. if (value > 0) and
  1995. (value-ord(issub) <= 32767) then
  1996. begin
  1997. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1998. regdst.reglo,regsrc.reglo,longint(value)));
  1999. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2000. regdst.reghi,regsrc.reghi));
  2001. end
  2002. else if ((value shr 32) = 0) then
  2003. begin
  2004. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2005. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2006. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2007. regdst.reglo,regsrc.reglo,tmpreg));
  2008. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2009. regdst.reghi,regsrc.reghi));
  2010. end
  2011. else
  2012. begin
  2013. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2014. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2015. a_load64_const_reg(list,value,tmpreg64);
  2016. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2017. end
  2018. end
  2019. else
  2020. begin
  2021. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2022. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2023. regdst.reghi);
  2024. end;
  2025. end;
  2026. else
  2027. internalerror(2002072802);
  2028. end;
  2029. end;
  2030. begin
  2031. cg := tcgppc.create;
  2032. cg64 :=tcg64fppc.create;
  2033. end.
  2034. {
  2035. $Log$
  2036. Revision 1.189 2004-12-24 11:51:55 jonas
  2037. * fixed a_jmp_name() for darwin
  2038. Revision 1.188 2004/12/11 12:42:28 jonas
  2039. * fixed synchronising 64bit regvars on 32bit systems at the start and
  2040. end of procedures
  2041. * hack for ppc for loading of paras from their callee location to local
  2042. temps
  2043. Revision 1.187 2004/12/04 21:47:46 jonas
  2044. * modifications to work with the generic code to copy LOC_REFERENCE
  2045. parameters to local temps (fixes tests/test/cg/tmanypara)
  2046. Revision 1.186 2004/11/15 23:35:31 peter
  2047. * tparaitem removed, use tparavarsym instead
  2048. * parameter order is now calculated from paranr value in tparavarsym
  2049. Revision 1.185 2004/11/11 19:31:33 peter
  2050. * fixed compile of powerpc,sparc,arm
  2051. Revision 1.184 2004/10/31 21:45:03 peter
  2052. * generic tlocation
  2053. * move tlocation to cgutils
  2054. Revision 1.183 2004/10/26 18:21:29 jonas
  2055. + empty g_save_standard_registers/g_restore_standard_registers overrides
  2056. (their work was/is done by g_proc_entry/g_proc_exit, and the generic
  2057. version saves the registers in the wrong place)
  2058. Revision 1.182 2004/10/24 20:01:08 peter
  2059. * remove saveregister calling convention
  2060. Revision 1.181 2004/10/24 11:53:45 peter
  2061. * fixed compilation with removed loadref
  2062. Revision 1.180 2004/10/20 07:32:42 jonas
  2063. + support for nostackframe directive
  2064. Revision 1.179 2004/10/11 07:13:14 jonas
  2065. * include pi_do_call if we generate a call instead of internalerroring
  2066. (workaround)
  2067. Revision 1.178 2004/09/25 14:23:54 peter
  2068. * ungetregister is now only used for cpuregisters, renamed to
  2069. ungetcpuregister
  2070. * renamed (get|unget)explicitregister(s) to ..cpuregister
  2071. * removed location-release/reference_release
  2072. Revision 1.177 2004/09/21 17:25:12 peter
  2073. * paraloc branch merged
  2074. Revision 1.176.4.2 2004/09/18 20:21:08 jonas
  2075. * fixed ppc, but still needs fix in tgobj
  2076. Revision 1.176.4.1 2004/09/10 11:10:08 florian
  2077. * first part of ppc fixes
  2078. Revision 1.176 2004/07/17 14:48:20 jonas
  2079. * fixed op_const_reg_reg for (OP_ADD,0,reg1,reg2)
  2080. Revision 1.175 2004/07/09 21:45:24 jonas
  2081. * fixed passing of fpu paras on the stack
  2082. * fixed number of fpu parameters passed in registers
  2083. * skip corresponding integer registers when using an fpu register for a
  2084. parameter under the AIX abi
  2085. Revision 1.174 2004/07/01 18:00:00 jonas
  2086. * fixed several errors due to aword -> aint change
  2087. Revision 1.173 2004/06/20 08:55:32 florian
  2088. * logs truncated
  2089. Revision 1.172 2004/06/17 16:55:46 peter
  2090. * powerpc compiles again
  2091. Revision 1.171 2004/06/02 17:18:10 jonas
  2092. * parameters passed on the stack now also work as register variables
  2093. Revision 1.170 2004/05/31 18:08:41 jonas
  2094. * changed calling of external procedures to be the same as under gcc
  2095. (don't worry about all the generated stubs, they're optimized away
  2096. by the linker)
  2097. -> side effect: no need anymore to use special declarations for
  2098. external C functions under Darwin compared to other platforms
  2099. (it's still necessary for variables though)
  2100. Revision 1.169 2004/04/04 17:50:36 olle
  2101. * macos: fixed large offsets in references
  2102. Revision 1.168 2004/03/06 21:37:45 florian
  2103. * fixed ppc compilation
  2104. }