cgcpu.pas 90 KB

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