cgcpu.pas 89 KB

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