cgcpu.pas 93 KB

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