cgcpu.pas 92 KB

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