cgcpu.pas 91 KB

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