cgcpu.pas 91 KB

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