cgcpu.pas 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cgbase,cgobj,
  23. aasmbase,aasmcpu,aasmtai,
  24. cpubase,cpuinfo,node,cg64f32,cginfo;
  25. type
  26. tcgppc = class(tcg)
  27. { passing parameters, per default the parameter is pushed }
  28. { nr gives the number of the parameter (enumerated from }
  29. { left to right), this allows to move the parameter to }
  30. { register, if the cpu supports register calling }
  31. { conventions }
  32. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  33. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  34. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  35. procedure a_call_name(list : taasmoutput;const s : string);override;
  36. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  37. procedure a_call_ref(list : taasmoutput;const ref : treference);override;
  38. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  39. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  40. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  41. size: tcgsize; a: aword; src, dst: tregister); override;
  42. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  43. size: tcgsize; src1, src2, dst: tregister); override;
  44. { move instructions }
  45. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  46. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  47. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const Ref : treference;reg : tregister);override;
  48. procedure a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  49. { fpu move instructions }
  50. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  51. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  52. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  53. { comparison operations }
  54. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  55. l : tasmlabel);override;
  56. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  57. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  58. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  59. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  60. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);override;
  61. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  62. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  63. procedure g_restore_frame_pointer(list : taasmoutput);override;
  64. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  65. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  66. procedure g_overflowcheck(list: taasmoutput; const p: tnode); override;
  67. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  68. { that's the case, we can use rlwinm to do an AND operation }
  69. function get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  70. procedure g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  71. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  72. procedure g_save_all_registers(list : taasmoutput);override;
  73. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);override;
  74. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  75. private
  76. procedure g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  77. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  78. procedure g_stackframe_entry_aix(list : taasmoutput;localsize : longint);
  79. procedure g_return_from_proc_aix(list : taasmoutput;parasize : aword);
  80. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  81. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  82. { Make sure ref is a valid reference for the PowerPC and sets the }
  83. { base to the value of the index if (base = R_NO). }
  84. { Returns true if the reference contained a base, index and an }
  85. { offset or symbol, in which case the base will have been changed }
  86. { to a tempreg (which has to be freed by the caller) containing }
  87. { the sum of part of the original reference }
  88. function fixref(list: taasmoutput; var ref: treference): boolean;
  89. { returns whether a reference can be used immediately in a powerpc }
  90. { instruction }
  91. function issimpleref(const ref: treference): boolean;
  92. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  93. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  94. ref: treference);
  95. { creates the correct branch instruction for a given combination }
  96. { of asmcondflags and destination addressing mode }
  97. procedure a_jmp(list: taasmoutput; op: tasmop;
  98. c: tasmcondflag; crval: longint; l: tasmlabel);
  99. end;
  100. tcg64fppc = class(tcg64f32)
  101. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  102. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  103. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  104. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  105. end;
  106. const
  107. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  108. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  109. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  110. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  111. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  112. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  113. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  114. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  115. implementation
  116. uses
  117. globtype,globals,verbose,systems,cutils,symconst,symdef,symsym,rgobj,tgobj,cpupi;
  118. { parameter passing... Still needs extra support from the processor }
  119. { independent code generator }
  120. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  121. var
  122. ref: treference;
  123. begin
  124. case locpara.loc of
  125. LOC_REGISTER,LOC_CREGISTER:
  126. a_load_const_reg(list,size,a,locpara.register);
  127. LOC_REFERENCE:
  128. begin
  129. reference_reset(ref);
  130. ref.base:=locpara.reference.index;
  131. ref.offset:=locpara.reference.offset;
  132. a_load_const_ref(list,size,a,ref);
  133. end;
  134. else
  135. internalerror(2002081101);
  136. end;
  137. if locpara.sp_fixup<>0 then
  138. internalerror(2002081102);
  139. end;
  140. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  141. var
  142. ref: treference;
  143. tmpreg: tregister;
  144. begin
  145. case locpara.loc of
  146. LOC_REGISTER,LOC_CREGISTER:
  147. a_load_ref_reg(list,size,r,locpara.register);
  148. LOC_REFERENCE:
  149. begin
  150. reference_reset(ref);
  151. ref.base:=locpara.reference.index;
  152. ref.offset:=locpara.reference.offset;
  153. tmpreg := get_scratch_reg_int(list,size);
  154. a_load_ref_reg(list,size,r,tmpreg);
  155. a_load_reg_ref(list,size,tmpreg,ref);
  156. free_scratch_reg(list,tmpreg);
  157. end;
  158. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  159. case size of
  160. OS_32:
  161. a_loadfpu_ref_reg(list,OS_F32,r,locpara.register);
  162. OS_64:
  163. a_loadfpu_ref_reg(list,OS_F64,r,locpara.register);
  164. else
  165. internalerror(2002072801);
  166. end;
  167. else
  168. internalerror(2002081103);
  169. end;
  170. if locpara.sp_fixup<>0 then
  171. internalerror(2002081104);
  172. end;
  173. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  174. var
  175. ref: treference;
  176. tmpreg: tregister;
  177. begin
  178. case locpara.loc of
  179. LOC_REGISTER,LOC_CREGISTER:
  180. a_loadaddr_ref_reg(list,r,locpara.register);
  181. LOC_REFERENCE:
  182. begin
  183. reference_reset(ref);
  184. ref.base := locpara.reference.index;
  185. ref.offset := locpara.reference.offset;
  186. tmpreg := get_scratch_reg_address(list);
  187. a_loadaddr_ref_reg(list,r,tmpreg);
  188. a_load_reg_ref(list,OS_ADDR,tmpreg,ref);
  189. free_scratch_reg(list,tmpreg);
  190. end;
  191. else
  192. internalerror(2002080701);
  193. end;
  194. end;
  195. { calling a procedure by name }
  196. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  197. var
  198. href : treference;
  199. begin
  200. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  201. if it is a cross-TOC call. If so, it also replaces the NOP
  202. with some restore code.}
  203. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  204. if target_info.system=system_powerpc_macos then
  205. list.concat(taicpu.op_none(A_NOP));
  206. include(current_procinfo.flags,pi_do_call);
  207. end;
  208. { calling a procedure by address }
  209. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  210. var
  211. tmpreg : tregister;
  212. tmpref : treference;
  213. begin
  214. if target_info.system=system_powerpc_macos then
  215. begin
  216. {Generate instruction to load the procedure address from
  217. the transition vector.}
  218. //TODO: Support cross-TOC calls.
  219. tmpreg := get_scratch_reg_int(list,OS_INT);
  220. reference_reset(tmpref);
  221. tmpref.offset := 0;
  222. //tmpref.symaddr := refs_full;
  223. tmpref.base:= reg;
  224. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  225. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  226. free_scratch_reg(list,tmpreg);
  227. end
  228. else
  229. list.concat(taicpu.op_reg(A_MTCTR,reg));
  230. list.concat(taicpu.op_none(A_BCTRL));
  231. //if target_info.system=system_powerpc_macos then
  232. // //NOP is not needed here.
  233. // list.concat(taicpu.op_none(A_NOP));
  234. include(current_procinfo.flags,pi_do_call);
  235. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  236. end;
  237. { calling a procedure by address }
  238. procedure tcgppc.a_call_ref(list : taasmoutput;const ref : treference);
  239. var
  240. tmpreg : tregister;
  241. tmpref : treference;
  242. begin
  243. tmpreg := get_scratch_reg_int(list,OS_ADDR);
  244. a_load_ref_reg(list,OS_ADDR,ref,tmpreg);
  245. if target_info.system=system_powerpc_macos then
  246. begin
  247. {Generate instruction to load the procedure address from
  248. the transition vector.}
  249. //TODO: Support cross-TOC calls.
  250. reference_reset(tmpref);
  251. tmpref.offset := 0;
  252. //tmpref.symaddr := refs_full;
  253. tmpref.base:= tmpreg;
  254. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  255. end;
  256. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  257. free_scratch_reg(list,tmpreg);
  258. list.concat(taicpu.op_none(A_BCTRL));
  259. //if target_info.system=system_powerpc_macos then
  260. // //NOP is not needed here.
  261. // list.concat(taicpu.op_none(A_NOP));
  262. include(current_procinfo.flags,pi_do_call);
  263. //list.concat(tai_comment.create(strpnew('***** a_call_ref')));
  264. end;
  265. {********************** load instructions ********************}
  266. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  267. begin
  268. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  269. internalerror(2002090902);
  270. if (longint(a) >= low(smallint)) and
  271. (longint(a) <= high(smallint)) then
  272. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  273. else if ((a and $ffff) <> 0) then
  274. begin
  275. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  276. if ((a shr 16) <> 0) or
  277. (smallint(a and $ffff) < 0) then
  278. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  279. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  280. end
  281. else
  282. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  283. end;
  284. procedure tcgppc.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  285. const
  286. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  287. { indexed? updating?}
  288. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  289. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  290. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  291. var
  292. op: TAsmOp;
  293. ref2: TReference;
  294. freereg: boolean;
  295. begin
  296. ref2 := ref;
  297. freereg := fixref(list,ref2);
  298. if size in [OS_S8..OS_S16] then
  299. { storing is the same for signed and unsigned values }
  300. size := tcgsize(ord(size)-(ord(OS_S8)-ord(OS_8)));
  301. { 64 bit stuff should be handled separately }
  302. if size in [OS_64,OS_S64] then
  303. internalerror(200109236);
  304. op := storeinstr[tcgsize2unsigned[size],ref2.index.number<>NR_NO,false];
  305. a_load_store(list,op,reg,ref2);
  306. if freereg then
  307. cg.free_scratch_reg(list,ref2.base);
  308. End;
  309. procedure tcgppc.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  310. const
  311. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  312. { indexed? updating?}
  313. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  314. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  315. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  316. { 64bit stuff should be handled separately }
  317. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  318. { there's no load-byte-with-sign-extend :( }
  319. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  320. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  321. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  322. var
  323. op: tasmop;
  324. tmpreg: tregister;
  325. ref2, tmpref: treference;
  326. freereg: boolean;
  327. begin
  328. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  329. internalerror(2002090902);
  330. ref2 := ref;
  331. freereg := fixref(list,ref2);
  332. op := loadinstr[size,ref2.index.number<>NR_NO,false];
  333. a_load_store(list,op,reg,ref2);
  334. if freereg then
  335. free_scratch_reg(list,ref2.base);
  336. { sign extend shortint if necessary, since there is no }
  337. { load instruction that does that automatically (JM) }
  338. if size = OS_S8 then
  339. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  340. end;
  341. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  342. begin
  343. if (reg1.enum<>R_INTREGISTER) or (reg1.number = 0) then
  344. internalerror(200303101);
  345. if (reg2.enum<>R_INTREGISTER) or (reg2.number = 0) then
  346. internalerror(200303102);
  347. if (reg1.number<>reg2.number) or
  348. (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  349. ((tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  350. (tosize <> fromsize) and
  351. not(fromsize in [OS_32,OS_S32])) then
  352. begin
  353. case tosize of
  354. OS_8:
  355. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  356. reg2,reg1,0,31-8+1,31));
  357. OS_S8:
  358. list.concat(taicpu.op_reg_reg(A_EXTSB,reg2,reg1));
  359. OS_16:
  360. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  361. reg2,reg1,0,31-16+1,31));
  362. OS_S16:
  363. list.concat(taicpu.op_reg_reg(A_EXTSH,reg2,reg1));
  364. OS_32,OS_S32:
  365. list.concat(taicpu.op_reg_reg(A_MR,reg2,reg1));
  366. else internalerror(2002090901);
  367. end;
  368. end;
  369. end;
  370. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  371. begin
  372. list.concat(taicpu.op_reg_reg(A_FMR,reg2,reg1));
  373. end;
  374. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  375. const
  376. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  377. { indexed? updating?}
  378. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  379. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  380. var
  381. op: tasmop;
  382. ref2: treference;
  383. freereg: boolean;
  384. begin
  385. { several functions call this procedure with OS_32 or OS_64 }
  386. { so this makes life easier (FK) }
  387. case size of
  388. OS_32,OS_F32:
  389. size:=OS_F32;
  390. OS_64,OS_F64,OS_C64:
  391. size:=OS_F64;
  392. else
  393. internalerror(200201121);
  394. end;
  395. ref2 := ref;
  396. freereg := fixref(list,ref2);
  397. op := fpuloadinstr[size,ref2.index.number <> NR_NO,false];
  398. a_load_store(list,op,reg,ref2);
  399. if freereg then
  400. cg.free_scratch_reg(list,ref2.base);
  401. end;
  402. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  403. const
  404. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  405. { indexed? updating?}
  406. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  407. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  408. var
  409. op: tasmop;
  410. ref2: treference;
  411. freereg: boolean;
  412. begin
  413. if not(size in [OS_F32,OS_F64]) then
  414. internalerror(200201122);
  415. ref2 := ref;
  416. freereg := fixref(list,ref2);
  417. op := fpustoreinstr[size,ref2.index.number <> NR_NO,false];
  418. a_load_store(list,op,reg,ref2);
  419. if freereg then
  420. cg.free_scratch_reg(list,ref2.base);
  421. end;
  422. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  423. var
  424. scratch_register: TRegister;
  425. begin
  426. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  427. end;
  428. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  429. begin
  430. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  431. end;
  432. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  433. size: tcgsize; a: aword; src, dst: tregister);
  434. var
  435. l1,l2: longint;
  436. oplo, ophi: tasmop;
  437. scratchreg: tregister;
  438. useReg, gotrlwi: boolean;
  439. procedure do_lo_hi;
  440. begin
  441. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  442. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  443. end;
  444. begin
  445. if src.enum<>R_INTREGISTER then
  446. internalerror(200303102);
  447. if op = OP_SUB then
  448. begin
  449. {$ifopt q+}
  450. {$q-}
  451. {$define overflowon}
  452. {$endif}
  453. a_op_const_reg_reg(list,OP_ADD,size,aword(-longint(a)),src,dst);
  454. {$ifdef overflowon}
  455. {$q+}
  456. {$undef overflowon}
  457. {$endif}
  458. exit;
  459. end;
  460. ophi := TOpCG2AsmOpConstHi[op];
  461. oplo := TOpCG2AsmOpConstLo[op];
  462. gotrlwi := get_rlwi_const(a,l1,l2);
  463. if (op in [OP_AND,OP_OR,OP_XOR]) then
  464. begin
  465. if (a = 0) then
  466. begin
  467. if op = OP_AND then
  468. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  469. else
  470. a_load_reg_reg(list,size,size,src,dst);
  471. exit;
  472. end
  473. else if (a = high(aword)) then
  474. begin
  475. case op of
  476. OP_OR:
  477. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  478. OP_XOR:
  479. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  480. OP_AND:
  481. a_load_reg_reg(list,size,size,src,dst);
  482. end;
  483. exit;
  484. end
  485. else if (a <= high(word)) and
  486. ((op <> OP_AND) or
  487. not gotrlwi) then
  488. begin
  489. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  490. exit;
  491. end;
  492. { all basic constant instructions also have a shifted form that }
  493. { works only on the highest 16bits, so if lo(a) is 0, we can }
  494. { use that one }
  495. if (word(a) = 0) and
  496. (not(op = OP_AND) or
  497. not gotrlwi) then
  498. begin
  499. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  500. exit;
  501. end;
  502. end
  503. else if (op = OP_ADD) then
  504. if a = 0 then
  505. exit
  506. else if (longint(a) >= low(smallint)) and
  507. (longint(a) <= high(smallint)) then
  508. begin
  509. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  510. exit;
  511. end;
  512. { otherwise, the instructions we can generate depend on the }
  513. { operation }
  514. useReg := false;
  515. case op of
  516. OP_DIV,OP_IDIV:
  517. if (a = 0) then
  518. internalerror(200208103)
  519. else if (a = 1) then
  520. begin
  521. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  522. exit
  523. end
  524. else if ispowerof2(a,l1) then
  525. begin
  526. case op of
  527. OP_DIV:
  528. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  529. OP_IDIV:
  530. begin
  531. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  532. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  533. end;
  534. end;
  535. exit;
  536. end
  537. else
  538. usereg := true;
  539. OP_IMUL, OP_MUL:
  540. if (a = 0) then
  541. begin
  542. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  543. exit
  544. end
  545. else if (a = 1) then
  546. begin
  547. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  548. exit
  549. end
  550. else if ispowerof2(a,l1) then
  551. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  552. else if (longint(a) >= low(smallint)) and
  553. (longint(a) <= high(smallint)) then
  554. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  555. else
  556. usereg := true;
  557. OP_ADD:
  558. begin
  559. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  560. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  561. smallint((a shr 16) + ord(smallint(a) < 0))));
  562. end;
  563. OP_OR:
  564. { try to use rlwimi }
  565. if gotrlwi and
  566. (src.number = dst.number) then
  567. begin
  568. scratchreg := get_scratch_reg_int(list,OS_INT);
  569. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  570. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  571. scratchreg,0,l1,l2));
  572. free_scratch_reg(list,scratchreg);
  573. end
  574. else
  575. do_lo_hi;
  576. OP_AND:
  577. { try to use rlwinm }
  578. if gotrlwi then
  579. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  580. src,0,l1,l2))
  581. else
  582. useReg := true;
  583. OP_XOR:
  584. do_lo_hi;
  585. OP_SHL,OP_SHR,OP_SAR:
  586. begin
  587. if (a and 31) <> 0 Then
  588. list.concat(taicpu.op_reg_reg_const(
  589. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  590. else
  591. a_load_reg_reg(list,size,size,src,dst);
  592. if (a shr 5) <> 0 then
  593. internalError(68991);
  594. end
  595. else
  596. internalerror(200109091);
  597. end;
  598. { if all else failed, load the constant in a register and then }
  599. { perform the operation }
  600. if useReg then
  601. begin
  602. scratchreg := get_scratch_reg_int(list,OS_INT);
  603. a_load_const_reg(list,OS_32,a,scratchreg);
  604. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  605. free_scratch_reg(list,scratchreg);
  606. end;
  607. end;
  608. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  609. size: tcgsize; src1, src2, dst: tregister);
  610. const
  611. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  612. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  613. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  614. begin
  615. case op of
  616. OP_NEG,OP_NOT:
  617. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  618. else
  619. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  620. end;
  621. end;
  622. {*************** compare instructructions ****************}
  623. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  624. l : tasmlabel);
  625. var
  626. p: taicpu;
  627. scratch_register: TRegister;
  628. signed: boolean;
  629. r:Tregister;
  630. begin
  631. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  632. { in the following case, we generate more efficient code when }
  633. { signed is true }
  634. if (cmp_op in [OC_EQ,OC_NE]) and
  635. (a > $ffff) then
  636. signed := true;
  637. r.enum:=R_CR0;
  638. if signed then
  639. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  640. list.concat(taicpu.op_reg_reg_const(A_CMPWI,r,reg,longint(a)))
  641. else
  642. begin
  643. scratch_register := get_scratch_reg_int(list,OS_INT);
  644. a_load_const_reg(list,OS_32,a,scratch_register);
  645. list.concat(taicpu.op_reg_reg_reg(A_CMPW,r,reg,scratch_register));
  646. free_scratch_reg(list,scratch_register);
  647. end
  648. else
  649. if (a <= $ffff) then
  650. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,r,reg,a))
  651. else
  652. begin
  653. scratch_register := get_scratch_reg_int(list,OS_32);
  654. a_load_const_reg(list,OS_32,a,scratch_register);
  655. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,r,reg,scratch_register));
  656. free_scratch_reg(list,scratch_register);
  657. end;
  658. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  659. end;
  660. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  661. reg1,reg2 : tregister;l : tasmlabel);
  662. var
  663. p: taicpu;
  664. op: tasmop;
  665. r:Tregister;
  666. begin
  667. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  668. op := A_CMPW
  669. else op := A_CMPLW;
  670. r.enum:=R_CR0;
  671. list.concat(taicpu.op_reg_reg_reg(op,r,reg2,reg1));
  672. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  673. end;
  674. procedure tcgppc.g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);
  675. begin
  676. {$warning FIX ME}
  677. end;
  678. procedure tcgppc.g_restore_standard_registers(list : taasmoutput; usedinproc : Tsupregset);
  679. begin
  680. {$warning FIX ME}
  681. end;
  682. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  683. begin
  684. {$warning FIX ME}
  685. end;
  686. procedure tcgppc.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  687. begin
  688. {$warning FIX ME}
  689. end;
  690. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  691. begin
  692. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  693. end;
  694. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  695. begin
  696. a_jmp(list,A_B,C_None,0,l);
  697. end;
  698. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  699. var
  700. c: tasmcond;
  701. r:Tregister;
  702. begin
  703. c := flags_to_cond(f);
  704. r.enum:=R_CR0;
  705. a_jmp(list,A_BC,c.cond,ord(c.cr)-ord(r.enum),l);
  706. end;
  707. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  708. var
  709. testbit: byte;
  710. bitvalue: boolean;
  711. begin
  712. { get the bit to extract from the conditional register + its }
  713. { requested value (0 or 1) }
  714. testbit := ((ord(f.cr)-ord(R_CR0)) * 4);
  715. case f.flag of
  716. F_EQ,F_NE:
  717. begin
  718. inc(testbit,2);
  719. bitvalue := f.flag = F_EQ;
  720. end;
  721. F_LT,F_GE:
  722. begin
  723. bitvalue := f.flag = F_LT;
  724. end;
  725. F_GT,F_LE:
  726. begin
  727. inc(testbit);
  728. bitvalue := f.flag = F_GT;
  729. end;
  730. else
  731. internalerror(200112261);
  732. end;
  733. { load the conditional register in the destination reg }
  734. list.concat(taicpu.op_reg(A_MFCR,reg));
  735. { we will move the bit that has to be tested to bit 0 by rotating }
  736. { left }
  737. testbit := (testbit + 1) and 31;
  738. { extract bit }
  739. list.concat(taicpu.op_reg_reg_const_const_const(
  740. A_RLWINM,reg,reg,testbit,31,31));
  741. { if we need the inverse, xor with 1 }
  742. if not bitvalue then
  743. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  744. end;
  745. (*
  746. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  747. var
  748. testbit: byte;
  749. bitvalue: boolean;
  750. begin
  751. { get the bit to extract from the conditional register + its }
  752. { requested value (0 or 1) }
  753. case f.simple of
  754. false:
  755. begin
  756. { we don't generate this in the compiler }
  757. internalerror(200109062);
  758. end;
  759. true:
  760. case f.cond of
  761. C_None:
  762. internalerror(200109063);
  763. C_LT..C_NU:
  764. begin
  765. testbit := (ord(f.cr) - ord(R_CR0))*4;
  766. inc(testbit,AsmCondFlag2BI[f.cond]);
  767. bitvalue := AsmCondFlagTF[f.cond];
  768. end;
  769. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  770. begin
  771. testbit := f.crbit
  772. bitvalue := AsmCondFlagTF[f.cond];
  773. end;
  774. else
  775. internalerror(200109064);
  776. end;
  777. end;
  778. { load the conditional register in the destination reg }
  779. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  780. { we will move the bit that has to be tested to bit 31 -> rotate }
  781. { left by bitpos+1 (remember, this is big-endian!) }
  782. if bitpos <> 31 then
  783. inc(bitpos)
  784. else
  785. bitpos := 0;
  786. { extract bit }
  787. list.concat(taicpu.op_reg_reg_const_const_const(
  788. A_RLWINM,reg,reg,bitpos,31,31));
  789. { if we need the inverse, xor with 1 }
  790. if not bitvalue then
  791. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  792. end;
  793. *)
  794. { *********** entry/exit code and address loading ************ }
  795. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  796. begin
  797. case target_info.abi of
  798. abi_powerpc_macos:
  799. g_stackframe_entry_mac(list,localsize);
  800. abi_powerpc_sysv:
  801. g_stackframe_entry_sysv(list,localsize);
  802. abi_powerpc_aix:
  803. g_stackframe_entry_aix(list,localsize);
  804. else
  805. internalerror(2204001);
  806. end;
  807. end;
  808. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  809. begin
  810. case target_info.abi of
  811. abi_powerpc_macos:
  812. g_return_from_proc_mac(list,parasize);
  813. abi_powerpc_sysv:
  814. g_return_from_proc_sysv(list,parasize);
  815. abi_powerpc_aix:
  816. g_return_from_proc_aix(list,parasize);
  817. else
  818. internalerror(2204001);
  819. end;
  820. end;
  821. procedure tcgppc.g_stackframe_entry_aix(list : taasmoutput;localsize : longint);
  822. begin
  823. g_stackframe_entry_sysv(list,localsize);
  824. end;
  825. procedure tcgppc.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  826. { generated the entry code of a procedure/function. Note: localsize is the }
  827. { sum of the size necessary for local variables and the maximum possible }
  828. { combined size of ALL the parameters of a procedure called by the current }
  829. { one }
  830. var regcounter,firstregfpu,firstreggpr: TRegister;
  831. href,href2 : treference;
  832. usesfpr,usesgpr,gotgot : boolean;
  833. parastart : aword;
  834. offset : aword;
  835. r,r2,rsp:Tregister;
  836. regcounter2: Tsuperregister;
  837. hp: tparaitem;
  838. begin
  839. { we do our own localsize calculation }
  840. localsize:=0;
  841. { CR and LR only have to be saved in case they are modified by the current }
  842. { procedure, but currently this isn't checked, so save them always }
  843. { following is the entry code as described in "Altivec Programming }
  844. { Interface Manual", bar the saving of AltiVec registers }
  845. rsp.enum:=R_INTREGISTER;
  846. rsp.number:=NR_STACK_POINTER_REG;
  847. a_reg_alloc(list,rsp);
  848. r.enum:=R_INTREGISTER;
  849. r.number:=NR_R0;
  850. a_reg_alloc(list,r);
  851. if current_procdef.parast.symtablelevel>1 then
  852. begin
  853. r.enum:=R_INTREGISTER;
  854. r.number:=NR_R11;
  855. a_reg_alloc(list,r);
  856. end;
  857. { allocate registers containing reg parameters }
  858. r.enum := R_INTREGISTER;
  859. for regcounter2 := RS_R3 to RS_R10 do
  860. begin
  861. r.number:=regcounter2 shl 8;
  862. a_reg_alloc(list,r);
  863. end;
  864. usesfpr:=false;
  865. if not (po_assembler in current_procdef.procoptions) then
  866. for regcounter.enum:=R_F14 to R_F31 do
  867. if regcounter.enum in rg.usedbyproc then
  868. begin
  869. usesfpr:= true;
  870. firstregfpu:=regcounter;
  871. break;
  872. end;
  873. usesgpr:=false;
  874. if not (po_assembler in current_procdef.procoptions) then
  875. for regcounter2:=firstsaveintreg to RS_R31 do
  876. begin
  877. if regcounter2 in rg.usedintbyproc then
  878. begin
  879. usesgpr:=true;
  880. firstreggpr.enum := R_INTREGISTER;
  881. firstreggpr.number := regcounter2 shl 8;
  882. break;
  883. end;
  884. end;
  885. { save link register? }
  886. if not (po_assembler in current_procdef.procoptions) then
  887. if (pi_do_call in current_procinfo.flags) then
  888. begin
  889. { save return address... }
  890. r.enum:=R_INTREGISTER;
  891. r.number:=NR_R0;
  892. list.concat(taicpu.op_reg(A_MFLR,r));
  893. { ... in caller's rframe }
  894. reference_reset_base(href,rsp,4);
  895. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  896. a_reg_dealloc(list,r);
  897. end;
  898. { !!! always allocate space for all registers for now !!! }
  899. if not (po_assembler in current_procdef.procoptions) then
  900. { if usesfpr or usesgpr then }
  901. begin
  902. r.enum:=R_INTREGISTER;
  903. r.number:=NR_R12;
  904. a_reg_alloc(list,r);
  905. { save end of fpr save area }
  906. list.concat(taicpu.op_reg_reg(A_MR,r,rsp));
  907. end;
  908. { calculate the size of the locals }
  909. {
  910. if usesgpr then
  911. inc(localsize,((NR_R31-firstreggpr.number) shr 8+1)*4);
  912. if usesfpr then
  913. inc(localsize,(ord(R_F31)-ord(firstregfpu.enum)+1)*8);
  914. }
  915. { !!! always allocate space for all registers for now !!! }
  916. if not (po_assembler in current_procdef.procoptions) then
  917. inc(localsize,(31-13+1)*4+(31-14+1)*8);
  918. { align to 16 bytes }
  919. localsize:=align(localsize,16);
  920. inc(localsize,tg.lasttemp);
  921. localsize:=align(localsize,16);
  922. tppcprocinfo(current_procinfo).localsize:=localsize;
  923. if (localsize <> 0) then
  924. begin
  925. r.enum:=R_INTREGISTER;
  926. r.number:=NR_STACK_POINTER_REG;
  927. if (localsize <= high(smallint)) then
  928. begin
  929. reference_reset_base(href,r,-localsize);
  930. a_load_store(list,A_STWU,r,href);
  931. end
  932. else
  933. begin
  934. reference_reset_base(href,r,0);
  935. href.index := get_scratch_reg_int(list,OS_32);
  936. a_load_const_reg(list,OS_S32,-localsize,href.index);
  937. a_load_store(list,A_STWUX,r,href);
  938. free_scratch_reg(list,href.index);
  939. end;
  940. end;
  941. { no GOT pointer loaded yet }
  942. gotgot:=false;
  943. r.enum := R_INTREGISTER;
  944. r.NUMBER := NR_R12;
  945. if usesfpr then
  946. begin
  947. { save floating-point registers
  948. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  949. begin
  950. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g');
  951. gotgot:=true;
  952. end
  953. else
  954. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14));
  955. }
  956. reference_reset_base(href,r,-8);
  957. for regcounter.enum:=firstregfpu.enum to R_F31 do
  958. if regcounter.enum in rg.usedbyproc then
  959. begin
  960. a_loadfpu_reg_ref(list,OS_F64,regcounter,href);
  961. dec(href.offset,8);
  962. end;
  963. { compute end of gpr save area }
  964. a_op_const_reg(list,OP_ADD,href.offset+8,r);
  965. end;
  966. { save gprs and fetch GOT pointer }
  967. if usesgpr then
  968. begin
  969. {
  970. if cs_create_pic in aktmoduleswitches then
  971. begin
  972. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g');
  973. gotgot:=true;
  974. end
  975. else
  976. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14))
  977. }
  978. reference_reset_base(href,r,-4);
  979. for regcounter2:=firstsaveintreg to RS_R31 do
  980. begin
  981. if regcounter2 in rg.usedintbyproc then
  982. begin
  983. usesgpr:=true;
  984. r.enum := R_INTREGISTER;
  985. r.number := regcounter2 shl 8;
  986. a_load_reg_ref(list,OS_INT,r,href);
  987. dec(href.offset,4);
  988. end;
  989. end;
  990. {
  991. r.enum:=R_INTREGISTER;
  992. r.number:=NR_R12;
  993. reference_reset_base(href,r,-((NR_R31-firstreggpr.number) shr 8+1)*4);
  994. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  995. }
  996. end;
  997. if assigned(current_procdef.parast) then
  998. begin
  999. if not (po_assembler in current_procdef.procoptions) then
  1000. begin
  1001. { copy memory parameters to local parast }
  1002. r.enum:=R_INTREGISTER;
  1003. r.number:=NR_R12;
  1004. hp:=tparaitem(current_procdef.para.first);
  1005. while assigned(hp) do
  1006. begin
  1007. if (hp.paraloc.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1008. begin
  1009. reference_reset_base(href,current_procinfo.framepointer,tvarsym(hp.parasym).adjusted_address);
  1010. reference_reset_base(href2,r,hp.paraloc.reference.offset);
  1011. cg.a_load_ref_ref(list,hp.paraloc.size,href2,href);
  1012. end;
  1013. hp := tparaitem(hp.next);
  1014. end;
  1015. end;
  1016. end;
  1017. r.enum:=R_INTREGISTER;
  1018. r.number:=NR_R12;
  1019. if usesfpr or usesgpr then
  1020. a_reg_dealloc(list,r);
  1021. { PIC code support, }
  1022. if cs_create_pic in aktmoduleswitches then
  1023. begin
  1024. { if we didn't get the GOT pointer till now, we've to calculate it now }
  1025. if not(gotgot) then
  1026. begin
  1027. {!!!!!!!!!!!!!}
  1028. end;
  1029. r.enum:=R_INTREGISTER;
  1030. r.number:=NR_R31;
  1031. r2.enum:=R_LR;
  1032. a_reg_alloc(list,r);
  1033. { place GOT ptr in r31 }
  1034. list.concat(taicpu.op_reg_reg(A_MFSPR,r,r2));
  1035. end;
  1036. { save the CR if necessary ( !!! always done currently ) }
  1037. { still need to find out where this has to be done for SystemV
  1038. a_reg_alloc(list,R_0);
  1039. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  1040. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  1041. new_reference(STACK_POINTER_REG,LA_CR)));
  1042. a_reg_dealloc(list,R_0); }
  1043. { now comes the AltiVec context save, not yet implemented !!! }
  1044. { if we're in a nested procedure, we've to save R11 }
  1045. if current_procdef.parast.symtablelevel>2 then
  1046. begin
  1047. r.enum:=R_INTREGISTER;
  1048. r.number:=NR_R11;
  1049. reference_reset_base(href,rsp,current_procinfo.framepointer_offset);
  1050. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  1051. end;
  1052. end;
  1053. procedure tcgppc.g_return_from_proc_aix(list : taasmoutput;parasize : aword);
  1054. begin
  1055. g_return_from_proc_sysv(list,parasize);
  1056. end;
  1057. procedure tcgppc.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  1058. var
  1059. regcounter,firstregfpu,firstreggpr: TRegister;
  1060. href : treference;
  1061. usesfpr,usesgpr,genret : boolean;
  1062. r,r2:Tregister;
  1063. regcounter2:Tsuperregister;
  1064. begin
  1065. { release parameter registers }
  1066. r.enum := R_INTREGISTER;
  1067. for regcounter2 := RS_R3 to RS_R10 do
  1068. begin
  1069. r.number:=regcounter2 shl 8;
  1070. a_reg_dealloc(list,r);
  1071. end;
  1072. { AltiVec context restore, not yet implemented !!! }
  1073. usesfpr:=false;
  1074. if not (po_assembler in current_procdef.procoptions) then
  1075. for regcounter.enum:=R_F14 to R_F31 do
  1076. if regcounter.enum in rg.usedbyproc then
  1077. begin
  1078. usesfpr:=true;
  1079. firstregfpu:=regcounter;
  1080. break;
  1081. end;
  1082. usesgpr:=false;
  1083. if not (po_assembler in current_procdef.procoptions) then
  1084. for regcounter2:=firstsaveintreg to RS_R31 do
  1085. begin
  1086. if regcounter2 in rg.usedintbyproc then
  1087. begin
  1088. usesgpr:=true;
  1089. firstreggpr.enum:=R_INTREGISTER;
  1090. firstreggpr.number:=regcounter2 shl 8;
  1091. break;
  1092. end;
  1093. end;
  1094. { no return (blr) generated yet }
  1095. genret:=true;
  1096. if usesgpr or usesfpr then
  1097. begin
  1098. { address of gpr save area to r11 }
  1099. r.enum:=R_INTREGISTER;
  1100. r.number:=NR_STACK_POINTER_REG;
  1101. r2.enum:=R_INTREGISTER;
  1102. r2.number:=NR_R12;
  1103. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tppcprocinfo(current_procinfo).localsize,r,r2);
  1104. if usesfpr then
  1105. begin
  1106. reference_reset_base(href,r2,-8);
  1107. for regcounter.enum := firstregfpu.enum to R_F31 do
  1108. if (regcounter.enum in rg.usedbyproc) then
  1109. begin
  1110. a_loadfpu_ref_reg(list,OS_F64,href,regcounter);
  1111. dec(href.offset,8);
  1112. end;
  1113. inc(href.offset,4);
  1114. end
  1115. else
  1116. reference_reset_base(href,r2,-4);
  1117. for regcounter2:=firstsaveintreg to RS_R31 do
  1118. begin
  1119. if regcounter2 in rg.usedintbyproc then
  1120. begin
  1121. usesgpr:=true;
  1122. r.enum := R_INTREGISTER;
  1123. r.number := regcounter2 shl 8;
  1124. a_load_ref_reg(list,OS_INT,href,r);
  1125. dec(href.offset,4);
  1126. end;
  1127. end;
  1128. (*
  1129. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr.number)) shr 8+1)*4);
  1130. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1131. *)
  1132. end;
  1133. (*
  1134. { restore fprs and return }
  1135. if usesfpr then
  1136. begin
  1137. { address of fpr save area to r11 }
  1138. r.enum:=R_INTREGISTER;
  1139. r.number:=NR_R12;
  1140. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1141. {
  1142. if (pi_do_call in current_procinfo.flags) then
  1143. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1144. '_x')
  1145. else
  1146. { leaf node => lr haven't to be restored }
  1147. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1148. '_l');
  1149. genret:=false;
  1150. }
  1151. end;
  1152. *)
  1153. { if we didn't generate the return code, we've to do it now }
  1154. if genret then
  1155. begin
  1156. { adjust r1 }
  1157. r.enum:=R_INTREGISTER;
  1158. r.number:=NR_R1;
  1159. a_op_const_reg(list,OP_ADD,tppcprocinfo(current_procinfo).localsize,r);
  1160. { load link register? }
  1161. if not (po_assembler in current_procdef.procoptions) then
  1162. if (pi_do_call in current_procinfo.flags) then
  1163. begin
  1164. r.enum:=R_INTREGISTER;
  1165. r.number:=NR_STACK_POINTER_REG;
  1166. reference_reset_base(href,r,4);
  1167. r.enum:=R_INTREGISTER;
  1168. r.number:=NR_R0;
  1169. list.concat(taicpu.op_reg_ref(A_LWZ,r,href));
  1170. list.concat(taicpu.op_reg(A_MTLR,r));
  1171. end;
  1172. list.concat(taicpu.op_none(A_BLR));
  1173. end;
  1174. end;
  1175. function save_regs(list : taasmoutput):longint;
  1176. {Generates code which saves used non-volatile registers in
  1177. the save area right below the address the stackpointer point to.
  1178. Returns the actual used save area size.}
  1179. var regcounter,firstregfpu,firstreggpr: TRegister;
  1180. usesfpr,usesgpr: boolean;
  1181. href : treference;
  1182. offset: integer;
  1183. r,r2:Tregister;
  1184. regcounter2: Tsuperregister;
  1185. begin
  1186. usesfpr:=false;
  1187. if not (po_assembler in current_procdef.procoptions) then
  1188. for regcounter.enum:=R_F14 to R_F31 do
  1189. if regcounter.enum in rg.usedbyproc then
  1190. begin
  1191. usesfpr:=true;
  1192. firstregfpu:=regcounter;
  1193. break;
  1194. end;
  1195. usesgpr:=false;
  1196. if not (po_assembler in current_procdef.procoptions) then
  1197. for regcounter2:=firstsaveintreg to RS_R31 do
  1198. begin
  1199. if regcounter2 in rg.usedintbyproc then
  1200. begin
  1201. usesgpr:=true;
  1202. firstreggpr.enum:=R_INTREGISTER;
  1203. firstreggpr.number:=regcounter2 shl 8;
  1204. break;
  1205. end;
  1206. end;
  1207. offset:= 0;
  1208. { save floating-point registers }
  1209. if usesfpr then
  1210. for regcounter.enum := firstregfpu.enum to R_F31 do
  1211. begin
  1212. offset:= offset - 8;
  1213. r.enum:=R_INTREGISTER;
  1214. r.number:=NR_STACK_POINTER_REG;
  1215. reference_reset_base(href, r, offset);
  1216. list.concat(taicpu.op_reg_ref(A_STFD, regcounter, href));
  1217. end;
  1218. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1219. { save gprs in gpr save area }
  1220. if usesgpr then
  1221. if firstreggpr.enum < R_30 then
  1222. begin
  1223. offset:= offset - 4 * (ord(R_31) - ord(firstreggpr.enum) + 1);
  1224. r.enum:=R_INTREGISTER;
  1225. r.number:=NR_STACK_POINTER_REG;
  1226. reference_reset_base(href,r,offset);
  1227. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1228. {STMW stores multiple registers}
  1229. end
  1230. else
  1231. begin
  1232. r.enum:=R_INTREGISTER;
  1233. r.number:=NR_STACK_POINTER_REG;
  1234. r2 := firstreggpr;
  1235. convert_register_to_enum(firstreggpr);
  1236. for regcounter.enum := firstreggpr.enum to R_31 do
  1237. begin
  1238. offset:= offset - 4;
  1239. reference_reset_base(href, r, offset);
  1240. list.concat(taicpu.op_reg_ref(A_STW, r2, href));
  1241. inc(r2.number,NR_R1-NR_R0);
  1242. end;
  1243. end;
  1244. { now comes the AltiVec context save, not yet implemented !!! }
  1245. save_regs:= -offset;
  1246. end;
  1247. procedure restore_regs(list : taasmoutput);
  1248. {Generates code which restores used non-volatile registers from
  1249. the save area right below the address the stackpointer point to.}
  1250. var regcounter,firstregfpu,firstreggpr: TRegister;
  1251. usesfpr,usesgpr: boolean;
  1252. href : treference;
  1253. offset: integer;
  1254. r,r2:Tregister;
  1255. regcounter2: Tsuperregister;
  1256. begin
  1257. usesfpr:=false;
  1258. if not (po_assembler in current_procdef.procoptions) then
  1259. for regcounter.enum:=R_F14 to R_F31 do
  1260. if regcounter.enum in rg.usedbyproc then
  1261. begin
  1262. usesfpr:=true;
  1263. firstregfpu:=regcounter;
  1264. break;
  1265. end;
  1266. usesgpr:=false;
  1267. if not (po_assembler in current_procdef.procoptions) then
  1268. for regcounter2:=RS_R13 to RS_R31 do
  1269. begin
  1270. if regcounter2 in rg.usedintbyproc then
  1271. begin
  1272. usesgpr:=true;
  1273. firstreggpr.enum:=R_INTREGISTER;
  1274. firstreggpr.number:=regcounter2 shl 8;
  1275. break;
  1276. end;
  1277. end;
  1278. offset:= 0;
  1279. { restore fp registers }
  1280. if usesfpr then
  1281. for regcounter.enum := firstregfpu.enum to R_F31 do
  1282. begin
  1283. offset:= offset - 8;
  1284. r.enum:=R_INTREGISTER;
  1285. r.number:=NR_STACK_POINTER_REG;
  1286. reference_reset_base(href, r, offset);
  1287. list.concat(taicpu.op_reg_ref(A_LFD, regcounter, href));
  1288. end;
  1289. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1290. { restore gprs }
  1291. if usesgpr then
  1292. if firstreggpr.enum < R_30 then
  1293. begin
  1294. offset:= offset - 4 * (ord(R_31) - ord(firstreggpr.enum) + 1);
  1295. r.enum:=R_INTREGISTER;
  1296. r.number:=NR_STACK_POINTER_REG;
  1297. reference_reset_base(href,r,offset); //-220
  1298. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1299. {LMW loads multiple registers}
  1300. end
  1301. else
  1302. begin
  1303. r.enum:=R_INTREGISTER;
  1304. r.number:=NR_STACK_POINTER_REG;
  1305. r2 := firstreggpr;
  1306. convert_register_to_enum(firstreggpr);
  1307. for regcounter.enum := firstreggpr.enum to R_31 do
  1308. begin
  1309. offset:= offset - 4;
  1310. reference_reset_base(href, r, offset);
  1311. list.concat(taicpu.op_reg_ref(A_LWZ, r2, href));
  1312. inc(r2.number,NR_R1-NR_R0);
  1313. end;
  1314. end;
  1315. { now comes the AltiVec context restore, not yet implemented !!! }
  1316. end;
  1317. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1318. { generated the entry code of a procedure/function. Note: localsize is the }
  1319. { sum of the size necessary for local variables and the maximum possible }
  1320. { combined size of ALL the parameters of a procedure called by the current }
  1321. { one }
  1322. const
  1323. macosLinkageAreaSize = 24;
  1324. var regcounter: TRegister;
  1325. href : treference;
  1326. registerSaveAreaSize : longint;
  1327. r,r2,rsp:Tregister;
  1328. regcounter2: Tsuperregister;
  1329. begin
  1330. if (localsize mod 8) <> 0 then internalerror(58991);
  1331. { CR and LR only have to be saved in case they are modified by the current }
  1332. { procedure, but currently this isn't checked, so save them always }
  1333. { following is the entry code as described in "Altivec Programming }
  1334. { Interface Manual", bar the saving of AltiVec registers }
  1335. r.enum:=R_INTREGISTER;
  1336. r.number:=NR_R0;
  1337. rsp.enum:=R_INTREGISTER;
  1338. rsp.number:=NR_STACK_POINTER_REG;
  1339. a_reg_alloc(list,rsp);
  1340. a_reg_alloc(list,r);
  1341. { allocate registers containing reg parameters }
  1342. r.enum := R_INTREGISTER;
  1343. for regcounter2 := RS_R3 to RS_R10 do
  1344. begin
  1345. r.number:=regcounter2 shl 8;
  1346. a_reg_alloc(list,r);
  1347. end;
  1348. {TODO: Allocate fp and altivec parameter registers also}
  1349. { save return address in callers frame}
  1350. r2.enum:=R_LR;
  1351. list.concat(taicpu.op_reg_reg(A_MFSPR,r,r2));
  1352. { ... in caller's frame }
  1353. reference_reset_base(href,rsp,8);
  1354. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  1355. a_reg_dealloc(list,r);
  1356. { save non-volatile registers in callers frame}
  1357. registerSaveAreaSize:= save_regs(list);
  1358. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1359. a_reg_alloc(list,r);
  1360. r2.enum:=R_CR;
  1361. list.concat(taicpu.op_reg_reg(A_MFSPR,r,r2));
  1362. reference_reset_base(href,rsp,LA_CR);
  1363. list.concat(taicpu.op_reg_ref(A_STW,r,href));
  1364. a_reg_dealloc(list,r);
  1365. (*
  1366. { save pointer to incoming arguments }
  1367. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1368. *)
  1369. (*
  1370. a_reg_alloc(list,R_12);
  1371. { 0 or 8 based on SP alignment }
  1372. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1373. R_12,STACK_POINTER_REG,0,28,28));
  1374. { add in stack length }
  1375. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1376. -localsize));
  1377. { establish new alignment }
  1378. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1379. a_reg_dealloc(list,R_12);
  1380. *)
  1381. { allocate stack frame }
  1382. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1383. inc(localsize,tg.lasttemp);
  1384. localsize:=align(localsize,16);
  1385. tppcprocinfo(current_procinfo).localsize:=localsize;
  1386. if (localsize <> 0) then
  1387. begin
  1388. r.enum:=R_INTREGISTER;
  1389. r.number:=NR_STACK_POINTER_REG;
  1390. if (localsize <= high(smallint)) then
  1391. begin
  1392. reference_reset_base(href,r,-localsize);
  1393. a_load_store(list,A_STWU,r,href);
  1394. end
  1395. else
  1396. begin
  1397. reference_reset_base(href,r,0);
  1398. href.index := get_scratch_reg_int(list,OS_32);
  1399. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1400. a_load_store(list,A_STWUX,r,href);
  1401. free_scratch_reg(list,href.index);
  1402. end;
  1403. end;
  1404. end;
  1405. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  1406. var
  1407. regcounter: TRegister;
  1408. href : treference;
  1409. r,r2,rsp:Tregister;
  1410. regcounter2: Tsuperregister;
  1411. begin
  1412. { release parameter registers }
  1413. r.enum := R_INTREGISTER;
  1414. for regcounter2 := RS_R3 to RS_R10 do
  1415. begin
  1416. r.number := regcounter2 shl 8;
  1417. a_reg_dealloc(list,r);
  1418. end;
  1419. {TODO: Release fp and altivec parameter registers also}
  1420. r.enum:=R_INTREGISTER;
  1421. r.number:=NR_R0;
  1422. rsp.enum:=R_INTREGISTER;
  1423. rsp.number:=NR_STACK_POINTER_REG;
  1424. a_reg_alloc(list,r);
  1425. { restore stack pointer }
  1426. reference_reset_base(href,rsp,LA_SP);
  1427. list.concat(taicpu.op_reg_ref(A_LWZ,rsp,href));
  1428. (*
  1429. list.concat(taicpu.op_reg_reg_const(A_ORI,rsp,R_31,0));
  1430. *)
  1431. { restore the CR if necessary from callers frame
  1432. ( !!! always done currently ) }
  1433. reference_reset_base(href,rsp,LA_CR);
  1434. r.enum:=R_INTREGISTER;
  1435. r.number:=NR_R0;
  1436. list.concat(taicpu.op_reg_ref(A_LWZ,r,href));
  1437. r2.enum:=R_CR;
  1438. list.concat(taicpu.op_reg_reg(A_MTSPR,r,r2));
  1439. a_reg_dealloc(list,r);
  1440. (*
  1441. { restore return address from callers frame }
  1442. reference_reset_base(href,STACK_POINTER_REG,8);
  1443. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1444. *)
  1445. { restore non-volatile registers from callers frame }
  1446. restore_regs(list);
  1447. (*
  1448. { return to caller }
  1449. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1450. list.concat(taicpu.op_none(A_BLR));
  1451. *)
  1452. { restore return address from callers frame }
  1453. r.enum:=R_INTREGISTER;
  1454. r.number:=NR_R0;
  1455. r2.enum:=R_LR;
  1456. reference_reset_base(href,rsp,8);
  1457. list.concat(taicpu.op_reg_ref(A_LWZ,r,href));
  1458. { return to caller }
  1459. list.concat(taicpu.op_reg_reg(A_MTSPR,r,r2));
  1460. list.concat(taicpu.op_none(A_BLR));
  1461. end;
  1462. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  1463. begin
  1464. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  1465. end;
  1466. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1467. var
  1468. ref2, tmpref: treference;
  1469. freereg: boolean;
  1470. r2,tmpreg:Tregister;
  1471. begin
  1472. ref2 := ref;
  1473. freereg := fixref(list,ref2);
  1474. if assigned(ref2.symbol) then
  1475. begin
  1476. if target_info.system = system_powerpc_macos then
  1477. begin
  1478. if ref2.base.number <> NR_NO then
  1479. internalerror(2002103102); //TODO: Implement this if needed
  1480. if macos_direct_globals then
  1481. begin
  1482. reference_reset(tmpref);
  1483. tmpref.offset := ref2.offset;
  1484. tmpref.symbol := ref2.symbol;
  1485. tmpref.symaddr := refs_full;
  1486. tmpref.base.number := NR_NO;
  1487. r2.enum:=R_INTREGISTER;
  1488. r2.number:=NR_RTOC;
  1489. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r2,tmpref));
  1490. end
  1491. else
  1492. begin
  1493. reference_reset(tmpref);
  1494. tmpref.symbol := ref2.symbol;
  1495. tmpref.offset := 0; //ref2.offset;
  1496. tmpref.symaddr := refs_full;
  1497. tmpref.base.enum := R_INTREGISTER;
  1498. tmpref.base.number := NR_RTOC;
  1499. if ref2.offset = 0 then
  1500. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref))
  1501. else
  1502. begin
  1503. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1504. reference_reset(tmpref);
  1505. tmpref.offset := ref2.offset;
  1506. tmpref.symaddr := refs_full;
  1507. tmpref.base:= r;
  1508. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1509. (*
  1510. tmpreg := get_scratch_reg_address(list);
  1511. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1512. reference_reset(tmpref);
  1513. tmpref.offset := ref2.offset;
  1514. tmpref.symaddr := refs_full;
  1515. tmpref.base:= tmpreg;
  1516. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1517. free_scratch_reg(list,tmpreg);
  1518. *)
  1519. end;
  1520. end;
  1521. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1522. end
  1523. else
  1524. begin
  1525. { add the symbol's value to the base of the reference, and if the }
  1526. { reference doesn't have a base, create one }
  1527. reference_reset(tmpref);
  1528. tmpref.offset := ref2.offset;
  1529. tmpref.symbol := ref2.symbol;
  1530. tmpref.symaddr := refs_ha;
  1531. if ref2.base .number<> NR_NO then
  1532. begin
  1533. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1534. ref2.base,tmpref));
  1535. if freereg then
  1536. begin
  1537. cg.free_scratch_reg(list,ref2.base);
  1538. freereg := false;
  1539. end;
  1540. end
  1541. else
  1542. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1543. tmpref.base.number := NR_NO;
  1544. tmpref.symaddr := refs_l;
  1545. { can be folded with one of the next instructions by the }
  1546. { optimizer probably }
  1547. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1548. end
  1549. end
  1550. else if ref2.offset <> 0 Then
  1551. if ref2.base.number <> NR_NO then
  1552. a_op_const_reg_reg(list,OP_ADD,OS_32,aword(ref2.offset),ref2.base,r)
  1553. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1554. { occurs, so now only ref.offset has to be loaded }
  1555. else
  1556. a_load_const_reg(list,OS_32,ref2.offset,r)
  1557. else if ref.index.number <> NR_NO Then
  1558. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1559. else if (ref2.base.number <> NR_NO) and
  1560. (r.number <> ref2.base.number) then
  1561. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  1562. if freereg then
  1563. cg.free_scratch_reg(list,ref2.base);
  1564. end;
  1565. { ************* concatcopy ************ }
  1566. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1567. var
  1568. countreg: TRegister;
  1569. src, dst: TReference;
  1570. lab: tasmlabel;
  1571. count, count2: aword;
  1572. orgsrc, orgdst: boolean;
  1573. r:Tregister;
  1574. begin
  1575. {$ifdef extdebug}
  1576. if len > high(longint) then
  1577. internalerror(2002072704);
  1578. {$endif extdebug}
  1579. { make sure short loads are handled as optimally as possible }
  1580. if not loadref then
  1581. if (len <= 8) and
  1582. (byte(len) in [1,2,4,8]) then
  1583. begin
  1584. if len < 8 then
  1585. begin
  1586. a_load_ref_ref(list,int_cgsize(len),source,dest);
  1587. if delsource then
  1588. reference_release(list,source);
  1589. end
  1590. else
  1591. begin
  1592. r.enum:=R_F0;
  1593. a_reg_alloc(list,r);
  1594. a_loadfpu_ref_reg(list,OS_F64,source,r);
  1595. if delsource then
  1596. reference_release(list,source);
  1597. a_loadfpu_reg_ref(list,OS_F64,r,dest);
  1598. a_reg_dealloc(list,r);
  1599. end;
  1600. exit;
  1601. end;
  1602. count := len div 8;
  1603. reference_reset(src);
  1604. reference_reset(dst);
  1605. { load the address of source into src.base }
  1606. if loadref then
  1607. begin
  1608. src.base := get_scratch_reg_address(list);
  1609. a_load_ref_reg(list,OS_32,source,src.base);
  1610. orgsrc := false;
  1611. end
  1612. else if (count > 4) or
  1613. not issimpleref(source) or
  1614. ((source.index.number <> NR_NO) and
  1615. ((source.offset + longint(len)) > high(smallint))) then
  1616. begin
  1617. src.base := get_scratch_reg_address(list);
  1618. a_loadaddr_ref_reg(list,source,src.base);
  1619. orgsrc := false;
  1620. end
  1621. else
  1622. begin
  1623. src := source;
  1624. orgsrc := true;
  1625. end;
  1626. if not orgsrc and delsource then
  1627. reference_release(list,source);
  1628. { load the address of dest into dst.base }
  1629. if (count > 4) or
  1630. not issimpleref(dest) or
  1631. ((dest.index.number <> NR_NO) and
  1632. ((dest.offset + longint(len)) > high(smallint))) then
  1633. begin
  1634. dst.base := get_scratch_reg_address(list);
  1635. a_loadaddr_ref_reg(list,dest,dst.base);
  1636. orgdst := false;
  1637. end
  1638. else
  1639. begin
  1640. dst := dest;
  1641. orgdst := true;
  1642. end;
  1643. if count > 4 then
  1644. { generate a loop }
  1645. begin
  1646. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1647. { have to be set to 8. I put an Inc there so debugging may be }
  1648. { easier (should offset be different from zero here, it will be }
  1649. { easy to notice in the generated assembler }
  1650. inc(dst.offset,8);
  1651. inc(src.offset,8);
  1652. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1653. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1654. countreg := get_scratch_reg_int(list,OS_INT);
  1655. a_load_const_reg(list,OS_32,count,countreg);
  1656. { explicitely allocate R_0 since it can be used safely here }
  1657. { (for holding date that's being copied) }
  1658. r.enum:=R_F0;
  1659. a_reg_alloc(list,r);
  1660. objectlibrary.getlabel(lab);
  1661. a_label(list, lab);
  1662. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1663. r.enum:=R_F0;
  1664. list.concat(taicpu.op_reg_ref(A_LFDU,r,src));
  1665. list.concat(taicpu.op_reg_ref(A_STFDU,r,dst));
  1666. a_jmp(list,A_BC,C_NE,0,lab);
  1667. free_scratch_reg(list,countreg);
  1668. a_reg_dealloc(list,r);
  1669. len := len mod 8;
  1670. end;
  1671. count := len div 8;
  1672. if count > 0 then
  1673. { unrolled loop }
  1674. begin
  1675. r.enum:=R_F0;
  1676. a_reg_alloc(list,r);
  1677. for count2 := 1 to count do
  1678. begin
  1679. a_loadfpu_ref_reg(list,OS_F64,src,r);
  1680. a_loadfpu_reg_ref(list,OS_F64,r,dst);
  1681. inc(src.offset,8);
  1682. inc(dst.offset,8);
  1683. end;
  1684. a_reg_dealloc(list,r);
  1685. len := len mod 8;
  1686. end;
  1687. if (len and 4) <> 0 then
  1688. begin
  1689. r.enum:=R_INTREGISTER;
  1690. r.number:=NR_R0;
  1691. a_reg_alloc(list,r);
  1692. a_load_ref_reg(list,OS_32,src,r);
  1693. a_load_reg_ref(list,OS_32,r,dst);
  1694. inc(src.offset,4);
  1695. inc(dst.offset,4);
  1696. a_reg_dealloc(list,r);
  1697. end;
  1698. { copy the leftovers }
  1699. if (len and 2) <> 0 then
  1700. begin
  1701. r.enum:=R_INTREGISTER;
  1702. r.number:=NR_R0;
  1703. a_reg_alloc(list,r);
  1704. a_load_ref_reg(list,OS_16,src,r);
  1705. a_load_reg_ref(list,OS_16,r,dst);
  1706. inc(src.offset,2);
  1707. inc(dst.offset,2);
  1708. a_reg_dealloc(list,r);
  1709. end;
  1710. if (len and 1) <> 0 then
  1711. begin
  1712. r.enum:=R_INTREGISTER;
  1713. r.number:=NR_R0;
  1714. a_reg_alloc(list,r);
  1715. a_load_ref_reg(list,OS_8,src,r);
  1716. a_load_reg_ref(list,OS_8,r,dst);
  1717. a_reg_dealloc(list,r);
  1718. end;
  1719. if orgsrc then
  1720. begin
  1721. if delsource then
  1722. reference_release(list,source);
  1723. end
  1724. else
  1725. free_scratch_reg(list,src.base);
  1726. if not orgdst then
  1727. free_scratch_reg(list,dst.base);
  1728. end;
  1729. procedure tcgppc.g_copyvaluepara_openarray(list : taasmoutput;const ref:treference;elesize:integer);
  1730. var
  1731. lenref : treference;
  1732. power,len : longint;
  1733. {$ifndef __NOWINPECOFF__}
  1734. again,ok : tasmlabel;
  1735. {$endif}
  1736. r,r2,rsp:Tregister;
  1737. begin
  1738. {$warning !!!! FIX ME !!!!}
  1739. internalerror(200305231);
  1740. {!!!!
  1741. lenref:=ref;
  1742. inc(lenref.offset,4);
  1743. { get stack space }
  1744. r.enum:=R_INTREGISTER;
  1745. r.number:=NR_EDI;
  1746. rsp.enum:=R_INTREGISTER;
  1747. rsp.number:=NR_ESP;
  1748. r2.enum:=R_INTREGISTER;
  1749. rg.getexplicitregisterint(list,NR_EDI);
  1750. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1751. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1752. if (elesize<>1) then
  1753. begin
  1754. if ispowerof2(elesize, power) then
  1755. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1756. else
  1757. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1758. end;
  1759. {$ifndef __NOWINPECOFF__}
  1760. { windows guards only a few pages for stack growing, }
  1761. { so we have to access every page first }
  1762. if target_info.system=system_i386_win32 then
  1763. begin
  1764. objectlibrary.getlabel(again);
  1765. objectlibrary.getlabel(ok);
  1766. a_label(list,again);
  1767. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1768. a_jmp_cond(list,OC_B,ok);
  1769. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1770. r2.number:=NR_EAX;
  1771. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1772. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1773. a_jmp_always(list,again);
  1774. a_label(list,ok);
  1775. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1776. rg.ungetregisterint(list,r);
  1777. { now reload EDI }
  1778. rg.getexplicitregisterint(list,NR_EDI);
  1779. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1780. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1781. if (elesize<>1) then
  1782. begin
  1783. if ispowerof2(elesize, power) then
  1784. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1785. else
  1786. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1787. end;
  1788. end
  1789. else
  1790. {$endif __NOWINPECOFF__}
  1791. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1792. { align stack on 4 bytes }
  1793. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1794. { load destination }
  1795. a_load_reg_reg(list,OS_INT,OS_INT,rsp,r);
  1796. { don't destroy the registers! }
  1797. r2.number:=NR_ECX;
  1798. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1799. r2.number:=NR_ESI;
  1800. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1801. { load count }
  1802. r2.number:=NR_ECX;
  1803. a_load_ref_reg(list,OS_INT,lenref,r2);
  1804. { load source }
  1805. r2.number:=NR_ESI;
  1806. a_load_ref_reg(list,OS_INT,ref,r2);
  1807. { scheduled .... }
  1808. r2.number:=NR_ECX;
  1809. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1810. { calculate size }
  1811. len:=elesize;
  1812. opsize:=S_B;
  1813. if (len and 3)=0 then
  1814. begin
  1815. opsize:=S_L;
  1816. len:=len shr 2;
  1817. end
  1818. else
  1819. if (len and 1)=0 then
  1820. begin
  1821. opsize:=S_W;
  1822. len:=len shr 1;
  1823. end;
  1824. if ispowerof2(len, power) then
  1825. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1826. else
  1827. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1828. list.concat(Taicpu.op_none(A_REP,S_NO));
  1829. case opsize of
  1830. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1831. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1832. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1833. end;
  1834. rg.ungetregisterint(list,r);
  1835. r2.number:=NR_ESI;
  1836. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1837. r2.number:=NR_ECX;
  1838. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1839. { patch the new address }
  1840. a_load_reg_ref(list,OS_INT,rsp,ref);
  1841. !!!!}
  1842. end;
  1843. procedure tcgppc.g_overflowcheck(list: taasmoutput; const p: tnode);
  1844. var
  1845. hl : tasmlabel;
  1846. r:Tregister;
  1847. begin
  1848. if not(cs_check_overflow in aktlocalswitches) then
  1849. exit;
  1850. objectlibrary.getlabel(hl);
  1851. if not ((p.resulttype.def.deftype=pointerdef) or
  1852. ((p.resulttype.def.deftype=orddef) and
  1853. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1854. bool8bit,bool16bit,bool32bit]))) then
  1855. begin
  1856. r.enum:=R_CR7;
  1857. list.concat(taicpu.op_reg(A_MCRXR,r));
  1858. a_jmp(list,A_BC,C_OV,7,hl)
  1859. end
  1860. else
  1861. a_jmp_cond(list,OC_AE,hl);
  1862. a_call_name(list,'FPC_OVERFLOW');
  1863. a_label(list,hl);
  1864. end;
  1865. {***************** This is private property, keep out! :) *****************}
  1866. function tcgppc.issimpleref(const ref: treference): boolean;
  1867. begin
  1868. if (ref.base.number = NR_NO) and
  1869. (ref.index.number <> NR_NO) then
  1870. internalerror(200208101);
  1871. result :=
  1872. not(assigned(ref.symbol)) and
  1873. (((ref.index.number = NR_NO) and
  1874. (ref.offset >= low(smallint)) and
  1875. (ref.offset <= high(smallint))) or
  1876. ((ref.index.number <> NR_NO) and
  1877. (ref.offset = 0)));
  1878. end;
  1879. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1880. var
  1881. tmpreg: tregister;
  1882. begin
  1883. result := false;
  1884. if (ref.base.number = NR_NO) then
  1885. ref.base := ref.index;
  1886. if (ref.base.number <> NR_NO) then
  1887. begin
  1888. if (ref.index.number <> NR_NO) and
  1889. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1890. begin
  1891. result := true;
  1892. tmpreg := cg.get_scratch_reg_int(list,OS_INT);
  1893. if not assigned(ref.symbol) and
  1894. (cardinal(ref.offset-low(smallint)) <=
  1895. high(smallint)-low(smallint)) then
  1896. begin
  1897. list.concat(taicpu.op_reg_reg_const(
  1898. A_ADDI,tmpreg,ref.base,ref.offset));
  1899. ref.offset := 0;
  1900. end
  1901. else
  1902. begin
  1903. list.concat(taicpu.op_reg_reg_reg(
  1904. A_ADD,tmpreg,ref.base,ref.index));
  1905. ref.index.number := NR_NO;
  1906. end;
  1907. ref.base := tmpreg;
  1908. end
  1909. end
  1910. else
  1911. if ref.index.number <> NR_NO then
  1912. internalerror(200208102);
  1913. end;
  1914. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1915. { that's the case, we can use rlwinm to do an AND operation }
  1916. function tcgppc.get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  1917. var
  1918. temp : longint;
  1919. testbit : aword;
  1920. compare: boolean;
  1921. begin
  1922. get_rlwi_const := false;
  1923. if (a = 0) or (a = $ffffffff) then
  1924. exit;
  1925. { start with the lowest bit }
  1926. testbit := 1;
  1927. { check its value }
  1928. compare := boolean(a and testbit);
  1929. { find out how long the run of bits with this value is }
  1930. { (it's impossible that all bits are 1 or 0, because in that case }
  1931. { this function wouldn't have been called) }
  1932. l1 := 31;
  1933. while (((a and testbit) <> 0) = compare) do
  1934. begin
  1935. testbit := testbit shl 1;
  1936. dec(l1);
  1937. end;
  1938. { check the length of the run of bits that comes next }
  1939. compare := not compare;
  1940. l2 := l1;
  1941. while (((a and testbit) <> 0) = compare) and
  1942. (l2 >= 0) do
  1943. begin
  1944. testbit := testbit shl 1;
  1945. dec(l2);
  1946. end;
  1947. { and finally the check whether the rest of the bits all have the }
  1948. { same value }
  1949. compare := not compare;
  1950. temp := l2;
  1951. if temp >= 0 then
  1952. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1953. exit;
  1954. { we have done "not(not(compare))", so compare is back to its }
  1955. { initial value. If the lowest bit was 0, a is of the form }
  1956. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1957. { because l2 now contains the position of the last zero of the }
  1958. { first run instead of that of the first 1) so switch l1 and l2 }
  1959. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1960. if not compare then
  1961. begin
  1962. temp := l1;
  1963. l1 := l2+1;
  1964. l2 := temp;
  1965. end
  1966. else
  1967. { otherwise, l1 currently contains the position of the last }
  1968. { zero instead of that of the first 1 of the second run -> +1 }
  1969. inc(l1);
  1970. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1971. l1 := l1 and 31;
  1972. l2 := l2 and 31;
  1973. get_rlwi_const := true;
  1974. end;
  1975. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1976. ref: treference);
  1977. var
  1978. tmpreg: tregister;
  1979. tmpref: treference;
  1980. r : Tregister;
  1981. begin
  1982. tmpreg.number := NR_NO;
  1983. if assigned(ref.symbol) or
  1984. (cardinal(ref.offset-low(smallint)) >
  1985. high(smallint)-low(smallint)) then
  1986. begin
  1987. if target_info.system = system_powerpc_macos then
  1988. begin
  1989. if ref.base.number <> NR_NO then
  1990. begin
  1991. if macos_direct_globals then
  1992. begin
  1993. {Generates
  1994. add tempreg, ref.base, RTOC
  1995. op reg, symbolplusoffset, tempreg
  1996. which is eqvivalent to the more comprehensive
  1997. addi tempreg, RTOC, symbolplusoffset
  1998. add tempreg, ref.base, tempreg
  1999. op reg, tempreg
  2000. but which saves one instruction.}
  2001. tmpreg := get_scratch_reg_address(list);
  2002. reference_reset(tmpref);
  2003. tmpref.symbol := ref.symbol;
  2004. tmpref.offset := ref.offset;
  2005. tmpref.symaddr := refs_full;
  2006. tmpref.base:= tmpreg;
  2007. r.enum:=R_INTREGISTER;
  2008. r.number:=NR_RTOC;
  2009. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  2010. ref.base,r));
  2011. list.concat(taicpu.op_reg_ref(op,reg,tmpref));
  2012. end
  2013. else
  2014. begin
  2015. tmpreg := get_scratch_reg_address(list);
  2016. reference_reset(tmpref);
  2017. tmpref.symbol := ref.symbol;
  2018. tmpref.offset := ref.offset;
  2019. tmpref.symaddr := refs_full;
  2020. tmpref.base.enum:= R_INTREGISTER;
  2021. tmpref.base.number:= NR_RTOC;
  2022. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  2023. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  2024. ref.base,tmpreg));
  2025. reference_reset(tmpref);
  2026. tmpref.offset := 0;
  2027. tmpref.symaddr := refs_full;
  2028. tmpref.base:= tmpreg;
  2029. list.concat(taicpu.op_reg_ref(op,reg,tmpref));
  2030. end;
  2031. //list.concat(tai_comment.create(strpnew('**** a_load_store 1')));
  2032. end
  2033. else
  2034. begin
  2035. if macos_direct_globals then
  2036. begin
  2037. reference_reset(tmpref);
  2038. tmpref.symbol := ref.symbol;
  2039. tmpref.offset := ref.offset;
  2040. tmpref.symaddr := refs_full;
  2041. tmpref.base.enum:= R_INTREGISTER;
  2042. tmpref.base.number:= NR_RTOC;
  2043. list.concat(taicpu.op_reg_ref(op,reg,tmpref));
  2044. end
  2045. else
  2046. begin
  2047. tmpreg := get_scratch_reg_address(list);
  2048. reference_reset(tmpref);
  2049. tmpref.symbol := ref.symbol;
  2050. tmpref.offset := ref.offset;
  2051. tmpref.symaddr := refs_full;
  2052. tmpref.base.enum:= R_INTREGISTER;
  2053. tmpref.base.number:= NR_RTOC;
  2054. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  2055. reference_reset(tmpref);
  2056. tmpref.offset := 0;
  2057. tmpref.symaddr := refs_full;
  2058. tmpref.base:= tmpreg;
  2059. list.concat(taicpu.op_reg_ref(op,reg,tmpref));
  2060. end;
  2061. //list.concat(tai_comment.create(strpnew('*** a_load_store 2')));
  2062. end;
  2063. end
  2064. else
  2065. begin
  2066. tmpreg := get_scratch_reg_address(list);
  2067. reference_reset(tmpref);
  2068. tmpref.symbol := ref.symbol;
  2069. tmpref.offset := ref.offset;
  2070. tmpref.symaddr := refs_ha;
  2071. if ref.base.number <> NR_NO then
  2072. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  2073. ref.base,tmpref))
  2074. else
  2075. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  2076. ref.base := tmpreg;
  2077. ref.symaddr := refs_l;
  2078. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2079. end
  2080. end
  2081. else
  2082. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2083. if (tmpreg.number <> NR_NO) then
  2084. free_scratch_reg(list,tmpreg);
  2085. end;
  2086. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  2087. crval: longint; l: tasmlabel);
  2088. var
  2089. p: taicpu;
  2090. begin
  2091. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name));
  2092. if op <> A_B then
  2093. create_cond_norm(c,crval,p.condition);
  2094. p.is_jmp := true;
  2095. list.concat(p)
  2096. end;
  2097. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  2098. begin
  2099. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  2100. end;
  2101. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  2102. begin
  2103. a_op64_const_reg_reg(list,op,value,reg,reg);
  2104. end;
  2105. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  2106. begin
  2107. case op of
  2108. OP_AND,OP_OR,OP_XOR:
  2109. begin
  2110. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2111. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2112. end;
  2113. OP_ADD:
  2114. begin
  2115. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2116. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2117. end;
  2118. OP_SUB:
  2119. begin
  2120. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2121. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2122. end;
  2123. else
  2124. internalerror(2002072801);
  2125. end;
  2126. end;
  2127. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  2128. const
  2129. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2130. (A_SUBIC,A_SUBC,A_ADDME));
  2131. var
  2132. tmpreg: tregister;
  2133. tmpreg64: tregister64;
  2134. newop: TOpCG;
  2135. issub: boolean;
  2136. begin
  2137. case op of
  2138. OP_AND,OP_OR,OP_XOR:
  2139. begin
  2140. cg.a_op_const_reg_reg(list,op,OS_32,cardinal(value),regsrc.reglo,regdst.reglo);
  2141. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  2142. regdst.reghi);
  2143. end;
  2144. OP_ADD, OP_SUB:
  2145. begin
  2146. if (int64(value) < 0) then
  2147. begin
  2148. if op = OP_ADD then
  2149. op := OP_SUB
  2150. else
  2151. op := OP_ADD;
  2152. int64(value) := -int64(value);
  2153. end;
  2154. if (longint(value) <> 0) then
  2155. begin
  2156. issub := op = OP_SUB;
  2157. if (int64(value) > 0) and
  2158. (int64(value)-ord(issub) <= 32767) then
  2159. begin
  2160. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2161. regdst.reglo,regsrc.reglo,longint(value)));
  2162. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2163. regdst.reghi,regsrc.reghi));
  2164. end
  2165. else if ((value shr 32) = 0) then
  2166. begin
  2167. tmpreg := cg.get_scratch_reg_int(list,OS_32);
  2168. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2169. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2170. regdst.reglo,regsrc.reglo,tmpreg));
  2171. cg.free_scratch_reg(list,tmpreg);
  2172. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2173. regdst.reghi,regsrc.reghi));
  2174. end
  2175. else
  2176. begin
  2177. tmpreg64.reglo := cg.get_scratch_reg_int(list,OS_INT);
  2178. tmpreg64.reghi := cg.get_scratch_reg_int(list,OS_INT);
  2179. a_load64_const_reg(list,value,tmpreg64);
  2180. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2181. cg.free_scratch_reg(list,tmpreg64.reghi);
  2182. cg.free_scratch_reg(list,tmpreg64.reglo);
  2183. end
  2184. end
  2185. else
  2186. begin
  2187. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2188. cg.a_op_const_reg_reg(list,op,OS_32,value shr 32,regsrc.reghi,
  2189. regdst.reghi);
  2190. end;
  2191. end;
  2192. else
  2193. internalerror(2002072802);
  2194. end;
  2195. end;
  2196. begin
  2197. cg := tcgppc.create;
  2198. cg64 :=tcg64fppc.create;
  2199. end.
  2200. {
  2201. $Log$
  2202. Revision 1.98 2003-05-28 23:58:18 jonas
  2203. * added missing initialization of rg.usedint{in,by}proc
  2204. * ppc now also saves/restores used fpu registers
  2205. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  2206. i386
  2207. Revision 1.97 2003/05/28 23:18:31 florian
  2208. * started to fix and clean up the sparc port
  2209. Revision 1.96 2003/05/24 11:59:42 jonas
  2210. * fixed integer typeconversion problems
  2211. Revision 1.95 2003/05/23 18:51:26 jonas
  2212. * fixed support for nested procedures and more parameters than those
  2213. which fit in registers (untested/probably not working: calling a
  2214. nested procedure from a deeper nested procedure)
  2215. Revision 1.94 2003/05/20 23:54:00 florian
  2216. + basic darwin support added
  2217. Revision 1.93 2003/05/15 22:14:42 florian
  2218. * fixed last commit, changing lastsaveintreg to r31 caused some strange problems
  2219. Revision 1.92 2003/05/15 21:37:00 florian
  2220. * sysv entry code saves r13 now as well
  2221. Revision 1.91 2003/05/15 19:39:09 florian
  2222. * fixed ppc compiler which was broken by Peter's changes
  2223. Revision 1.90 2003/05/12 18:43:50 jonas
  2224. * fixed g_concatcopy
  2225. Revision 1.89 2003/05/11 20:59:23 jonas
  2226. * fixed bug with large offsets in entrycode
  2227. Revision 1.88 2003/05/11 11:45:08 jonas
  2228. * fixed shifts
  2229. Revision 1.87 2003/05/11 11:07:33 jonas
  2230. * fixed optimizations in a_op_const_reg_reg()
  2231. Revision 1.86 2003/04/27 11:21:36 peter
  2232. * aktprocdef renamed to current_procdef
  2233. * procinfo renamed to current_procinfo
  2234. * procinfo will now be stored in current_module so it can be
  2235. cleaned up properly
  2236. * gen_main_procsym changed to create_main_proc and release_main_proc
  2237. to also generate a tprocinfo structure
  2238. * fixed unit implicit initfinal
  2239. Revision 1.85 2003/04/26 22:56:11 jonas
  2240. * fix to a_op64_const_reg_reg
  2241. Revision 1.84 2003/04/26 16:08:41 jonas
  2242. * fixed g_flags2reg
  2243. Revision 1.83 2003/04/26 15:25:29 florian
  2244. * fixed cmp_reg_reg_reg, cmp operands were emitted in the wrong order
  2245. Revision 1.82 2003/04/25 20:55:34 florian
  2246. * stack frame calculations are now completly done using the code generator
  2247. routines instead of generating directly assembler so also large stack frames
  2248. are handle properly
  2249. Revision 1.81 2003/04/24 11:24:00 florian
  2250. * fixed several issues with nested procedures
  2251. Revision 1.80 2003/04/23 22:18:01 peter
  2252. * fixes to get rtl compiled
  2253. Revision 1.79 2003/04/23 12:35:35 florian
  2254. * fixed several issues with powerpc
  2255. + applied a patch from Jonas for nested function calls (PowerPC only)
  2256. * ...
  2257. Revision 1.78 2003/04/16 09:26:55 jonas
  2258. * assembler procedures now again get a stackframe if they have local
  2259. variables. No space is reserved for a function result however.
  2260. Also, the register parameters aren't automatically saved on the stack
  2261. anymore in assembler procedures.
  2262. Revision 1.77 2003/04/06 16:39:11 jonas
  2263. * don't generate entry/exit code for assembler procedures
  2264. Revision 1.76 2003/03/22 18:01:13 jonas
  2265. * fixed linux entry/exit code generation
  2266. Revision 1.75 2003/03/19 14:26:26 jonas
  2267. * fixed R_TOC bugs introduced by new register allocator conversion
  2268. Revision 1.74 2003/03/13 22:57:45 olle
  2269. * change in a_loadaddr_ref_reg
  2270. Revision 1.73 2003/03/12 22:43:38 jonas
  2271. * more powerpc and generic fixes related to the new register allocator
  2272. Revision 1.72 2003/03/11 21:46:24 jonas
  2273. * lots of new regallocator fixes, both in generic and ppc-specific code
  2274. (ppc compiler still can't compile the linux system unit though)
  2275. Revision 1.71 2003/02/19 22:00:16 daniel
  2276. * Code generator converted to new register notation
  2277. - Horribily outdated todo.txt removed
  2278. Revision 1.70 2003/01/13 17:17:50 olle
  2279. * changed global var access, TOC now contain pointers to globals
  2280. * fixed handling of function pointers
  2281. Revision 1.69 2003/01/09 22:00:53 florian
  2282. * fixed some PowerPC issues
  2283. Revision 1.68 2003/01/08 18:43:58 daniel
  2284. * Tregister changed into a record
  2285. Revision 1.67 2002/12/15 19:22:01 florian
  2286. * fixed some crashes and a rte 201
  2287. Revision 1.66 2002/11/28 10:55:16 olle
  2288. * macos: changing code gen for references to globals
  2289. Revision 1.65 2002/11/07 15:50:23 jonas
  2290. * fixed bctr(l) problems
  2291. Revision 1.64 2002/11/04 18:24:19 olle
  2292. * macos: globals are located in TOC and relative r2, instead of absolute
  2293. Revision 1.63 2002/10/28 22:24:28 olle
  2294. * macos entry/exit: only used registers are saved
  2295. - macos entry/exit: stackptr not saved in r31 anymore
  2296. * macos entry/exit: misc fixes
  2297. Revision 1.62 2002/10/19 23:51:48 olle
  2298. * macos stack frame size computing updated
  2299. + macos epilogue: control register now restored
  2300. * macos prologue and epilogue: fp reg now saved and restored
  2301. Revision 1.61 2002/10/19 12:50:36 olle
  2302. * reorganized prologue and epilogue routines
  2303. Revision 1.60 2002/10/02 21:49:51 florian
  2304. * all A_BL instructions replaced by calls to a_call_name
  2305. Revision 1.59 2002/10/02 13:24:58 jonas
  2306. * changed a_call_* so that no superfluous code is generated anymore
  2307. Revision 1.58 2002/09/17 18:54:06 jonas
  2308. * a_load_reg_reg() now has two size parameters: source and dest. This
  2309. allows some optimizations on architectures that don't encode the
  2310. register size in the register name.
  2311. Revision 1.57 2002/09/10 21:22:25 jonas
  2312. + added some internal errors
  2313. * fixed bug in sysv exit code
  2314. Revision 1.56 2002/09/08 20:11:56 jonas
  2315. * fixed TOpCmp2AsmCond array (some unsigned equivalents were wrong)
  2316. Revision 1.55 2002/09/08 13:03:26 jonas
  2317. * several large offset-related fixes
  2318. Revision 1.54 2002/09/07 17:54:58 florian
  2319. * first part of PowerPC fixes
  2320. Revision 1.53 2002/09/07 15:25:14 peter
  2321. * old logs removed and tabs fixed
  2322. Revision 1.52 2002/09/02 10:14:51 jonas
  2323. + a_call_reg()
  2324. * small fix in a_call_ref()
  2325. Revision 1.51 2002/09/02 06:09:02 jonas
  2326. * fixed range error
  2327. Revision 1.50 2002/09/01 21:04:49 florian
  2328. * several powerpc related stuff fixed
  2329. Revision 1.49 2002/09/01 12:09:27 peter
  2330. + a_call_reg, a_call_loc added
  2331. * removed exprasmlist references
  2332. Revision 1.48 2002/08/31 21:38:02 jonas
  2333. * fixed a_call_ref (it should load ctr, not lr)
  2334. Revision 1.47 2002/08/31 21:30:45 florian
  2335. * fixed several problems caused by Jonas' commit :)
  2336. Revision 1.46 2002/08/31 19:25:50 jonas
  2337. + implemented a_call_ref()
  2338. Revision 1.45 2002/08/18 22:16:14 florian
  2339. + the ppc gas assembler writer adds now registers aliases
  2340. to the assembler file
  2341. Revision 1.44 2002/08/17 18:23:53 florian
  2342. * some assembler writer bugs fixed
  2343. Revision 1.43 2002/08/17 09:23:49 florian
  2344. * first part of procinfo rewrite
  2345. Revision 1.42 2002/08/16 14:24:59 carl
  2346. * issameref() to test if two references are the same (then emit no opcodes)
  2347. + ret_in_reg to replace ret_in_acc
  2348. (fix some register allocation bugs at the same time)
  2349. + save_std_register now has an extra parameter which is the
  2350. usedinproc registers
  2351. Revision 1.41 2002/08/15 08:13:54 carl
  2352. - a_load_sym_ofs_reg removed
  2353. * loadvmt now calls loadaddr_ref_reg instead
  2354. Revision 1.40 2002/08/11 14:32:32 peter
  2355. * renamed current_library to objectlibrary
  2356. Revision 1.39 2002/08/11 13:24:18 peter
  2357. * saving of asmsymbols in ppu supported
  2358. * asmsymbollist global is removed and moved into a new class
  2359. tasmlibrarydata that will hold the info of a .a file which
  2360. corresponds with a single module. Added librarydata to tmodule
  2361. to keep the library info stored for the module. In the future the
  2362. objectfiles will also be stored to the tasmlibrarydata class
  2363. * all getlabel/newasmsymbol and friends are moved to the new class
  2364. Revision 1.38 2002/08/11 11:39:31 jonas
  2365. + powerpc-specific genlinearlist
  2366. Revision 1.37 2002/08/10 17:15:31 jonas
  2367. * various fixes and optimizations
  2368. Revision 1.36 2002/08/06 20:55:23 florian
  2369. * first part of ppc calling conventions fix
  2370. Revision 1.35 2002/08/06 07:12:05 jonas
  2371. * fixed bug in g_flags2reg()
  2372. * and yet more constant operation fixes :)
  2373. Revision 1.34 2002/08/05 08:58:53 jonas
  2374. * fixed compilation problems
  2375. Revision 1.33 2002/08/04 12:57:55 jonas
  2376. * more misc. fixes, mostly constant-related
  2377. }