cgcpu.pas 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  1. {
  2. Copyright (c) 1998-2002 by the FPC team
  3. This unit implements the code generator for the 680x0
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,cgobj,globtype,
  22. aasmbase,aasmtai,aasmdata,aasmcpu,
  23. cpubase,cpuinfo,
  24. parabase,cpupara,
  25. node,symconst,symtype,symdef,
  26. cgutils,cg64f32;
  27. type
  28. tcg68k = class(tcg)
  29. procedure init_register_allocators;override;
  30. procedure done_register_allocators;override;
  31. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  32. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  33. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  34. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  35. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  36. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  37. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);override;
  38. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  39. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  40. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  41. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  42. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  43. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  44. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);override;
  45. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  46. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  47. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  48. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  49. procedure a_loadfpu_reg_cgpara(list : TAsmList; size : tcgsize;const reg : tregister;const cgpara : TCGPara); override;
  50. procedure a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);override;
  51. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister); override;
  52. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  53. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  54. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); override;
  55. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  56. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister; l : tasmlabel);override;
  57. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel); override;
  58. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  59. procedure a_jmp_name(list : TAsmList;const s : string); override;
  60. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  61. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  62. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  63. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  64. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  65. { generates overflow checking code for a node }
  66. procedure g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef); override;
  67. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  68. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  69. procedure g_save_registers(list:TAsmList);override;
  70. procedure g_restore_registers(list:TAsmList);override;
  71. procedure g_adjust_self_value(list:TAsmList;procdef:tprocdef;ioffset:tcgint);override;
  72. { # Sign or zero extend the register to a full 32-bit value.
  73. The new value is left in the same register.
  74. }
  75. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  76. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  77. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  78. function fixref(list: TAsmList; var ref: treference; fullyresolve: boolean): boolean;
  79. function force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  80. procedure move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  81. { optimize mul with const to a sequence of shifts and subs/adds, mainly for the '000 to '030 }
  82. function optimize_const_mul_to_shift_sub_add(list: TAsmList; maxops: longint; a: tcgint; size: tcgsize; reg: TRegister): boolean;
  83. protected
  84. procedure call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  85. procedure call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  86. procedure check_register_size(size:tcgsize;reg:tregister);
  87. end;
  88. tcg64f68k = class(tcg64f32)
  89. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG; size: tcgsize; regsrc,regdst : tregister64);override;
  90. procedure a_op64_const_reg(list : TAsmList;op:TOpCG; size: tcgsize; value : int64;regdst : tregister64);override;
  91. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;
  92. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const ref : treference);override;
  93. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference); override;
  94. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64); override;
  95. end;
  96. { This function returns true if the reference+offset is valid.
  97. Otherwise extra code must be generated to solve the reference.
  98. On the m68k, this verifies that the reference is valid
  99. (e.g : if index register is used, then the max displacement
  100. is 256 bytes, if only base is used, then max displacement
  101. is 32K
  102. }
  103. function isvalidrefoffset(const ref: treference): boolean;
  104. function isvalidreference(const ref: treference): boolean;
  105. procedure create_codegen;
  106. implementation
  107. uses
  108. globals,verbose,systems,cutils,
  109. symsym,symtable,defutil,paramgr,procinfo,
  110. rgobj,tgobj,rgcpu,fmodule;
  111. { Range check must be disabled explicitly as conversions between signed and unsigned
  112. 32-bit values are done without explicit typecasts }
  113. {$R-}
  114. const
  115. { opcode table lookup }
  116. topcg2tasmop: Array[topcg] of tasmop =
  117. (
  118. A_NONE,
  119. A_MOVE,
  120. A_ADD,
  121. A_AND,
  122. A_DIVU,
  123. A_DIVS,
  124. A_MULS,
  125. A_MULU,
  126. A_NEG,
  127. A_NOT,
  128. A_OR,
  129. A_ASR,
  130. A_LSL,
  131. A_LSR,
  132. A_SUB,
  133. A_EOR,
  134. A_ROL,
  135. A_ROR
  136. );
  137. { opcode with extend bits table lookup, used by 64bit cg }
  138. topcg2tasmopx: Array[topcg] of tasmop =
  139. (
  140. A_NONE,
  141. A_NONE,
  142. A_ADDX,
  143. A_NONE,
  144. A_NONE,
  145. A_NONE,
  146. A_NONE,
  147. A_NONE,
  148. A_NEGX,
  149. A_NONE,
  150. A_NONE,
  151. A_NONE,
  152. A_NONE,
  153. A_NONE,
  154. A_SUBX,
  155. A_NONE,
  156. A_NONE,
  157. A_NONE
  158. );
  159. TOpCmp2AsmCond: Array[topcmp] of TAsmCond =
  160. (
  161. C_NONE,
  162. C_EQ,
  163. C_GT,
  164. C_LT,
  165. C_GE,
  166. C_LE,
  167. C_NE,
  168. C_LS,
  169. C_CS,
  170. C_CC,
  171. C_HI
  172. );
  173. function isvalidreference(const ref: treference): boolean;
  174. begin
  175. isvalidreference:=isvalidrefoffset(ref) and
  176. { don't try to generate addressing with symbol and base reg and offset
  177. it might fail in linking stage if the symbol is more than 32k away (KB) }
  178. not (assigned(ref.symbol) and (ref.base <> NR_NO) and (ref.offset <> 0)) and
  179. { coldfire and 68000 cannot handle non-addressregs as bases }
  180. not ((current_settings.cputype in cpu_coldfire+[cpu_mc68000]) and
  181. not isaddressregister(ref.base));
  182. end;
  183. function isvalidrefoffset(const ref: treference): boolean;
  184. begin
  185. isvalidrefoffset := true;
  186. if ref.index <> NR_NO then
  187. begin
  188. // if ref.base <> NR_NO then
  189. // internalerror(2002081401);
  190. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  191. isvalidrefoffset := false
  192. end
  193. else
  194. begin
  195. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  196. isvalidrefoffset := false;
  197. end;
  198. end;
  199. {****************************************************************************}
  200. { TCG68K }
  201. {****************************************************************************}
  202. function use_push(const cgpara:tcgpara):boolean;
  203. begin
  204. result:=(not paramanager.use_fixed_stack) and
  205. assigned(cgpara.location) and
  206. (cgpara.location^.loc=LOC_REFERENCE) and
  207. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  208. end;
  209. procedure tcg68k.init_register_allocators;
  210. var
  211. reg: TSuperRegister;
  212. address_regs: array of TSuperRegister;
  213. begin
  214. inherited init_register_allocators;
  215. address_regs:=nil;
  216. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  217. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7],
  218. first_int_imreg,[]);
  219. { set up the array of address registers to use }
  220. for reg:=RS_A0 to RS_A6 do
  221. begin
  222. { don't hardwire the frame pointer register, because it can vary between target OS }
  223. if (assigned(current_procinfo) and (current_procinfo.framepointer = NR_FRAME_POINTER_REG)
  224. and (reg = RS_FRAME_POINTER_REG))
  225. or ((reg = RS_PIC_OFFSET_REG) and (tf_static_reg_based in target_info.flags)) then
  226. continue;
  227. setlength(address_regs,length(address_regs)+1);
  228. address_regs[length(address_regs)-1]:=reg;
  229. end;
  230. rg[R_ADDRESSREGISTER]:=trgcpu.create(R_ADDRESSREGISTER,R_SUBWHOLE,
  231. address_regs, first_addr_imreg, []);
  232. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  233. [RS_FP0,RS_FP1,RS_FP2,RS_FP3,RS_FP4,RS_FP5,RS_FP6,RS_FP7],
  234. first_fpu_imreg,[]);
  235. end;
  236. procedure tcg68k.done_register_allocators;
  237. begin
  238. rg[R_INTREGISTER].free;
  239. rg[R_FPUREGISTER].free;
  240. rg[R_ADDRESSREGISTER].free;
  241. inherited done_register_allocators;
  242. end;
  243. procedure tcg68k.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  244. var
  245. pushsize : tcgsize;
  246. ref : treference;
  247. begin
  248. { it's probably necessary to port this from x86 later, or provide an m68k solution (KB) }
  249. { TODO: FIX ME! check_register_size()}
  250. // check_register_size(size,r);
  251. if use_push(cgpara) then
  252. begin
  253. cgpara.check_simple_location;
  254. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  255. pushsize:=cgpara.location^.size
  256. else
  257. pushsize:=int_cgsize(cgpara.alignment);
  258. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, ctempposinvalid ,cgpara.alignment, []);
  259. ref.direction := dir_dec;
  260. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize),ref));
  261. end
  262. else
  263. inherited a_load_reg_cgpara(list,size,r,cgpara);
  264. end;
  265. procedure tcg68k.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  266. var
  267. pushsize : tcgsize;
  268. ref : treference;
  269. begin
  270. if use_push(cgpara) then
  271. begin
  272. cgpara.check_simple_location;
  273. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  274. pushsize:=cgpara.location^.size
  275. else
  276. pushsize:=int_cgsize(cgpara.alignment);
  277. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, ctempposinvalid, cgpara.alignment, []);
  278. ref.direction := dir_dec;
  279. a_load_const_ref(list, pushsize, a, ref);
  280. end
  281. else
  282. inherited a_load_const_cgpara(list,size,a,cgpara);
  283. end;
  284. procedure tcg68k.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  285. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  286. var
  287. pushsize : tcgsize;
  288. tmpreg : tregister;
  289. href : treference;
  290. ref : treference;
  291. begin
  292. if not assigned(paraloc) then
  293. exit;
  294. if (paraloc^.loc<>LOC_REFERENCE) or
  295. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  296. (tcgsize2size[paraloc^.size]>sizeof(tcgint)) then
  297. internalerror(200501162);
  298. { Pushes are needed in reverse order, add the size of the
  299. current location to the offset where to load from. This
  300. prevents wrong calculations for the last location when
  301. the size is not a power of 2 }
  302. if assigned(paraloc^.next) then
  303. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  304. { Push the data starting at ofs }
  305. href:=r;
  306. inc(href.offset,ofs);
  307. fixref(list,href,false);
  308. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  309. pushsize:=paraloc^.size
  310. else
  311. pushsize:=int_cgsize(cgpara.alignment);
  312. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, ctempposinvalid, tcgsize2size[pushsize], []);
  313. ref.direction := dir_dec;
  314. a_load_ref_ref(list,int_cgsize(tcgsize2size[paraloc^.size]),pushsize,href,ref);
  315. end;
  316. var
  317. len : tcgint;
  318. ofs : tcgint;
  319. href : treference;
  320. begin
  321. { cgpara.size=OS_NO requires a copy on the stack }
  322. if use_push(cgpara) then
  323. begin
  324. { Record copy? }
  325. if (cgpara.size in [OS_NO,OS_F64]) or (size in [OS_NO,OS_F64]) then
  326. begin
  327. //list.concat(tai_comment.create(strpnew('a_load_ref_cgpara: g_concatcopy')));
  328. cgpara.check_simple_location;
  329. len:=align(cgpara.intsize,cgpara.alignment);
  330. g_stackpointer_alloc(list,len);
  331. ofs:=0;
  332. if (cgpara.intsize<cgpara.alignment) then
  333. ofs:=cgpara.alignment-cgpara.intsize;
  334. reference_reset_base(href,NR_STACK_POINTER_REG,ofs,ctempposinvalid,cgpara.alignment,[]);
  335. g_concatcopy(list,r,href,cgpara.intsize);
  336. end
  337. else
  338. begin
  339. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  340. internalerror(200501161);
  341. { We need to push the data in reverse order,
  342. therefore we use a recursive algorithm }
  343. pushdata(cgpara.location,0);
  344. end
  345. end
  346. else
  347. inherited a_load_ref_cgpara(list,size,r,cgpara);
  348. end;
  349. procedure tcg68k.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  350. var
  351. tmpref : treference;
  352. begin
  353. { 68k always passes arguments on the stack }
  354. if use_push(cgpara) then
  355. begin
  356. //list.concat(tai_comment.create(strpnew('a_loadaddr_ref_cgpara: PEA')));
  357. cgpara.check_simple_location;
  358. tmpref:=r;
  359. fixref(list,tmpref,false);
  360. list.concat(taicpu.op_ref(A_PEA,S_NO,tmpref));
  361. end
  362. else
  363. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  364. end;
  365. function tcg68k.fixref(list: TAsmList; var ref: treference; fullyresolve: boolean): boolean;
  366. var
  367. hreg : tregister;
  368. href : treference;
  369. instr : taicpu;
  370. begin
  371. result:=false;
  372. hreg:=NR_NO;
  373. { NOTE: we don't have to fixup scaling in this function, because the memnode
  374. won't generate scaling on CPUs which don't support it }
  375. if (tf_static_reg_based in target_info.flags) and assigned(ref.symbol) and (ref.base=NR_NO) then
  376. fullyresolve:=true;
  377. { first, deal with the symbol, if we have an index or base register.
  378. in theory, the '020+ could deal with these, but it's better to avoid
  379. long displacements on most members of the 68k family anyway }
  380. if assigned(ref.symbol) and ((ref.base<>NR_NO) or (ref.index<>NR_NO)) then
  381. begin
  382. //list.concat(tai_comment.create(strpnew('fixref: symbol with base or index')));
  383. hreg:=getaddressregister(list);
  384. reference_reset_symbol(href,ref.symbol,ref.offset,ref.alignment,ref.volatility);
  385. if (tf_static_reg_based in target_info.flags) and (ref.base=NR_NO) then
  386. begin
  387. if ref.symbol.typ in [AT_DATA,AT_DATA_FORCEINDIRECT,AT_DATA_NOINDIRECT] then
  388. href.base:=NR_PIC_OFFSET_REG
  389. else
  390. href.base:=NR_PC;
  391. end;
  392. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  393. ref.offset:=0;
  394. ref.symbol:=nil;
  395. { if we have unused base or index, try to use it, otherwise fold the existing base,
  396. also handle the case where the base might be a data register. }
  397. if ref.base=NR_NO then
  398. ref.base:=hreg
  399. else
  400. if (ref.index=NR_NO) and not isintregister(ref.base) then
  401. ref.index:=hreg
  402. else
  403. begin
  404. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.base,hreg));
  405. ref.base:=hreg;
  406. end;
  407. { at this point we have base + (optional) index * scale }
  408. end;
  409. { deal with the case if our base is a dataregister }
  410. if (ref.base<>NR_NO) and not isaddressregister(ref.base) then
  411. begin
  412. hreg:=getaddressregister(list);
  413. if isaddressregister(ref.index) and (ref.scalefactor < 2) then
  414. begin
  415. //list.concat(tai_comment.create(strpnew('fixref: base is dX, resolving with reverse regs')));
  416. reference_reset_base(href,ref.index,0,ref.temppos,ref.alignment,ref.volatility);
  417. href.index:=ref.base;
  418. { we can fold in an 8 bit offset "for free" }
  419. if isvalue8bit(ref.offset) then
  420. begin
  421. href.offset:=ref.offset;
  422. ref.offset:=0;
  423. end;
  424. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  425. ref.base:=hreg;
  426. ref.index:=NR_NO;
  427. result:=true;
  428. end
  429. else
  430. begin
  431. //list.concat(tai_comment.create(strpnew('fixref: base is dX, can''t resolve with reverse regs')));
  432. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  433. add_move_instruction(instr);
  434. list.concat(instr);
  435. ref.base:=hreg;
  436. result:=true;
  437. end;
  438. end;
  439. { deal with large offsets on non-020+ }
  440. if not (CPUM68K_HAS_BASEDISP in cpu_capabilities[current_settings.cputype]) then
  441. begin
  442. if ((ref.index<>NR_NO) and not isvalue8bit(ref.offset)) or
  443. ((ref.base<>NR_NO) and not isvalue16bit(ref.offset)) then
  444. begin
  445. //list.concat(tai_comment.create(strpnew('fixref: handling large offsets')));
  446. { if we have a temp register from above, we can just add to it }
  447. if hreg=NR_NO then
  448. hreg:=getaddressregister(list);
  449. if isvalue16bit(ref.offset) then
  450. begin
  451. reference_reset_base(href,ref.base,ref.offset,ref.temppos,ref.alignment,ref.volatility);
  452. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  453. end
  454. else
  455. begin
  456. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  457. add_move_instruction(instr);
  458. list.concat(instr);
  459. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  460. end;
  461. ref.offset:=0;
  462. ref.base:=hreg;
  463. result:=true;
  464. end;
  465. end;
  466. { fully resolve the reference to an address register, if we're told to do so
  467. and there's a reason to do so }
  468. if fullyresolve and
  469. ((ref.index<>NR_NO) or assigned(ref.symbol) or (ref.offset<>0)) then
  470. begin
  471. //list.concat(tai_comment.create(strpnew('fixref: fully resolve to register')));
  472. if hreg=NR_NO then
  473. hreg:=getaddressregister(list);
  474. if (tf_static_reg_based in target_info.flags) and (ref.base=NR_NO) then
  475. begin
  476. if ref.symbol.typ in [AT_DATA,AT_DATA_FORCEINDIRECT,AT_DATA_NOINDIRECT] then
  477. ref.base:=NR_PIC_OFFSET_REG
  478. else
  479. ref.base:=NR_PC;
  480. end;
  481. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  482. ref.base:=hreg;
  483. ref.index:=NR_NO;
  484. ref.scalefactor:=1;
  485. ref.symbol:=nil;
  486. ref.offset:=0;
  487. result:=true;
  488. end;
  489. end;
  490. procedure tcg68k.call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  491. var
  492. paraloc1,paraloc2: tcgpara;
  493. pd : tprocdef;
  494. begin
  495. pd:=search_system_proc(name);
  496. paraloc1.init;
  497. paraloc2.init;
  498. paramanager.getcgtempparaloc(list,pd,1,paraloc1);
  499. paramanager.getcgtempparaloc(list,pd,2,paraloc2);
  500. a_load_const_cgpara(list,size,a,paraloc2);
  501. a_load_reg_cgpara(list,OS_32,reg,paraloc1);
  502. paramanager.freecgpara(list,paraloc2);
  503. paramanager.freecgpara(list,paraloc1);
  504. g_call(list,name);
  505. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  506. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg);
  507. paraloc2.done;
  508. paraloc1.done;
  509. end;
  510. procedure tcg68k.call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  511. var
  512. paraloc1,paraloc2: tcgpara;
  513. pd : tprocdef;
  514. begin
  515. pd:=search_system_proc(name);
  516. paraloc1.init;
  517. paraloc2.init;
  518. paramanager.getcgtempparaloc(list,pd,1,paraloc1);
  519. paramanager.getcgtempparaloc(list,pd,2,paraloc2);
  520. a_load_reg_cgpara(list,OS_32,reg1,paraloc2);
  521. a_load_reg_cgpara(list,OS_32,reg2,paraloc1);
  522. paramanager.freecgpara(list,paraloc2);
  523. paramanager.freecgpara(list,paraloc1);
  524. g_call(list,name);
  525. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  526. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg2);
  527. paraloc2.done;
  528. paraloc1.done;
  529. end;
  530. procedure tcg68k.a_call_name(list : TAsmList;const s : string; weak: boolean);
  531. var
  532. sym: tasmsymbol;
  533. const
  534. jmp_inst: array[boolean] of tasmop = ( A_JSR, A_BSR );
  535. begin
  536. if not(weak) then
  537. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION)
  538. else
  539. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION);
  540. list.concat(taicpu.op_sym(jmp_inst[tf_code_small in target_info.flags],S_NO,sym));
  541. end;
  542. procedure tcg68k.a_call_reg(list : TAsmList;reg: tregister);
  543. var
  544. tmpref : treference;
  545. tmpreg : tregister;
  546. instr : taicpu;
  547. begin
  548. if isaddressregister(reg) then
  549. begin
  550. { if we have an address register, we can jump to the address directly }
  551. reference_reset_base(tmpref,reg,0,ctempposinvalid,4,[]);
  552. end
  553. else
  554. begin
  555. { if we have a data register, we need to move it to an address register first }
  556. tmpreg:=getaddressregister(list);
  557. reference_reset_base(tmpref,tmpreg,0,ctempposinvalid,4,[]);
  558. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,tmpreg);
  559. add_move_instruction(instr);
  560. list.concat(instr);
  561. end;
  562. list.concat(taicpu.op_ref(A_JSR,S_NO,tmpref));
  563. end;
  564. procedure tcg68k.a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);
  565. var
  566. opsize: topsize;
  567. begin
  568. opsize:=tcgsize2opsize[size];
  569. if isaddressregister(register) then
  570. begin
  571. { an m68k manual I have recommends SUB Ax,Ax to be used instead of CLR for address regs }
  572. { Premature optimization is the root of all evil - this code breaks spilling if the
  573. register contains a spilled regvar, eg. a Pointer which is set to nil, then random
  574. havoc happens... This is kept here for reference now, to allow fixing of the spilling
  575. later. Most of the optimizations below here could be moved to the optimizer. (KB) }
  576. {if a = 0 then
  577. list.concat(taicpu.op_reg_reg(A_SUB,S_L,register,register))
  578. else}
  579. { ISA B/C Coldfire has MOV3Q which can move -1 or 1..7 to any reg }
  580. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  581. ((longint(a) = -1) or ((longint(a) > 0) and (longint(a) < 8))) then
  582. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  583. else
  584. { MOVEA.W will sign extend the value in the dest. reg to full 32 bits
  585. (specific to Ax regs only) }
  586. if isvalue16bit(a) then
  587. list.concat(taicpu.op_const_reg(A_MOVEA,S_W,longint(a),register))
  588. else
  589. list.concat(taicpu.op_const_reg(A_MOVEA,S_L,longint(a),register));
  590. end
  591. else
  592. if a = 0 then
  593. list.concat(taicpu.op_reg(A_CLR,S_L,register))
  594. else
  595. begin
  596. { Prefer MOV3Q if applicable, it allows replacement spilling for register }
  597. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  598. ((longint(a)=-1) or ((longint(a)>0) and (longint(a)<8))) then
  599. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  600. else if (longint(a) >= low(shortint)) and (longint(a) <= high(shortint)) then
  601. list.concat(taicpu.op_const_reg(A_MOVEQ,S_L,longint(a),register))
  602. else
  603. begin
  604. { ISA B/C Coldfire has sign extend/zero extend moves }
  605. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  606. (size in [OS_16, OS_8, OS_S16, OS_S8]) and
  607. ((longint(a) >= low(smallint)) and (longint(a) <= high(smallint))) then
  608. begin
  609. if size in [OS_16, OS_8] then
  610. list.concat(taicpu.op_const_reg(A_MVZ,opsize,longint(a),register))
  611. else
  612. list.concat(taicpu.op_const_reg(A_MVS,opsize,longint(a),register));
  613. end
  614. else
  615. begin
  616. { clear the register first, for unsigned and positive values, so
  617. we don't need to zero extend after }
  618. if (size in [OS_16,OS_8]) or
  619. ((size in [OS_S16,OS_S8]) and (a > 0)) then
  620. list.concat(taicpu.op_reg(A_CLR,S_L,register));
  621. list.concat(taicpu.op_const_reg(A_MOVE,opsize,longint(a),register));
  622. { only sign extend if we need to, zero extension is not necessary because the CLR.L above }
  623. if (size in [OS_S16,OS_S8]) and (a < 0) then
  624. sign_extend(list,size,register);
  625. end;
  626. end;
  627. end;
  628. end;
  629. procedure tcg68k.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  630. var
  631. hreg : tregister;
  632. href : treference;
  633. begin
  634. if needs_unaligned(ref.alignment,tosize) then
  635. begin
  636. inherited;
  637. exit;
  638. end;
  639. a:=longint(a);
  640. href:=ref;
  641. fixref(list,href,false);
  642. if (a=0) and not (current_settings.cputype = cpu_mc68000) then
  643. list.concat(taicpu.op_ref(A_CLR,tcgsize2opsize[tosize],href))
  644. else if (tcgsize2opsize[tosize]=S_L) and
  645. (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  646. ((a=-1) or ((a>0) and (a<8))) then
  647. list.concat(taicpu.op_const_ref(A_MOV3Q,S_L,a,href))
  648. { for coldfire we need to go through a temporary register if we have a
  649. offset, index or symbol given }
  650. else if (current_settings.cputype in cpu_coldfire) and
  651. (
  652. (href.offset<>0) or
  653. { TODO : check whether we really need this second condition }
  654. (href.index<>NR_NO) or
  655. assigned(href.symbol)
  656. ) then
  657. begin
  658. hreg:=getintregister(list,tosize);
  659. a_load_const_reg(list,tosize,a,hreg);
  660. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  661. end
  662. else
  663. { loading via a register is almost always faster if the value is small.
  664. (with the 68040 being the only notable exception, so maybe disable
  665. this on a '040? but the difference is minor) it also results in shorter
  666. code. (KB) }
  667. if isvalue8bit(a) and (tcgsize2opsize[tosize] = S_L) then
  668. begin
  669. hreg:=getintregister(list,OS_INT);
  670. a_load_const_reg(list,OS_INT,a,hreg); // this will use moveq et.al.
  671. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  672. end
  673. else
  674. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[tosize],longint(a),href));
  675. end;
  676. procedure tcg68k.a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  677. var
  678. href : treference;
  679. hreg : tregister;
  680. begin
  681. if needs_unaligned(ref.alignment,tosize) then
  682. begin
  683. //list.concat(tai_comment.create(strpnew('a_load_reg_ref calling unaligned')));
  684. a_load_reg_ref_unaligned(list,fromsize,tosize,register,ref);
  685. exit;
  686. end;
  687. href := ref;
  688. hreg := register;
  689. fixref(list,href,false);
  690. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  691. begin
  692. hreg:=getintregister(list,tosize);
  693. a_load_reg_reg(list,fromsize,tosize,register,hreg);
  694. end;
  695. { move to destination reference }
  696. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,href));
  697. end;
  698. procedure tcg68k.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  699. var
  700. tmpref : treference;
  701. tmpreg,
  702. tmpreg2 : tregister;
  703. begin
  704. if not needs_unaligned(ref.alignment,tosize) then
  705. begin
  706. a_load_reg_ref(list,fromsize,tosize,register,ref);
  707. exit;
  708. end;
  709. list.concat(tai_comment.create(strpnew('a_load_reg_ref_unaligned: generating unaligned store')));
  710. tmpreg2:=getaddressregister(list);
  711. tmpref:=ref;
  712. inc(tmpref.offset,tcgsize2size[tosize]-1);
  713. a_loadaddr_ref_reg(list,tmpref,tmpreg2);
  714. reference_reset_base(tmpref,tmpreg2,0,ctempposinvalid,1,ref.volatility);
  715. tmpref.direction:=dir_none;
  716. tmpreg:=getintregister(list,tosize);
  717. a_load_reg_reg(list,fromsize,tosize,register,tmpreg);
  718. case tosize of
  719. OS_16,OS_S16:
  720. begin
  721. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  722. list.concat(taicpu.op_const_reg(A_LSR,S_W,8,tmpreg));
  723. tmpref.direction:=dir_dec;
  724. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  725. end;
  726. OS_32,OS_S32:
  727. begin
  728. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  729. list.concat(taicpu.op_const_reg(A_LSR,S_W,8,tmpreg));
  730. tmpref.direction:=dir_dec;
  731. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  732. list.concat(taicpu.op_reg(A_SWAP,S_L,tmpreg));
  733. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  734. list.concat(taicpu.op_const_reg(A_LSR,S_W,8,tmpreg));
  735. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  736. end
  737. else
  738. internalerror(2016052201);
  739. end;
  740. end;
  741. procedure tcg68k.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  742. var
  743. aref: treference;
  744. bref: treference;
  745. usetemp: boolean;
  746. hreg: TRegister;
  747. begin
  748. usetemp:=TCGSize2OpSize[fromsize]<>TCGSize2OpSize[tosize];
  749. usetemp:=usetemp or (needs_unaligned(sref.alignment,fromsize) or needs_unaligned(dref.alignment,tosize));
  750. aref := sref;
  751. bref := dref;
  752. if usetemp then
  753. begin
  754. { if we need to change the size then always use a temporary register }
  755. hreg:=getintregister(list,fromsize);
  756. if needs_unaligned(sref.alignment,fromsize) then
  757. a_load_ref_reg_unaligned(list,fromsize,tosize,sref,hreg)
  758. else
  759. begin
  760. fixref(list,aref,false);
  761. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],aref,hreg));
  762. sign_extend(list,fromsize,tosize,hreg);
  763. end;
  764. if needs_unaligned(dref.alignment,tosize) then
  765. a_load_reg_ref_unaligned(list,tosize,tosize,hreg,dref)
  766. else
  767. begin
  768. { if we use a temp register, we don't need to fully resolve
  769. the dest ref, not even on coldfire }
  770. fixref(list,bref,false);
  771. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,bref));
  772. end;
  773. end
  774. else
  775. begin
  776. fixref(list,aref,false);
  777. fixref(list,bref,current_settings.cputype in cpu_coldfire);
  778. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],aref,bref));
  779. end;
  780. end;
  781. procedure tcg68k.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  782. var
  783. instr : taicpu;
  784. hreg : tregister;
  785. opsize : topsize;
  786. begin
  787. { move to destination register }
  788. opsize:=TCGSize2OpSize[fromsize];
  789. if isaddressregister(reg2) and not (opsize in [S_L]) then
  790. begin
  791. hreg:=cg.getintregister(list,OS_ADDR);
  792. instr:=taicpu.op_reg_reg(A_MOVE,TCGSize2OpSize[fromsize],reg1,hreg);
  793. add_move_instruction(instr);
  794. list.concat(instr);
  795. sign_extend(list,fromsize,hreg);
  796. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg2));
  797. end
  798. else
  799. begin
  800. if not isregoverlap(reg1,reg2) then
  801. begin
  802. instr:=taicpu.op_reg_reg(A_MOVE,opsize,reg1,reg2);
  803. add_move_instruction(instr);
  804. list.concat(instr);
  805. end;
  806. sign_extend(list,fromsize,tosize,reg2);
  807. end;
  808. end;
  809. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  810. var
  811. href : treference;
  812. hreg : tregister;
  813. size : tcgsize;
  814. opsize: topsize;
  815. needsext: boolean;
  816. begin
  817. if needs_unaligned(ref.alignment,fromsize) then
  818. begin
  819. //list.concat(tai_comment.create(strpnew('a_load_ref_reg calling unaligned')));
  820. a_load_ref_reg_unaligned(list,fromsize,tosize,ref,register);
  821. exit;
  822. end;
  823. href:=ref;
  824. fixref(list,href,false);
  825. needsext:=tcgsize2size[fromsize]<tcgsize2size[tosize];
  826. if needsext then
  827. size:=fromsize
  828. else
  829. size:=tosize;
  830. opsize:=TCGSize2OpSize[size];
  831. if isaddressregister(register) and not (opsize in [S_L]) then
  832. hreg:=getintregister(list,OS_ADDR)
  833. else
  834. hreg:=register;
  835. if needsext and (CPUM68K_HAS_MVSMVZ in cpu_capabilities[current_settings.cputype]) and not (opsize in [S_L]) then
  836. begin
  837. if fromsize in [OS_S8,OS_S16] then
  838. list.concat(taicpu.op_ref_reg(A_MVS,opsize,href,hreg))
  839. else if fromsize in [OS_8,OS_16] then
  840. list.concat(taicpu.op_ref_reg(A_MVZ,opsize,href,hreg))
  841. else
  842. internalerror(2016050502);
  843. end
  844. else
  845. begin
  846. if needsext and (fromsize in [OS_8,OS_16]) then
  847. begin
  848. //list.concat(tai_comment.create(strpnew('a_load_ref_reg: zero ext')));
  849. a_load_const_reg(list,OS_32,0,hreg);
  850. needsext:=false;
  851. end;
  852. list.concat(taicpu.op_ref_reg(A_MOVE,opsize,href,hreg));
  853. if needsext then
  854. sign_extend(list,size,hreg);
  855. end;
  856. if hreg<>register then
  857. a_load_reg_reg(list,OS_ADDR,OS_ADDR,hreg,register);
  858. end;
  859. procedure tcg68k.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  860. var
  861. tmpref : treference;
  862. tmpreg,
  863. tmpreg2 : tregister;
  864. begin
  865. if not needs_unaligned(ref.alignment,fromsize) then
  866. begin
  867. a_load_ref_reg(list,fromsize,tosize,ref,register);
  868. exit;
  869. end;
  870. list.concat(tai_comment.create(strpnew('a_load_ref_reg_unaligned: generating unaligned load')));
  871. tmpreg2:=getaddressregister(list);
  872. a_loadaddr_ref_reg(list,ref,tmpreg2);
  873. reference_reset_base(tmpref,tmpreg2,0,ctempposinvalid,1,ref.volatility);
  874. tmpref.direction:=dir_inc;
  875. if isaddressregister(register) then
  876. tmpreg:=getintregister(list,OS_ADDR)
  877. else
  878. tmpreg:=register;
  879. case fromsize of
  880. OS_16,OS_S16:
  881. begin
  882. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  883. list.concat(taicpu.op_const_reg(A_LSL,S_W,8,tmpreg));
  884. tmpref.direction:=dir_none;
  885. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  886. sign_extend(list,fromsize,tmpreg);
  887. end;
  888. OS_32,OS_S32:
  889. begin
  890. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  891. list.concat(taicpu.op_const_reg(A_LSL,S_W,8,tmpreg));
  892. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  893. list.concat(taicpu.op_reg(A_SWAP,S_L,tmpreg));
  894. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  895. list.concat(taicpu.op_const_reg(A_LSL,S_W,8,tmpreg));
  896. tmpref.direction:=dir_none;
  897. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  898. end
  899. else
  900. internalerror(2016052103);
  901. end;
  902. if tmpreg<>register then
  903. a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpreg,register);
  904. end;
  905. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  906. var
  907. href : treference;
  908. hreg : tregister;
  909. begin
  910. href:=ref;
  911. fixref(list, href, false);
  912. if not isaddressregister(r) then
  913. begin
  914. hreg:=getaddressregister(list);
  915. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  916. a_load_reg_reg(list, OS_ADDR, OS_ADDR, hreg, r);
  917. end
  918. else
  919. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  920. end;
  921. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  922. var
  923. instr : taicpu;
  924. op: tasmop;
  925. href: treference;
  926. hreg: tregister;
  927. begin
  928. if fromsize > tosize then
  929. begin
  930. { we have to do a load-store through an intregister or the stack in this case,
  931. which is probably the fastest way, and simpler than messing around with FPU control
  932. words for one-off custom rounding (KB) }
  933. case tosize of
  934. OS_F32:
  935. begin
  936. //list.concat(tai_comment.create(strpnew('a_loadfpu_reg_reg rounding via intreg')));
  937. hreg := getintregister(list,OS_32);
  938. list.concat(taicpu.op_reg_reg(A_FMOVE, tcgsize2opsize[tosize], reg1, hreg));
  939. list.concat(taicpu.op_reg_reg(A_FMOVE, tcgsize2opsize[tosize], hreg, reg2));
  940. end;
  941. else
  942. begin
  943. //list.concat(tai_comment.create(strpnew('a_loadfpu_reg_reg rounding via stack')));
  944. reference_reset_base(href, NR_STACK_POINTER_REG, 0, ctempposinvalid, 0, []);
  945. href.direction:=dir_dec;
  946. list.concat(taicpu.op_reg_ref(A_FMOVE, tcgsize2opsize[tosize], reg1, href));
  947. href.direction:=dir_inc;
  948. list.concat(taicpu.op_ref_reg(A_FMOVE, tcgsize2opsize[tosize], href, reg2));
  949. end;
  950. end;
  951. end
  952. else
  953. begin
  954. instr:=taicpu.op_reg_reg(A_FMOVE,fpuregopsize,reg1,reg2);
  955. add_move_instruction(instr);
  956. list.concat(instr);
  957. end;
  958. end;
  959. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  960. var
  961. opsize : topsize;
  962. href : treference;
  963. begin
  964. opsize := tcgsize2opsize[fromsize];
  965. href := ref;
  966. fixref(list,href,current_settings.fputype = fpu_coldfire);
  967. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  968. if fromsize > tosize then
  969. a_load_reg_reg(list,fromsize,tosize,reg,reg);
  970. end;
  971. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  972. var
  973. opsize : topsize;
  974. href : treference;
  975. begin
  976. opsize := tcgsize2opsize[tosize];
  977. href := ref;
  978. fixref(list,href,current_settings.fputype = fpu_coldfire);
  979. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg,href));
  980. end;
  981. procedure tcg68k.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const reg : tregister;const cgpara : tcgpara);
  982. var
  983. ref : treference;
  984. begin
  985. if use_push(cgpara) and (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]) then
  986. begin
  987. cgpara.check_simple_location;
  988. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, ctempposinvalid, cgpara.alignment, []);
  989. ref.direction := dir_dec;
  990. list.concat(taicpu.op_reg_ref(A_FMOVE,tcgsize2opsize[cgpara.location^.size],reg,ref));
  991. end
  992. else
  993. inherited a_loadfpu_reg_cgpara(list,size,reg,cgpara);
  994. end;
  995. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  996. var
  997. href, href2 : treference;
  998. freg : tregister;
  999. begin
  1000. if current_settings.fputype = fpu_soft then
  1001. case cgpara.location^.loc of
  1002. LOC_REFERENCE,LOC_CREFERENCE:
  1003. begin
  1004. case size of
  1005. OS_F64:
  1006. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  1007. OS_F32:
  1008. a_load_ref_cgpara(list,size,ref,cgpara);
  1009. else
  1010. internalerror(2013021201);
  1011. end;
  1012. end;
  1013. else
  1014. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1015. end
  1016. else
  1017. if use_push(cgpara) and (FPUM68K_HAS_HARDWARE in fpu_capabilities[current_settings.fputype]) then
  1018. begin
  1019. //list.concat(tai_comment.create(strpnew('a_loadfpu_ref_cgpara copy')));
  1020. cgpara.check_simple_location;
  1021. reference_reset_base(href, NR_STACK_POINTER_REG, 0, ctempposinvalid, cgpara.alignment, []);
  1022. href.direction := dir_dec;
  1023. case size of
  1024. OS_F64:
  1025. begin
  1026. href2:=ref;
  1027. inc(href2.offset,8);
  1028. fixref(list,href2,true);
  1029. href2.direction := dir_dec;
  1030. cg.a_load_ref_ref(list,OS_32,OS_32,href2,href);
  1031. cg.a_load_ref_ref(list,OS_32,OS_32,href2,href);
  1032. end;
  1033. OS_F32:
  1034. cg.a_load_ref_ref(list,OS_32,OS_32,ref,href);
  1035. else
  1036. internalerror(2017052110);
  1037. end;
  1038. end
  1039. else
  1040. begin
  1041. //list.concat(tai_comment.create(strpnew('a_loadfpu_ref_cgpara inherited')));
  1042. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1043. end;
  1044. end;
  1045. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  1046. var
  1047. scratch_reg : tregister;
  1048. scratch_reg2: tregister;
  1049. opcode : tasmop;
  1050. begin
  1051. optimize_op_const(size, op, a);
  1052. opcode := topcg2tasmop[op];
  1053. if (a >0) and (a<=high(dword)) then
  1054. a:=longint(dword(a))
  1055. else if (a>=low(longint)) then
  1056. a:=longint(a)
  1057. else
  1058. internalerror(201810201);
  1059. case op of
  1060. OP_NONE :
  1061. begin
  1062. { Opcode is optimized away }
  1063. end;
  1064. OP_MOVE :
  1065. begin
  1066. { Optimized, replaced with a simple load }
  1067. a_load_const_reg(list,size,a,reg);
  1068. end;
  1069. OP_ADD,
  1070. OP_SUB:
  1071. begin
  1072. { add/sub works the same way, so have it unified here }
  1073. if (a >= 1) and (a <= 8) then
  1074. if (op = OP_ADD) then
  1075. opcode:=A_ADDQ
  1076. else
  1077. opcode:=A_SUBQ;
  1078. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  1079. end;
  1080. OP_AND,
  1081. OP_OR,
  1082. OP_XOR:
  1083. begin
  1084. scratch_reg := force_to_dataregister(list, size, reg);
  1085. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1086. move_if_needed(list, size, scratch_reg, reg);
  1087. end;
  1088. OP_DIV,
  1089. OP_IDIV:
  1090. begin
  1091. internalerror(2002081601);
  1092. end;
  1093. OP_MUL,
  1094. OP_IMUL:
  1095. begin
  1096. { NOTE: better have this as fast as possible on every CPU in all cases,
  1097. because the compiler uses OP_IMUL for array indexing... (KB) }
  1098. { ColdFire doesn't support MULS/MULU <imm>,dX }
  1099. if not (CPUM68K_HAS_MULIMM in cpu_capabilities[current_settings.cputype]) then
  1100. begin
  1101. { move const to a register first }
  1102. scratch_reg := getintregister(list,OS_INT);
  1103. a_load_const_reg(list, size, a, scratch_reg);
  1104. { do the multiplication }
  1105. scratch_reg2 := force_to_dataregister(list, size, reg);
  1106. sign_extend(list, size, scratch_reg2);
  1107. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  1108. { move the value back to the original register }
  1109. move_if_needed(list, size, scratch_reg2, reg);
  1110. end
  1111. else
  1112. begin
  1113. if CPUM68K_HAS_32BITMUL in cpu_capabilities[current_settings.cputype] then
  1114. begin
  1115. { do the multiplication }
  1116. scratch_reg := force_to_dataregister(list, size, reg);
  1117. sign_extend(list, size, scratch_reg);
  1118. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  1119. { move the value back to the original register }
  1120. move_if_needed(list, size, scratch_reg, reg);
  1121. end
  1122. else
  1123. { Fallback branch, plain 68000 for now }
  1124. if not optimize_const_mul_to_shift_sub_add(list, 5, a, size, reg) then
  1125. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  1126. if op = OP_MUL then
  1127. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  1128. else
  1129. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1130. end;
  1131. end;
  1132. OP_ROL,
  1133. OP_ROR,
  1134. OP_SAR,
  1135. OP_SHL,
  1136. OP_SHR :
  1137. begin
  1138. scratch_reg := force_to_dataregister(list, size, reg);
  1139. sign_extend(list, size, scratch_reg);
  1140. { some special cases which can generate smarter code
  1141. using the SWAP instruction }
  1142. if (a = 16) then
  1143. begin
  1144. if (op = OP_SHL) then
  1145. begin
  1146. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1147. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1148. end
  1149. else if (op = OP_SHR) then
  1150. begin
  1151. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1152. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1153. end
  1154. else if (op = OP_SAR) then
  1155. begin
  1156. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1157. list.concat(taicpu.op_reg(A_EXT,S_L,scratch_reg));
  1158. end
  1159. else if (op = OP_ROR) or (op = OP_ROL) then
  1160. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg))
  1161. end
  1162. else if (a >= 1) and (a <= 8) then
  1163. begin
  1164. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1165. end
  1166. else if (a >= 9) and (a < 16) then
  1167. begin
  1168. { Use two ops instead of const -> reg + shift with reg, because
  1169. this way is the same in length and speed but has less register
  1170. pressure }
  1171. list.concat(taicpu.op_const_reg(opcode, S_L, 8, scratch_reg));
  1172. list.concat(taicpu.op_const_reg(opcode, S_L, a-8, scratch_reg));
  1173. end
  1174. else
  1175. begin
  1176. { move const to a register first }
  1177. scratch_reg2 := getintregister(list,OS_INT);
  1178. a_load_const_reg(list, size, a, scratch_reg2);
  1179. { do the operation }
  1180. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1181. end;
  1182. { move the value back to the original register }
  1183. move_if_needed(list, size, scratch_reg, reg);
  1184. end;
  1185. else
  1186. internalerror(2002072901);
  1187. end;
  1188. end;
  1189. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1190. var
  1191. opcode: tasmop;
  1192. opsize: topsize;
  1193. href : treference;
  1194. hreg : tregister;
  1195. begin
  1196. optimize_op_const(size, op, a);
  1197. opcode := topcg2tasmop[op];
  1198. opsize := TCGSize2OpSize[size];
  1199. { on ColdFire all arithmetic operations are only possible on 32bit }
  1200. if needs_unaligned(ref.alignment,size) or
  1201. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)
  1202. and not (op in [OP_NONE,OP_MOVE])) then
  1203. begin
  1204. inherited;
  1205. exit;
  1206. end;
  1207. case op of
  1208. OP_NONE :
  1209. begin
  1210. { opcode was optimized away }
  1211. end;
  1212. OP_MOVE :
  1213. begin
  1214. { Optimized, replaced with a simple load }
  1215. a_load_const_ref(list,size,a,ref);
  1216. end;
  1217. OP_AND,
  1218. OP_OR,
  1219. OP_XOR :
  1220. begin
  1221. //list.concat(tai_comment.create(strpnew('a_op_const_ref: bitwise')));
  1222. hreg:=getintregister(list,size);
  1223. a_load_const_reg(list,size,a,hreg);
  1224. href:=ref;
  1225. fixref(list,href,false);
  1226. list.concat(taicpu.op_reg_ref(opcode, opsize, hreg, href));
  1227. end;
  1228. OP_ADD,
  1229. OP_SUB :
  1230. begin
  1231. href:=ref;
  1232. { add/sub works the same way, so have it unified here }
  1233. if (a >= 1) and (a <= 8) then
  1234. begin
  1235. fixref(list,href,false);
  1236. if (op = OP_ADD) then
  1237. opcode:=A_ADDQ
  1238. else
  1239. opcode:=A_SUBQ;
  1240. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1241. end
  1242. else
  1243. if not(current_settings.cputype in cpu_coldfire) then
  1244. begin
  1245. fixref(list,href,false);
  1246. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1247. end
  1248. else
  1249. { on ColdFire, ADDI/SUBI cannot act on memory
  1250. so we can only go through a register }
  1251. inherited;
  1252. end;
  1253. else begin
  1254. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1255. inherited;
  1256. end;
  1257. end;
  1258. end;
  1259. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1260. var
  1261. hreg1, hreg2: tregister;
  1262. opcode : tasmop;
  1263. opsize : topsize;
  1264. begin
  1265. opcode := topcg2tasmop[op];
  1266. if current_settings.cputype in cpu_coldfire then
  1267. opsize := S_L
  1268. else
  1269. opsize := TCGSize2OpSize[size];
  1270. case op of
  1271. OP_ADD,
  1272. OP_SUB:
  1273. begin
  1274. if current_settings.cputype in cpu_coldfire then
  1275. begin
  1276. { operation only allowed only a longword }
  1277. sign_extend(list, size, src);
  1278. sign_extend(list, size, dst);
  1279. end;
  1280. list.concat(taicpu.op_reg_reg(opcode, opsize, src, dst));
  1281. end;
  1282. OP_AND,OP_OR,
  1283. OP_SAR,OP_SHL,
  1284. OP_SHR,OP_XOR:
  1285. begin
  1286. { load to data registers }
  1287. hreg1 := force_to_dataregister(list, size, src);
  1288. hreg2 := force_to_dataregister(list, size, dst);
  1289. if current_settings.cputype in cpu_coldfire then
  1290. begin
  1291. { operation only allowed only a longword }
  1292. {!***************************************
  1293. in the case of shifts, the value to
  1294. shift by, should already be valid, so
  1295. no need to sign extend the value
  1296. !
  1297. }
  1298. if op in [OP_AND,OP_OR,OP_XOR] then
  1299. sign_extend(list, size, hreg1);
  1300. sign_extend(list, size, hreg2);
  1301. end;
  1302. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1303. { move back result into destination register }
  1304. move_if_needed(list, size, hreg2, dst);
  1305. end;
  1306. OP_DIV,
  1307. OP_IDIV :
  1308. begin
  1309. internalerror(2002081602);
  1310. end;
  1311. OP_MUL,
  1312. OP_IMUL:
  1313. begin
  1314. if not (CPUM68K_HAS_32BITMUL in cpu_capabilities[current_settings.cputype]) then
  1315. if op = OP_MUL then
  1316. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_dword')
  1317. else
  1318. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_longint')
  1319. else
  1320. begin
  1321. { 68020+ and ColdFire codepath, probably could be improved }
  1322. hreg1 := force_to_dataregister(list, size, src);
  1323. hreg2 := force_to_dataregister(list, size, dst);
  1324. sign_extend(list, size, hreg1);
  1325. sign_extend(list, size, hreg2);
  1326. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1327. { move back result into destination register }
  1328. move_if_needed(list, size, hreg2, dst);
  1329. end;
  1330. end;
  1331. OP_NEG,
  1332. OP_NOT :
  1333. begin
  1334. { if there are two operands, move the register,
  1335. since the operation will only be done on the result
  1336. register. }
  1337. if (src<>dst) then
  1338. a_load_reg_reg(list,size,size,src,dst);
  1339. hreg2 := force_to_dataregister(list, size, dst);
  1340. { coldfire only supports long version }
  1341. if current_settings.cputype in cpu_ColdFire then
  1342. sign_extend(list, size, hreg2);
  1343. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1344. { move back the result to the result register if needed }
  1345. move_if_needed(list, size, hreg2, dst);
  1346. end;
  1347. else
  1348. internalerror(2002072902);
  1349. end;
  1350. end;
  1351. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1352. var
  1353. opcode : tasmop;
  1354. opsize : topsize;
  1355. href : treference;
  1356. hreg : tregister;
  1357. begin
  1358. opcode := topcg2tasmop[op];
  1359. opsize := TCGSize2OpSize[size];
  1360. { on ColdFire all arithmetic operations are only possible on 32bit
  1361. and addressing modes are limited }
  1362. if needs_unaligned(ref.alignment,size) or
  1363. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1364. begin
  1365. //list.concat(tai_comment.create(strpnew('a_op_reg_ref: inherited #1')));
  1366. inherited;
  1367. exit;
  1368. end;
  1369. case op of
  1370. OP_ADD,
  1371. OP_SUB,
  1372. OP_OR,
  1373. OP_XOR,
  1374. OP_AND:
  1375. begin
  1376. //list.concat(tai_comment.create(strpnew('a_op_reg_ref: normal op')));
  1377. href:=ref;
  1378. fixref(list,href,false);
  1379. { areg -> ref arithmetic operations are impossible on 68k }
  1380. hreg:=force_to_dataregister(list,size,reg);
  1381. { add/sub works the same way, so have it unified here }
  1382. list.concat(taicpu.op_reg_ref(opcode, opsize, hreg, href));
  1383. end;
  1384. else begin
  1385. //list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited #2')));
  1386. inherited;
  1387. end;
  1388. end;
  1389. end;
  1390. procedure tcg68k.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1391. var
  1392. opcode : tasmop;
  1393. opsize : topsize;
  1394. href : treference;
  1395. hreg : tregister;
  1396. begin
  1397. opcode := topcg2tasmop[op];
  1398. opsize := TCGSize2OpSize[size];
  1399. { on ColdFire all arithmetic operations are only possible on 32bit
  1400. and addressing modes are limited }
  1401. if needs_unaligned(ref.alignment,size) or
  1402. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1403. begin
  1404. //list.concat(tai_comment.create(strpnew('a_op_ref_reg: inherited #1')));
  1405. inherited;
  1406. exit;
  1407. end;
  1408. case op of
  1409. OP_ADD,
  1410. OP_SUB,
  1411. OP_OR,
  1412. OP_AND,
  1413. OP_MUL,
  1414. OP_IMUL:
  1415. begin
  1416. //list.concat(tai_comment.create(strpnew('a_op_ref_reg: normal op')));
  1417. href:=ref;
  1418. { Coldfire doesn't support d(Ax,Dx) for long MULx... }
  1419. fixref(list,href,(op in [OP_MUL,OP_IMUL]) and
  1420. (current_settings.cputype in cpu_coldfire));
  1421. list.concat(taicpu.op_ref_reg(opcode, opsize, href, reg));
  1422. end;
  1423. else begin
  1424. //list.concat(tai_comment.create(strpnew('a_op_ref_reg inherited #2')));
  1425. inherited;
  1426. end;
  1427. end;
  1428. end;
  1429. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1430. l : tasmlabel);
  1431. var
  1432. hregister : tregister;
  1433. instr : taicpu;
  1434. need_temp_reg : boolean;
  1435. temp_size: topsize;
  1436. begin
  1437. need_temp_reg := false;
  1438. { plain 68000 doesn't support address registers for TST }
  1439. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1440. (a = 0) and isaddressregister(reg);
  1441. { ColdFire doesn't support address registers for CMPI }
  1442. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1443. and (a <> 0) and isaddressregister(reg));
  1444. if need_temp_reg then
  1445. begin
  1446. hregister := getintregister(list,OS_INT);
  1447. temp_size := TCGSize2OpSize[size];
  1448. if temp_size < S_W then
  1449. temp_size := S_W;
  1450. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1451. add_move_instruction(instr);
  1452. list.concat(instr);
  1453. reg := hregister;
  1454. { do sign extension if size had to be modified }
  1455. if temp_size <> TCGSize2OpSize[size] then
  1456. begin
  1457. sign_extend(list, size, reg);
  1458. size:=OS_INT;
  1459. end;
  1460. end;
  1461. if a = 0 then
  1462. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1463. else
  1464. begin
  1465. { ColdFire ISA A also needs S_L for CMPI }
  1466. { Note: older QEMU pukes from CMPI sizes <> .L even on ISA B/C, but
  1467. it's actually *LEGAL*, see CFPRM, page 4-30, the bug also seems
  1468. fixed in recent QEMU, but only when CPU cfv4e is forced, not by
  1469. default. (KB) }
  1470. if current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c,cpu_cfv4e]} then
  1471. begin
  1472. sign_extend(list, size, reg);
  1473. size:=OS_INT;
  1474. end;
  1475. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1476. end;
  1477. { emit the actual jump to the label }
  1478. a_jmp_cond(list,cmp_op,l);
  1479. end;
  1480. procedure tcg68k.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel);
  1481. var
  1482. tmpref: treference;
  1483. begin
  1484. { optimize for usage of TST here, so ref compares against zero, which is the
  1485. most common case by far in the RTL code at least (KB) }
  1486. if not needs_unaligned(ref.alignment,size) and (a = 0) then
  1487. begin
  1488. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label with TST')));
  1489. tmpref:=ref;
  1490. fixref(list,tmpref,false);
  1491. list.concat(taicpu.op_ref(A_TST,tcgsize2opsize[size],tmpref));
  1492. a_jmp_cond(list,cmp_op,l);
  1493. end
  1494. else
  1495. begin
  1496. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label inherited')));
  1497. inherited;
  1498. end;
  1499. end;
  1500. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1501. begin
  1502. if (current_settings.cputype in cpu_coldfire-[cpu_isa_b,cpu_isa_c,cpu_cfv4e]) then
  1503. begin
  1504. sign_extend(list,size,reg1);
  1505. sign_extend(list,size,reg2);
  1506. size:=OS_INT;
  1507. end;
  1508. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1509. { emit the actual jump to the label }
  1510. a_jmp_cond(list,cmp_op,l);
  1511. end;
  1512. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1513. var
  1514. ai: taicpu;
  1515. begin
  1516. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1517. ai.is_jmp := true;
  1518. list.concat(ai);
  1519. end;
  1520. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1521. var
  1522. ai: taicpu;
  1523. begin
  1524. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1525. ai.is_jmp := true;
  1526. list.concat(ai);
  1527. end;
  1528. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1529. var
  1530. ai : taicpu;
  1531. begin
  1532. if not (f in FloatResFlags) then
  1533. ai := Taicpu.op_sym(A_BXX,S_NO,l)
  1534. else
  1535. ai := Taicpu.op_sym(A_FBXX,S_NO,l);
  1536. ai.SetCondition(flags_to_cond(f));
  1537. ai.is_jmp := true;
  1538. list.concat(ai);
  1539. end;
  1540. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1541. var
  1542. ai : taicpu;
  1543. htrue: tasmlabel;
  1544. begin
  1545. if isaddressregister(reg) then
  1546. internalerror(2017051701);
  1547. if (f in FloatResFlags) then
  1548. begin
  1549. //list.concat(tai_comment.create(strpnew('flags2reg: float resflags')));
  1550. current_asmdata.getjumplabel(htrue);
  1551. a_load_const_reg(current_asmdata.CurrAsmList,OS_32,1,reg);
  1552. a_jmp_flags(list, f, htrue);
  1553. a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,reg);
  1554. a_label(current_asmdata.CurrAsmList,htrue);
  1555. exit;
  1556. end;
  1557. ai:=Taicpu.Op_reg(A_Sxx,S_B,reg);
  1558. ai.SetCondition(flags_to_cond(f));
  1559. list.concat(ai);
  1560. { Scc stores a complete byte of 1s, but the compiler expects only one
  1561. bit set, so ensure this is the case }
  1562. if not (current_settings.cputype in cpu_coldfire) then
  1563. begin
  1564. if size in [OS_S8,OS_8] then
  1565. list.concat(taicpu.op_reg(A_NEG,S_B,reg))
  1566. else
  1567. list.concat(taicpu.op_const_reg(A_AND,TCgSize2OpSize[size],1,reg));
  1568. end
  1569. else
  1570. list.concat(taicpu.op_const_reg(A_AND,S_L,1,reg));
  1571. end;
  1572. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1573. const
  1574. lentocgsize: array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  1575. var
  1576. helpsize : longint;
  1577. i : byte;
  1578. hregister : tregister;
  1579. iregister : tregister;
  1580. jregister : tregister;
  1581. hl : tasmlabel;
  1582. srcrefp,dstrefp : treference;
  1583. srcref,dstref : treference;
  1584. begin
  1585. if (len = 1) or
  1586. ((len in [2,4]) and
  1587. not needs_unaligned(source.alignment,lentocgsize[len]) and
  1588. not needs_unaligned(dest.alignment,lentocgsize[len])) then
  1589. begin
  1590. //list.concat(tai_comment.create(strpnew('g_concatcopy: small')));
  1591. a_load_ref_ref(list,lentocgsize[len],lentocgsize[len],source,dest);
  1592. exit;
  1593. end;
  1594. //list.concat(tai_comment.create(strpnew('g_concatcopy')));
  1595. hregister := getintregister(list,OS_INT);
  1596. iregister:=getaddressregister(list);
  1597. reference_reset_base(srcref,iregister,0,source.temppos,source.alignment,source.volatility);
  1598. srcrefp:=srcref;
  1599. srcrefp.direction := dir_inc;
  1600. jregister:=getaddressregister(list);
  1601. reference_reset_base(dstref,jregister,0,dest.temppos,dest.alignment,dest.volatility);
  1602. dstrefp:=dstref;
  1603. dstrefp.direction := dir_inc;
  1604. { iregister = source }
  1605. { jregister = destination }
  1606. a_loadaddr_ref_reg(list,source,iregister);
  1607. a_loadaddr_ref_reg(list,dest,jregister);
  1608. if not (needs_unaligned(source.alignment,OS_INT) or needs_unaligned(dest.alignment,OS_INT)) then
  1609. begin
  1610. if not ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=16))) then
  1611. begin
  1612. //list.concat(tai_comment.create(strpnew('g_concatcopy tight copy loop 020+')));
  1613. helpsize := len - len mod 4;
  1614. len := len mod 4;
  1615. a_load_const_reg(list,OS_INT,(helpsize div 4)-1,hregister);
  1616. current_asmdata.getjumplabel(hl);
  1617. a_label(list,hl);
  1618. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcrefp,dstrefp));
  1619. if (current_settings.cputype in cpu_coldfire) or ((helpsize div 4)-1 > high(smallint)) then
  1620. begin
  1621. { Coldfire does not support DBRA, also it is word only }
  1622. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1623. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1624. end
  1625. else
  1626. list.concat(taicpu.op_reg_sym(A_DBRA,S_NO,hregister,hl));
  1627. end;
  1628. helpsize:=len div 4;
  1629. { move a dword x times }
  1630. for i:=1 to helpsize do
  1631. begin
  1632. dec(len,4);
  1633. if (len > 0) then
  1634. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcrefp,dstrefp))
  1635. else
  1636. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcref,dstref));
  1637. end;
  1638. { move a word }
  1639. if len>1 then
  1640. begin
  1641. dec(len,2);
  1642. if (len > 0) then
  1643. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,srcrefp,dstrefp))
  1644. else
  1645. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,srcref,dstref));
  1646. end;
  1647. { move a single byte }
  1648. if len>0 then
  1649. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,srcref,dstref));
  1650. end
  1651. else
  1652. begin
  1653. { Fast 68010 loop mode with no possible alignment problems }
  1654. //list.concat(tai_comment.create(strpnew('g_concatcopy tight byte copy loop')));
  1655. a_load_const_reg(list,OS_INT,len - 1,hregister);
  1656. current_asmdata.getjumplabel(hl);
  1657. a_label(list,hl);
  1658. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,srcrefp,dstrefp));
  1659. if (len - 1) > high(smallint) then
  1660. begin
  1661. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1662. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1663. end
  1664. else
  1665. list.concat(taicpu.op_reg_sym(A_DBRA,S_NO,hregister,hl));
  1666. end;
  1667. end;
  1668. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1669. var
  1670. hl : tasmlabel;
  1671. ai : taicpu;
  1672. cond : TAsmCond;
  1673. begin
  1674. if not(cs_check_overflow in current_settings.localswitches) then
  1675. exit;
  1676. current_asmdata.getjumplabel(hl);
  1677. if not ((def.typ=pointerdef) or
  1678. ((def.typ=orddef) and
  1679. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1680. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1681. cond:=C_VC
  1682. else
  1683. begin
  1684. { MUL/DIV always sets the overflow flag, and never the carry flag }
  1685. { Note/Fixme: This still doesn't cover the ColdFire, where none of these opcodes
  1686. set either the overflow or the carry flag. So CF must be handled in other ways. }
  1687. case taicpu(list.last).opcode of
  1688. A_MULU,A_MULS,A_DIVS,A_DIVU,A_DIVUL,A_DIVSL:
  1689. cond:=C_VC;
  1690. else
  1691. cond:=C_CC;
  1692. end;
  1693. end;
  1694. ai:=Taicpu.Op_Sym(A_Bxx,S_NO,hl);
  1695. ai.SetCondition(cond);
  1696. ai.is_jmp:=true;
  1697. list.concat(ai);
  1698. a_call_name(list,'FPC_OVERFLOW',false);
  1699. a_label(list,hl);
  1700. end;
  1701. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1702. begin
  1703. { Carl's original code used 2x MOVE instead of LINK when localsize = 0.
  1704. However, a LINK seems faster than two moves on everything from 68000
  1705. to '060, so the two move branch here was dropped. (KB) }
  1706. if not nostackframe then
  1707. begin
  1708. localsize:=align(localsize,4);
  1709. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1710. begin
  1711. if (localsize > high(smallint)) then
  1712. begin
  1713. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1714. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1715. end
  1716. else
  1717. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1718. end
  1719. else
  1720. begin
  1721. if localsize<>0 then
  1722. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1723. current_procinfo.final_localsize:=localsize;
  1724. end;
  1725. end;
  1726. end;
  1727. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1728. var
  1729. r,hregister : TRegister;
  1730. ref : TReference;
  1731. ref2: TReference;
  1732. begin
  1733. { if a subroutine is marked as non-returning, we do
  1734. not generate any exit code, so we really trust the noreturn directive
  1735. }
  1736. if po_noreturn in current_procinfo.procdef.procoptions then
  1737. exit;
  1738. if not nostackframe then
  1739. begin
  1740. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1741. begin
  1742. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1743. { if parasize is less than zero here, we probably have a cdecl function.
  1744. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1745. 68k GCC uses two different methods to free the stack, depending if the target
  1746. architecture supports RTD or not, and one does callee side, the other does
  1747. caller side free, which looks like a PITA to support. We have to figure this
  1748. out later. More info welcomed. (KB) }
  1749. if (parasize > 0) and not (current_procinfo.procdef.proccalloption in clearstack_pocalls) then
  1750. begin
  1751. if CPUM68K_HAS_RTD in cpu_capabilities[current_settings.cputype] then
  1752. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1753. else
  1754. begin
  1755. { We must pull the PC Counter from the stack, before }
  1756. { restoring the stack pointer, otherwise the PC would }
  1757. { point to nowhere! }
  1758. { Instead of doing a slow copy of the return address while trying }
  1759. { to feed it to the RTS instruction, load the PC to A1 (scratch reg) }
  1760. { then free up the stack allocated for paras, then use a JMP (A1) to }
  1761. { return to the caller with the paras freed. (KB) }
  1762. hregister:=NR_A1;
  1763. cg.a_reg_alloc(list,hregister);
  1764. reference_reset_base(ref,NR_STACK_POINTER_REG,0,ctempposinvalid,4,[]);
  1765. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1766. { instead of using a postincrement above (which also writes the }
  1767. { stackpointer reg) simply add 4 to the parasize, the instructions }
  1768. { below then take that size into account as well, so SP reg is only }
  1769. { written once (KB) }
  1770. parasize:=parasize+4;
  1771. r:=NR_SP;
  1772. { can we do a quick addition ... }
  1773. if (parasize < 9) then
  1774. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1775. else { nope ... }
  1776. begin
  1777. reference_reset_base(ref2,NR_STACK_POINTER_REG,parasize,ctempposinvalid,4,[]);
  1778. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref2,r));
  1779. end;
  1780. reference_reset_base(ref,hregister,0,ctempposinvalid,4,[]);
  1781. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1782. end;
  1783. end
  1784. else
  1785. list.concat(taicpu.op_none(A_RTS,S_NO));
  1786. end
  1787. else
  1788. begin
  1789. if parasize<>0 then
  1790. Internalerror(2020112901);
  1791. if current_procinfo.final_localsize<>0 then
  1792. list.concat(taicpu.op_const_reg(A_ADDA,S_L,current_procinfo.final_localsize,NR_STACK_POINTER_REG));
  1793. list.concat(taicpu.op_none(A_RTS,S_NO));
  1794. end;
  1795. end
  1796. else
  1797. begin
  1798. list.concat(taicpu.op_none(A_RTS,S_NO));
  1799. end;
  1800. { Routines with the poclearstack flag set use only a ret.
  1801. also routines with parasize=0 }
  1802. { TODO: figure out if these are still relevant to us (KB) }
  1803. (*
  1804. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1805. begin
  1806. { complex return values are removed from stack in C code PM }
  1807. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1808. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1809. else
  1810. list.concat(taicpu.op_none(A_RTS,S_NO));
  1811. end
  1812. else if (parasize=0) then
  1813. begin
  1814. list.concat(taicpu.op_none(A_RTS,S_NO));
  1815. end
  1816. else
  1817. *)
  1818. end;
  1819. procedure tcg68k.g_save_registers(list:TAsmList);
  1820. var
  1821. dataregs: tcpuregisterset;
  1822. addrregs: tcpuregisterset;
  1823. fpuregs: tcpuregisterset;
  1824. href : treference;
  1825. hreg : tregister;
  1826. hfreg : tregister;
  1827. size : longint;
  1828. fsize : longint;
  1829. r : integer;
  1830. regs_to_save_int,
  1831. regs_to_save_address,
  1832. regs_to_save_fpu: tcpuregisterarray;
  1833. begin
  1834. { The code generated by the section below, particularly the movem.l
  1835. instruction is known to cause an issue when compiled by some GNU
  1836. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1837. when you run into this problem, just call inherited here instead
  1838. to skip the movem.l generation. But better just use working GNU
  1839. AS version instead. (KB) }
  1840. dataregs:=[];
  1841. addrregs:=[];
  1842. fpuregs:=[];
  1843. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  1844. regs_to_save_address:=paramanager.get_saved_registers_address(current_procinfo.procdef.proccalloption);
  1845. regs_to_save_fpu:=paramanager.get_saved_registers_fpu(current_procinfo.procdef.proccalloption);
  1846. { calculate temp. size }
  1847. size:=0;
  1848. fsize:=0;
  1849. hreg:=NR_NO;
  1850. hfreg:=NR_NO;
  1851. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  1852. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  1853. begin
  1854. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  1855. inc(size,sizeof(aint));
  1856. dataregs:=dataregs + [regs_to_save_int[r]];
  1857. end;
  1858. if uses_registers(R_ADDRESSREGISTER) then
  1859. for r:=low(regs_to_save_address) to high(regs_to_save_address) do
  1860. if regs_to_save_address[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1861. begin
  1862. hreg:=newreg(R_ADDRESSREGISTER,regs_to_save_address[r],R_SUBWHOLE);
  1863. inc(size,sizeof(aint));
  1864. addrregs:=addrregs + [regs_to_save_address[r]];
  1865. end;
  1866. if uses_registers(R_FPUREGISTER) then
  1867. for r:=low(regs_to_save_fpu) to high(regs_to_save_fpu) do
  1868. if regs_to_save_fpu[r] in rg[R_FPUREGISTER].used_in_proc then
  1869. begin
  1870. hfreg:=newreg(R_FPUREGISTER,regs_to_save_fpu[r],R_SUBNONE);
  1871. inc(fsize,fpuregsize);
  1872. fpuregs:=fpuregs + [regs_to_save_fpu[r]];
  1873. end;
  1874. { 68k has no MM registers }
  1875. if uses_registers(R_MMREGISTER) then
  1876. internalerror(2014030201);
  1877. if (size+fsize) > 0 then
  1878. begin
  1879. tg.GetTemp(list,size+fsize,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1880. include(current_procinfo.flags,pi_has_saved_regs);
  1881. { Copy registers to temp }
  1882. { NOTE: virtual registers allocated here won't be translated --> no higher-level stuff. }
  1883. href:=current_procinfo.save_regs_ref;
  1884. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire+[cpu_mc68000]) then
  1885. begin
  1886. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1887. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1888. reference_reset_base(href,NR_A0,0,ctempposinvalid,sizeof(pint),[]);
  1889. end;
  1890. if size > 0 then
  1891. if size = sizeof(aint) then
  1892. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href))
  1893. else
  1894. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,[],href));
  1895. if fsize > 0 then
  1896. begin
  1897. { size is always longword aligned, while fsize is not }
  1898. inc(href.offset,size);
  1899. if fsize = fpuregsize then
  1900. list.concat(taicpu.op_reg_ref(A_FMOVE,fpuregopsize,hfreg,href))
  1901. else
  1902. list.concat(taicpu.op_regset_ref(A_FMOVEM,fpuregopsize,[],[],fpuregs,href));
  1903. end;
  1904. end;
  1905. end;
  1906. procedure tcg68k.g_restore_registers(list:TAsmList);
  1907. var
  1908. dataregs: tcpuregisterset;
  1909. addrregs: tcpuregisterset;
  1910. fpuregs : tcpuregisterset;
  1911. href : treference;
  1912. r : integer;
  1913. hreg : tregister;
  1914. hfreg : tregister;
  1915. size : longint;
  1916. fsize : longint;
  1917. regs_to_save_int,
  1918. regs_to_save_address,
  1919. regs_to_save_fpu: tcpuregisterarray;
  1920. begin
  1921. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1922. dataregs:=[];
  1923. addrregs:=[];
  1924. fpuregs:=[];
  1925. if not(pi_has_saved_regs in current_procinfo.flags) then
  1926. exit;
  1927. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  1928. regs_to_save_address:=paramanager.get_saved_registers_address(current_procinfo.procdef.proccalloption);
  1929. regs_to_save_fpu:=paramanager.get_saved_registers_fpu(current_procinfo.procdef.proccalloption);
  1930. { Copy registers from temp }
  1931. size:=0;
  1932. fsize:=0;
  1933. hreg:=NR_NO;
  1934. hfreg:=NR_NO;
  1935. for r:=low(regs_to_save_int) to high(regs_to_save_int) do
  1936. if regs_to_save_int[r] in rg[R_INTREGISTER].used_in_proc then
  1937. begin
  1938. inc(size,sizeof(aint));
  1939. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  1940. { Allocate register so the optimizer does not remove the load }
  1941. a_reg_alloc(list,hreg);
  1942. dataregs:=dataregs + [regs_to_save_int[r]];
  1943. end;
  1944. if uses_registers(R_ADDRESSREGISTER) then
  1945. for r:=low(regs_to_save_address) to high(regs_to_save_address) do
  1946. if regs_to_save_address[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1947. begin
  1948. inc(size,sizeof(aint));
  1949. hreg:=newreg(R_ADDRESSREGISTER,regs_to_save_address[r],R_SUBWHOLE);
  1950. { Allocate register so the optimizer does not remove the load }
  1951. a_reg_alloc(list,hreg);
  1952. addrregs:=addrregs + [regs_to_save_address[r]];
  1953. end;
  1954. if uses_registers(R_FPUREGISTER) then
  1955. for r:=low(regs_to_save_fpu) to high(regs_to_save_fpu) do
  1956. if regs_to_save_fpu[r] in rg[R_FPUREGISTER].used_in_proc then
  1957. begin
  1958. inc(fsize,fpuregsize);
  1959. hfreg:=newreg(R_FPUREGISTER,regs_to_save_fpu[r],R_SUBNONE);
  1960. { Allocate register so the optimizer does not remove the load }
  1961. a_reg_alloc(list,hfreg);
  1962. fpuregs:=fpuregs + [regs_to_save_fpu[r]];
  1963. end;
  1964. { 68k has no MM registers }
  1965. if uses_registers(R_MMREGISTER) then
  1966. internalerror(2014030202);
  1967. { Restore registers from temp }
  1968. href:=current_procinfo.save_regs_ref;
  1969. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire+[cpu_mc68000]) then
  1970. begin
  1971. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1972. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1973. reference_reset_base(href,NR_A0,0,ctempposinvalid,sizeof(pint),[]);
  1974. end;
  1975. if size > 0 then
  1976. if size = sizeof(aint) then
  1977. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,hreg))
  1978. else
  1979. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs,[]));
  1980. if fsize > 0 then
  1981. begin
  1982. { size is always longword aligned, while fsize is not }
  1983. inc(href.offset,size);
  1984. if fsize = fpuregsize then
  1985. list.concat(taicpu.op_ref_reg(A_FMOVE,fpuregopsize,href,hfreg))
  1986. else
  1987. list.concat(taicpu.op_ref_regset(A_FMOVEM,fpuregopsize,href,[],[],fpuregs));
  1988. end;
  1989. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1990. end;
  1991. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  1992. begin
  1993. case _newsize of
  1994. OS_S16, OS_16:
  1995. case _oldsize of
  1996. OS_S8:
  1997. begin { 8 -> 16 bit sign extend }
  1998. if (isaddressregister(reg)) then
  1999. internalerror(2014031201);
  2000. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  2001. end;
  2002. OS_8: { 8 -> 16 bit zero extend }
  2003. begin
  2004. if (current_settings.cputype in cpu_coldfire) then
  2005. { ColdFire has no ANDI.W }
  2006. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  2007. else
  2008. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  2009. end;
  2010. else
  2011. ;
  2012. end;
  2013. OS_S32, OS_32:
  2014. case _oldsize of
  2015. OS_S8:
  2016. begin { 8 -> 32 bit sign extend }
  2017. if (isaddressregister(reg)) then
  2018. internalerror(2014031202);
  2019. if (current_settings.cputype = cpu_MC68000) then
  2020. begin
  2021. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  2022. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  2023. end
  2024. else
  2025. begin
  2026. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  2027. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  2028. end;
  2029. end;
  2030. OS_8: { 8 -> 32 bit zero extend }
  2031. begin
  2032. if (isaddressregister(reg)) then
  2033. internalerror(2015031501);
  2034. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  2035. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  2036. end;
  2037. OS_S16: { 16 -> 32 bit sign extend }
  2038. begin
  2039. { address registers are sign-extended from 16->32 bit anyway
  2040. automagically on every W operation by the CPU, so this is a NOP }
  2041. if not isaddressregister(reg) then
  2042. begin
  2043. //list.concat(tai_comment.create(strpnew('sign extend word')));
  2044. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  2045. end;
  2046. end;
  2047. OS_16:
  2048. begin
  2049. if (isaddressregister(reg)) then
  2050. internalerror(2015031503);
  2051. //list.concat(tai_comment.create(strpnew('zero extend word')));
  2052. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  2053. end;
  2054. else
  2055. ;
  2056. end;
  2057. else
  2058. ;
  2059. end; { otherwise the size is already correct }
  2060. end;
  2061. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  2062. begin
  2063. sign_extend(list, _oldsize, OS_INT, reg);
  2064. end;
  2065. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2066. var
  2067. ai : taicpu;
  2068. begin
  2069. if cond=OC_None then
  2070. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  2071. else
  2072. begin
  2073. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  2074. ai.SetCondition(TOpCmp2AsmCond[cond]);
  2075. end;
  2076. ai.is_jmp:=true;
  2077. list.concat(ai);
  2078. end;
  2079. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  2080. operations on an address register. if the register is a dataregister anyway, it
  2081. just returns it untouched.}
  2082. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  2083. var
  2084. scratch_reg: TRegister;
  2085. begin
  2086. if isaddressregister(reg) then
  2087. begin
  2088. scratch_reg:=getintregister(list,OS_INT);
  2089. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg));
  2090. result:=scratch_reg;
  2091. end
  2092. else
  2093. result:=reg;
  2094. end;
  2095. { moves source register to destination register, if the two are not the same. can be used in pair
  2096. with force_to_dataregister() }
  2097. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  2098. var
  2099. instr: Taicpu;
  2100. begin
  2101. if (src <> dest) then
  2102. begin
  2103. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  2104. add_move_instruction(instr);
  2105. list.concat(instr);
  2106. end;
  2107. end;
  2108. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2109. var
  2110. hsym : tsym;
  2111. href : treference;
  2112. paraloc : Pcgparalocation;
  2113. begin
  2114. { calculate the parameter info for the procdef }
  2115. procdef.init_paraloc_info(callerside);
  2116. hsym:=tsym(procdef.parast.Find('self'));
  2117. if not(assigned(hsym) and
  2118. (hsym.typ=paravarsym)) then
  2119. internalerror(2013100702);
  2120. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2121. while paraloc<>nil do
  2122. with paraloc^ do
  2123. begin
  2124. case loc of
  2125. LOC_REGISTER:
  2126. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2127. LOC_REFERENCE:
  2128. begin
  2129. { offset in the wrapper needs to be adjusted for the stored
  2130. return address }
  2131. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),ctempposinvalid,sizeof(pint),[]);
  2132. { plain 68k could use SUBI on href directly, but this way it works on Coldfire too
  2133. and it's probably smaller code for the majority of cases (if ioffset small, the
  2134. load will use MOVEQ) (KB) }
  2135. a_load_const_reg(list,OS_ADDR,ioffset,NR_D0);
  2136. list.concat(taicpu.op_reg_ref(A_SUB,S_L,NR_D0,href));
  2137. end
  2138. else
  2139. internalerror(2013100703);
  2140. end;
  2141. paraloc:=next;
  2142. end;
  2143. end;
  2144. procedure tcg68k.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2145. begin
  2146. list.concat(taicpu.op_const_reg(A_SUB,S_L,localsize,NR_STACK_POINTER_REG));
  2147. end;
  2148. procedure tcg68k.check_register_size(size:tcgsize;reg:tregister);
  2149. begin
  2150. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  2151. internalerror(201512131);
  2152. end;
  2153. function tcg68k.optimize_const_mul_to_shift_sub_add(list: TAsmList; maxops: longint; a: tcgint; size: tcgsize; reg: TRegister): boolean;
  2154. var
  2155. i: longint;
  2156. nextpower: tcgint;
  2157. powerbit: longint;
  2158. submask: tcgint;
  2159. lastshift: longint;
  2160. hreg: tregister;
  2161. firstmov: boolean;
  2162. begin
  2163. nextpower:=nextpowerof2(a,powerbit);
  2164. submask:=nextpower-a;
  2165. result:=not ((popcnt(qword(a)) > maxops) and ((popcnt(qword(submask))+1) > maxops));
  2166. if not result then
  2167. exit;
  2168. list.concat(tai_comment.create(strpnew('optimize_const_mul_to_shift_sub_add, multiplier: '+tostr(a))));
  2169. lastshift:=0;
  2170. hreg:=getintregister(list,OS_INT);
  2171. if (popcnt(qword(a)) < (popcnt(qword(submask))+1)) then
  2172. begin
  2173. { doing additions }
  2174. firstmov:=(a and 1) = 0;
  2175. if not firstmov then
  2176. a_load_reg_reg(list,size,OS_INT,reg,hreg);
  2177. for i:=1 to bsrqword(a) do
  2178. if ((a shr i) and 1) = 1 then
  2179. begin
  2180. if firstmov then
  2181. begin
  2182. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,reg);
  2183. a_load_reg_reg(list,OS_INT,OS_INT,reg,hreg);
  2184. firstmov:=false;
  2185. end
  2186. else
  2187. begin
  2188. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,hreg);
  2189. a_op_reg_reg(list,OP_ADD,OS_INT,hreg,reg);
  2190. end;
  2191. lastshift:=i;
  2192. end;
  2193. end
  2194. else
  2195. begin
  2196. { doing subtractions }
  2197. a_load_const_reg(list,OS_INT,0,hreg);
  2198. for i:=0 to bsrqword(submask) do
  2199. if ((submask shr i) and 1) = 1 then
  2200. begin
  2201. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,reg);
  2202. a_op_reg_reg(list,OP_SUB,OS_INT,reg,hreg);
  2203. lastshift:=i;
  2204. end;
  2205. a_op_const_reg(list,OP_SHL,OS_INT,powerbit-lastshift,reg);
  2206. a_op_reg_reg(list,OP_ADD,OS_INT,hreg,reg);
  2207. end;
  2208. result:=true;
  2209. end;
  2210. {****************************************************************************}
  2211. { TCG64F68K }
  2212. {****************************************************************************}
  2213. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  2214. var
  2215. opcode : tasmop;
  2216. xopcode : tasmop;
  2217. instr : taicpu;
  2218. begin
  2219. opcode := topcg2tasmop[op];
  2220. xopcode := topcg2tasmopx[op];
  2221. case op of
  2222. OP_ADD,OP_SUB:
  2223. begin
  2224. { if one of these three registers is an address
  2225. register, we'll really get into problems! }
  2226. if isaddressregister(regdst.reglo) or
  2227. isaddressregister(regdst.reghi) or
  2228. isaddressregister(regsrc.reghi) then
  2229. internalerror(2014030101);
  2230. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  2231. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  2232. end;
  2233. OP_AND,OP_OR:
  2234. begin
  2235. { at least one of the registers must be a data register }
  2236. if (isaddressregister(regdst.reglo) and
  2237. isaddressregister(regsrc.reglo)) or
  2238. (isaddressregister(regsrc.reghi) and
  2239. isaddressregister(regdst.reghi)) then
  2240. internalerror(2014030102);
  2241. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2242. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2243. end;
  2244. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  2245. OP_IDIV,OP_DIV,
  2246. OP_IMUL,OP_MUL:
  2247. internalerror(2002081701);
  2248. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  2249. OP_SAR,OP_SHL,OP_SHR:
  2250. internalerror(2002081702);
  2251. OP_XOR:
  2252. begin
  2253. if isaddressregister(regdst.reglo) or
  2254. isaddressregister(regsrc.reglo) or
  2255. isaddressregister(regsrc.reghi) or
  2256. isaddressregister(regdst.reghi) then
  2257. internalerror(2014030103);
  2258. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2259. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2260. end;
  2261. OP_NEG,OP_NOT:
  2262. begin
  2263. if isaddressregister(regdst.reglo) or
  2264. isaddressregister(regdst.reghi) then
  2265. internalerror(2014030104);
  2266. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  2267. cg.add_move_instruction(instr);
  2268. list.concat(instr);
  2269. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  2270. cg.add_move_instruction(instr);
  2271. list.concat(instr);
  2272. if (op = OP_NOT) then
  2273. xopcode:=opcode;
  2274. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  2275. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  2276. end;
  2277. else
  2278. ;
  2279. end; { end case }
  2280. end;
  2281. procedure tcg64f68k.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  2282. var
  2283. href : treference;
  2284. hreg: tregister;
  2285. begin
  2286. case op of
  2287. OP_NEG,OP_NOT:
  2288. begin
  2289. a_load64_ref_reg(list,ref,reg);
  2290. a_op64_reg_reg(list,op,size,reg,reg);
  2291. end;
  2292. OP_AND,OP_OR:
  2293. begin
  2294. href:=ref;
  2295. tcg68k(cg).fixref(list,href,false);
  2296. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reghi));
  2297. inc(href.offset,4);
  2298. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reglo));
  2299. end;
  2300. OP_ADD,OP_SUB:
  2301. begin
  2302. href:=ref;
  2303. tcg68k(cg).fixref(list,href,false);
  2304. hreg:=cg.getintregister(list,OS_32);
  2305. cg.a_load_ref_reg(list,OS_32,OS_32,href,hreg);
  2306. inc(href.offset,4);
  2307. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reglo));
  2308. list.concat(taicpu.op_reg_reg(topcg2tasmopx[op],S_L,hreg,reg.reghi));
  2309. end;
  2310. else
  2311. { XOR does not allow reference for source; ADD/SUB do not allow reference for
  2312. high dword, although low dword can still be handled directly. }
  2313. inherited a_op64_ref_reg(list,op,size,ref,reg);
  2314. end;
  2315. end;
  2316. procedure tcg64f68k.a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const ref : treference);
  2317. var
  2318. href: treference;
  2319. hreg: tregister;
  2320. begin
  2321. case op of
  2322. OP_AND,OP_OR,OP_XOR:
  2323. begin
  2324. href:=ref;
  2325. tcg68k(cg).fixref(list,href,false);
  2326. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reghi,href));
  2327. inc(href.offset,4);
  2328. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reglo,href));
  2329. end;
  2330. OP_ADD,OP_SUB:
  2331. begin
  2332. href:=ref;
  2333. tcg68k(cg).fixref(list,href,false);
  2334. hreg:=cg.getintregister(list,OS_32);
  2335. cg.a_load_ref_reg(list,OS_32,OS_32,href,hreg);
  2336. inc(href.offset,4);
  2337. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reglo,href));
  2338. list.concat(taicpu.op_reg_reg(topcg2tasmopx[op],S_L,reg.reghi,hreg));
  2339. dec(href.offset,4);
  2340. cg.a_load_reg_ref(list,OS_32,OS_32,hreg,href);
  2341. end;
  2342. else
  2343. inherited a_op64_reg_ref(list,op,size,reg,ref);
  2344. end;
  2345. end;
  2346. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  2347. var
  2348. lowvalue : cardinal;
  2349. highvalue : cardinal;
  2350. opcode : tasmop;
  2351. xopcode : tasmop;
  2352. hreg : tregister;
  2353. begin
  2354. { is it optimized out ? }
  2355. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  2356. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  2357. exit; }
  2358. lowvalue := cardinal(value);
  2359. highvalue := value shr 32;
  2360. opcode := topcg2tasmop[op];
  2361. xopcode := topcg2tasmopx[op];
  2362. { the destination registers must be data registers }
  2363. if isaddressregister(regdst.reglo) or
  2364. isaddressregister(regdst.reghi) then
  2365. internalerror(2014030105);
  2366. case op of
  2367. OP_ADD,OP_SUB:
  2368. begin
  2369. hreg:=cg.getintregister(list,OS_INT);
  2370. { cg.a_load_const_reg provides optimized loading to register for special cases }
  2371. cg.a_load_const_reg(list,OS_S32,tcgint(highvalue),hreg);
  2372. { don't use cg.a_op_const_reg() here, because a possible optimized
  2373. ADDQ/SUBQ wouldn't set the eXtend bit }
  2374. list.concat(taicpu.op_const_reg(opcode,S_L,tcgint(lowvalue),regdst.reglo));
  2375. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  2376. end;
  2377. OP_AND,OP_OR,OP_XOR:
  2378. begin
  2379. cg.a_op_const_reg(list,op,OS_S32,tcgint(lowvalue),regdst.reglo);
  2380. cg.a_op_const_reg(list,op,OS_S32,tcgint(highvalue),regdst.reghi);
  2381. end;
  2382. { this is handled in 1st pass for 32-bit cpus (helper call) }
  2383. OP_IDIV,OP_DIV,
  2384. OP_IMUL,OP_MUL:
  2385. internalerror(2002081703);
  2386. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  2387. OP_SAR,OP_SHL,OP_SHR:
  2388. internalerror(2002081704);
  2389. { these should have been handled already by earlier passes }
  2390. OP_NOT,OP_NEG:
  2391. internalerror(2012110403);
  2392. else
  2393. ;
  2394. end; { end case }
  2395. end;
  2396. procedure tcg64f68k.a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);
  2397. var
  2398. tmpref: treference;
  2399. begin
  2400. tmpref:=ref;
  2401. tcg68k(cg).fixref(list,tmpref,false);
  2402. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reghi,tmpref);
  2403. inc(tmpref.offset,4);
  2404. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reglo,tmpref);
  2405. end;
  2406. procedure tcg64f68k.a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);
  2407. var
  2408. tmpref: treference;
  2409. begin
  2410. { do not allow 64bit values to be loaded to address registers }
  2411. if isaddressregister(reg.reglo) or
  2412. isaddressregister(reg.reghi) then
  2413. internalerror(2016050501);
  2414. tmpref:=ref;
  2415. tcg68k(cg).fixref(list,tmpref,false);
  2416. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reghi);
  2417. inc(tmpref.offset,4);
  2418. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reglo);
  2419. end;
  2420. procedure create_codegen;
  2421. begin
  2422. cg := tcg68k.create;
  2423. cg64 :=tcg64f68k.create;
  2424. end;
  2425. end.