cgcpu.pas 88 KB

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