cgcpu.pas 89 KB

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