cgcpu.pas 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  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_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  41. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  42. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);override;
  43. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  44. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  45. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  46. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  47. procedure a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);override;
  48. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister); override;
  49. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  50. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  51. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); override;
  52. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister; l : tasmlabel);override;
  53. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel); override;
  54. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  55. procedure a_jmp_name(list : TAsmList;const s : string); override;
  56. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  57. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  58. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  59. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  60. { generates overflow checking code for a node }
  61. procedure g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef); override;
  62. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  63. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  64. procedure g_save_registers(list:TAsmList);override;
  65. procedure g_restore_registers(list:TAsmList);override;
  66. procedure g_adjust_self_value(list:TAsmList;procdef:tprocdef;ioffset:tcgint);override;
  67. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  68. { # Sign or zero extend the register to a full 32-bit value.
  69. The new value is left in the same register.
  70. }
  71. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  72. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  73. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  74. function fixref(list: TAsmList; var ref: treference): boolean;
  75. protected
  76. procedure call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  77. procedure call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  78. private
  79. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  80. function force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  81. procedure move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  82. end;
  83. tcg64f68k = class(tcg64f32)
  84. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG; size: tcgsize; regsrc,regdst : tregister64);override;
  85. procedure a_op64_const_reg(list : TAsmList;op:TOpCG; size: tcgsize; value : int64;regdst : tregister64);override;
  86. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;
  87. end;
  88. { This function returns true if the reference+offset is valid.
  89. Otherwise extra code must be generated to solve the reference.
  90. On the m68k, this verifies that the reference is valid
  91. (e.g : if index register is used, then the max displacement
  92. is 256 bytes, if only base is used, then max displacement
  93. is 32K
  94. }
  95. function isvalidrefoffset(const ref: treference): boolean;
  96. function isvalidreference(const ref: treference): boolean;
  97. procedure create_codegen;
  98. implementation
  99. uses
  100. globals,verbose,systems,cutils,
  101. symsym,symtable,defutil,paramgr,procinfo,
  102. rgobj,tgobj,rgcpu,fmodule;
  103. const
  104. { opcode table lookup }
  105. topcg2tasmop: Array[topcg] of tasmop =
  106. (
  107. A_NONE,
  108. A_MOVE,
  109. A_ADD,
  110. A_AND,
  111. A_DIVU,
  112. A_DIVS,
  113. A_MULS,
  114. A_MULU,
  115. A_NEG,
  116. A_NOT,
  117. A_OR,
  118. A_ASR,
  119. A_LSL,
  120. A_LSR,
  121. A_SUB,
  122. A_EOR,
  123. A_ROL,
  124. A_ROR
  125. );
  126. { opcode with extend bits table lookup, used by 64bit cg }
  127. topcg2tasmopx: Array[topcg] of tasmop =
  128. (
  129. A_NONE,
  130. A_NONE,
  131. A_ADDX,
  132. A_NONE,
  133. A_NONE,
  134. A_NONE,
  135. A_NONE,
  136. A_NONE,
  137. A_NEGX,
  138. A_NONE,
  139. A_NONE,
  140. A_NONE,
  141. A_NONE,
  142. A_NONE,
  143. A_SUBX,
  144. A_NONE,
  145. A_NONE,
  146. A_NONE
  147. );
  148. TOpCmp2AsmCond: Array[topcmp] of TAsmCond =
  149. (
  150. C_NONE,
  151. C_EQ,
  152. C_GT,
  153. C_LT,
  154. C_GE,
  155. C_LE,
  156. C_NE,
  157. C_LS,
  158. C_CS,
  159. C_CC,
  160. C_HI
  161. );
  162. function isvalidreference(const ref: treference): boolean;
  163. begin
  164. isvalidreference:=isvalidrefoffset(ref) and
  165. { don't try to generate addressing with symbol and base reg and offset
  166. it might fail in linking stage if the symbol is more than 32k away (KB) }
  167. not (assigned(ref.symbol) and (ref.base <> NR_NO) and (ref.offset <> 0)) and
  168. { coldfire and 68000 cannot handle non-addressregs as bases }
  169. not ((current_settings.cputype in cpu_coldfire+[cpu_mc68000]) and
  170. not isaddressregister(ref.base));
  171. end;
  172. function isvalidrefoffset(const ref: treference): boolean;
  173. begin
  174. isvalidrefoffset := true;
  175. if ref.index <> NR_NO then
  176. begin
  177. // if ref.base <> NR_NO then
  178. // internalerror(2002081401);
  179. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  180. isvalidrefoffset := false
  181. end
  182. else
  183. begin
  184. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  185. isvalidrefoffset := false;
  186. end;
  187. end;
  188. {****************************************************************************}
  189. { TCG68K }
  190. {****************************************************************************}
  191. function use_push(const cgpara:tcgpara):boolean;
  192. begin
  193. result:=(not paramanager.use_fixed_stack) and
  194. assigned(cgpara.location) and
  195. (cgpara.location^.loc=LOC_REFERENCE) and
  196. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  197. end;
  198. procedure tcg68k.init_register_allocators;
  199. var
  200. reg: TSuperRegister;
  201. address_regs: array of TSuperRegister;
  202. begin
  203. inherited init_register_allocators;
  204. address_regs:=nil;
  205. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  206. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7],
  207. first_int_imreg,[]);
  208. { set up the array of address registers to use }
  209. for reg:=RS_A0 to RS_A6 do
  210. begin
  211. { don't hardwire the frame pointer register, because it can vary between target OS }
  212. if assigned(current_procinfo) and (current_procinfo.framepointer = NR_FRAME_POINTER_REG)
  213. and (reg = RS_FRAME_POINTER_REG) then
  214. continue;
  215. setlength(address_regs,length(address_regs)+1);
  216. address_regs[length(address_regs)-1]:=reg;
  217. end;
  218. rg[R_ADDRESSREGISTER]:=trgcpu.create(R_ADDRESSREGISTER,R_SUBWHOLE,
  219. address_regs, first_addr_imreg, []);
  220. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  221. [RS_FP0,RS_FP1,RS_FP2,RS_FP3,RS_FP4,RS_FP5,RS_FP6,RS_FP7],
  222. first_fpu_imreg,[]);
  223. end;
  224. procedure tcg68k.done_register_allocators;
  225. begin
  226. rg[R_INTREGISTER].free;
  227. rg[R_FPUREGISTER].free;
  228. rg[R_ADDRESSREGISTER].free;
  229. inherited done_register_allocators;
  230. end;
  231. procedure tcg68k.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  232. var
  233. pushsize : tcgsize;
  234. ref : treference;
  235. begin
  236. { it's probably necessary to port this from x86 later, or provide an m68k solution (KB) }
  237. { TODO: FIX ME! check_register_size()}
  238. // check_register_size(size,r);
  239. if use_push(cgpara) then
  240. begin
  241. cgpara.check_simple_location;
  242. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  243. pushsize:=cgpara.location^.size
  244. else
  245. pushsize:=int_cgsize(cgpara.alignment);
  246. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  247. ref.direction := dir_dec;
  248. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize),ref));
  249. end
  250. else
  251. inherited a_load_reg_cgpara(list,size,r,cgpara);
  252. end;
  253. procedure tcg68k.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  254. var
  255. pushsize : tcgsize;
  256. ref : treference;
  257. begin
  258. if use_push(cgpara) then
  259. begin
  260. cgpara.check_simple_location;
  261. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  262. pushsize:=cgpara.location^.size
  263. else
  264. pushsize:=int_cgsize(cgpara.alignment);
  265. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  266. ref.direction := dir_dec;
  267. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[pushsize],a,ref));
  268. end
  269. else
  270. inherited a_load_const_cgpara(list,size,a,cgpara);
  271. end;
  272. procedure tcg68k.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  273. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  274. var
  275. pushsize : tcgsize;
  276. tmpreg : tregister;
  277. href : treference;
  278. ref : treference;
  279. begin
  280. if not assigned(paraloc) then
  281. exit;
  282. { TODO: FIX ME!!! this also triggers location bug }
  283. {if (paraloc^.loc<>LOC_REFERENCE) or
  284. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  285. (tcgsize2size[paraloc^.size]>sizeof(tcgint)) then
  286. internalerror(200501162);}
  287. { Pushes are needed in reverse order, add the size of the
  288. current location to the offset where to load from. This
  289. prevents wrong calculations for the last location when
  290. the size is not a power of 2 }
  291. if assigned(paraloc^.next) then
  292. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  293. { Push the data starting at ofs }
  294. href:=r;
  295. inc(href.offset,ofs);
  296. fixref(list,href);
  297. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  298. pushsize:=paraloc^.size
  299. else
  300. pushsize:=int_cgsize(cgpara.alignment);
  301. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, tcgsize2size[pushsize]);
  302. ref.direction := dir_dec;
  303. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  304. begin
  305. tmpreg:=getintregister(list,pushsize);
  306. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  307. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],tmpreg,ref));
  308. end
  309. else
  310. list.concat(taicpu.op_ref_ref(A_MOVE,tcgsize2opsize[pushsize],href,ref));
  311. end;
  312. var
  313. len : tcgint;
  314. href : treference;
  315. begin
  316. { cgpara.size=OS_NO requires a copy on the stack }
  317. if use_push(cgpara) then
  318. begin
  319. { Record copy? }
  320. if (cgpara.size in [OS_NO,OS_F64]) or (size=OS_NO) then
  321. begin
  322. cgpara.check_simple_location;
  323. len:=align(cgpara.intsize,cgpara.alignment);
  324. g_stackpointer_alloc(list,len);
  325. reference_reset_base(href,NR_STACK_POINTER_REG,0,cgpara.alignment);
  326. g_concatcopy(list,r,href,len);
  327. end
  328. else
  329. begin
  330. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  331. internalerror(200501161);
  332. { We need to push the data in reverse order,
  333. therefor we use a recursive algorithm }
  334. pushdata(cgpara.location,0);
  335. end
  336. end
  337. else
  338. inherited a_load_ref_cgpara(list,size,r,cgpara);
  339. end;
  340. procedure tcg68k.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  341. var
  342. tmpref : treference;
  343. begin
  344. { 68k always passes arguments on the stack }
  345. if use_push(cgpara) then
  346. begin
  347. //list.concat(tai_comment.create(strpnew('a_loadaddr_ref_cgpara: PEA')));
  348. cgpara.check_simple_location;
  349. tmpref:=r;
  350. fixref(list,tmpref);
  351. list.concat(taicpu.op_ref(A_PEA,S_NO,tmpref));
  352. end
  353. else
  354. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  355. end;
  356. function tcg68k.fixref(list: TAsmList; var ref: treference): boolean;
  357. var
  358. hreg,idxreg : tregister;
  359. href : treference;
  360. instr : taicpu;
  361. scale : aint;
  362. begin
  363. result:=false;
  364. { The MC68020+ has extended
  365. addressing capabilities with a 32-bit
  366. displacement.
  367. }
  368. { first ensure that base is an address register }
  369. if ((ref.base<>NR_NO) and (ref.index<>NR_NO)) and
  370. (not isaddressregister(ref.base) and isaddressregister(ref.index)) and
  371. (ref.scalefactor < 2) then
  372. begin
  373. { if we have both base and index registers, but base is data and index
  374. is address, we can just swap them, as FPC always uses long index.
  375. but we can only do this, if the index has no scalefactor }
  376. hreg:=ref.base;
  377. ref.base:=ref.index;
  378. ref.index:=hreg;
  379. //list.concat(tai_comment.create(strpnew('fixref: base and index swapped')));
  380. end;
  381. if (not assigned (ref.symbol) and (current_settings.cputype<>cpu_MC68000)) and
  382. (ref.base<>NR_NO) and not isaddressregister(ref.base) then
  383. begin
  384. hreg:=getaddressregister(list);
  385. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  386. add_move_instruction(instr);
  387. list.concat(instr);
  388. fixref:=true;
  389. ref.base:=hreg;
  390. end;
  391. if (current_settings.cputype=cpu_MC68020) then
  392. exit;
  393. { ToDo: check which constraints of Coldfire also apply to MC68000 }
  394. case current_settings.cputype of
  395. cpu_MC68000:
  396. begin
  397. if (ref.base<>NR_NO) then
  398. begin
  399. if (ref.index<>NR_NO) and assigned(ref.symbol) then
  400. begin
  401. hreg:=getaddressregister(list);
  402. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  403. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.index,hreg));
  404. ref.index:=NR_NO;
  405. ref.base:=hreg;
  406. end;
  407. { base + reg }
  408. if ref.index <> NR_NO then
  409. begin
  410. { base + reg + offset }
  411. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  412. begin
  413. hreg:=getaddressregister(list);
  414. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  415. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  416. fixref:=true;
  417. ref.offset:=0;
  418. ref.base:=hreg;
  419. exit;
  420. end;
  421. end
  422. else
  423. { base + offset }
  424. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  425. begin
  426. hreg:=getaddressregister(list);
  427. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  428. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  429. fixref:=true;
  430. ref.offset:=0;
  431. ref.base:=hreg;
  432. exit;
  433. end;
  434. if assigned(ref.symbol) then
  435. begin
  436. hreg:=getaddressregister(list);
  437. idxreg:=ref.base;
  438. ref.base:=NR_NO;
  439. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  440. reference_reset_base(ref,hreg,0,ref.alignment);
  441. fixref:=true;
  442. ref.index:=idxreg;
  443. end
  444. else if not isaddressregister(ref.base) then
  445. begin
  446. hreg:=getaddressregister(list);
  447. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  448. //add_move_instruction(instr);
  449. list.concat(instr);
  450. fixref:=true;
  451. ref.base:=hreg;
  452. end;
  453. end
  454. else
  455. { Note: symbol -> ref would be supported as long as ref does not
  456. contain a offset or index... (maybe something for the
  457. optimizer) }
  458. if Assigned(ref.symbol) and (ref.index<>NR_NO) then
  459. begin
  460. hreg:=cg.getaddressregister(list);
  461. idxreg:=ref.index;
  462. ref.index:=NR_NO;
  463. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  464. reference_reset_base(ref,hreg,0,ref.alignment);
  465. ref.index:=idxreg;
  466. fixref:=true;
  467. end;
  468. end;
  469. cpu_isa_a,
  470. cpu_isa_a_p,
  471. cpu_isa_b,
  472. cpu_isa_c:
  473. begin
  474. if (ref.base<>NR_NO) then
  475. begin
  476. if assigned(ref.symbol) then
  477. begin
  478. //list.concat(tai_comment.create(strpnew('fixref: symbol')));
  479. hreg:=cg.getaddressregister(list);
  480. reference_reset_symbol(href,ref.symbol,ref.offset,ref.alignment);
  481. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  482. if ref.index<>NR_NO then
  483. begin
  484. { fold the symbol + offset into the base, not the base into the index,
  485. because that might screw up the scalefactor of the reference }
  486. //list.concat(tai_comment.create(strpnew('fixref: symbol + offset (index + base)')));
  487. idxreg:=getaddressregister(list);
  488. reference_reset_base(href,ref.base,0,ref.alignment);
  489. href.index:=hreg;
  490. hreg:=getaddressregister(list);
  491. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  492. ref.base:=hreg;
  493. end
  494. else
  495. ref.index:=hreg;
  496. ref.offset:=0;
  497. ref.symbol:=nil;
  498. fixref:=true;
  499. end
  500. else
  501. { base + reg }
  502. if ref.index <> NR_NO then
  503. begin
  504. { base + reg + offset }
  505. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  506. begin
  507. hreg:=getaddressregister(list);
  508. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  509. begin
  510. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  511. //add_move_instruction(instr);
  512. list.concat(instr);
  513. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  514. end
  515. else
  516. begin
  517. //list.concat(tai_comment.create(strpnew('fixref: base + reg + offset lea')));
  518. reference_reset_base(href,ref.base,ref.offset,ref.alignment);
  519. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,href,hreg));
  520. end;
  521. fixref:=true;
  522. ref.base:=hreg;
  523. ref.offset:=0;
  524. exit;
  525. end;
  526. end
  527. else
  528. { base + offset }
  529. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  530. begin
  531. hreg:=getaddressregister(list);
  532. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  533. //add_move_instruction(instr);
  534. list.concat(instr);
  535. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  536. fixref:=true;
  537. ref.offset:=0;
  538. ref.base:=hreg;
  539. exit;
  540. end;
  541. end
  542. else
  543. { Note: symbol -> ref would be supported as long as ref does not
  544. contain a offset or index... (maybe something for the
  545. optimizer) }
  546. if Assigned(ref.symbol) {and (ref.index<>NR_NO)} then
  547. begin
  548. hreg:=cg.getaddressregister(list);
  549. idxreg:=ref.index;
  550. scale:=ref.scalefactor;
  551. ref.index:=NR_NO;
  552. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  553. reference_reset_base(ref,hreg,0,ref.alignment);
  554. ref.index:=idxreg;
  555. ref.scalefactor:=scale;
  556. fixref:=true;
  557. end;
  558. end;
  559. end;
  560. end;
  561. procedure tcg68k.call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  562. var
  563. paraloc1,paraloc2,paraloc3 : tcgpara;
  564. pd : tprocdef;
  565. begin
  566. pd:=search_system_proc(name);
  567. paraloc1.init;
  568. paraloc2.init;
  569. paraloc3.init;
  570. paramanager.getintparaloc(pd,1,paraloc1);
  571. paramanager.getintparaloc(pd,2,paraloc2);
  572. paramanager.getintparaloc(pd,3,paraloc3);
  573. a_load_const_cgpara(list,OS_8,0,paraloc3);
  574. a_load_const_cgpara(list,size,a,paraloc2);
  575. a_load_reg_cgpara(list,OS_32,reg,paraloc1);
  576. paramanager.freecgpara(list,paraloc3);
  577. paramanager.freecgpara(list,paraloc2);
  578. paramanager.freecgpara(list,paraloc1);
  579. alloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  580. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  581. a_call_name(list,name,false);
  582. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  583. dealloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  584. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  585. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg);
  586. paraloc3.done;
  587. paraloc2.done;
  588. paraloc1.done;
  589. end;
  590. procedure tcg68k.call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  591. var
  592. paraloc1,paraloc2,paraloc3 : tcgpara;
  593. pd : tprocdef;
  594. begin
  595. pd:=search_system_proc(name);
  596. paraloc1.init;
  597. paraloc2.init;
  598. paraloc3.init;
  599. paramanager.getintparaloc(pd,1,paraloc1);
  600. paramanager.getintparaloc(pd,2,paraloc2);
  601. paramanager.getintparaloc(pd,3,paraloc3);
  602. a_load_const_cgpara(list,OS_8,0,paraloc3);
  603. a_load_reg_cgpara(list,OS_32,reg1,paraloc2);
  604. a_load_reg_cgpara(list,OS_32,reg2,paraloc1);
  605. paramanager.freecgpara(list,paraloc3);
  606. paramanager.freecgpara(list,paraloc2);
  607. paramanager.freecgpara(list,paraloc1);
  608. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  609. alloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  610. a_call_name(list,name,false);
  611. dealloccpuregisters(list,R_ADDRESSREGISTER,paramanager.get_volatile_registers_address(pocall_default));
  612. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  613. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  614. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg2);
  615. paraloc3.done;
  616. paraloc2.done;
  617. paraloc1.done;
  618. end;
  619. procedure tcg68k.a_call_name(list : TAsmList;const s : string; weak: boolean);
  620. var
  621. sym: tasmsymbol;
  622. begin
  623. if not(weak) then
  624. sym:=current_asmdata.RefAsmSymbol(s)
  625. else
  626. sym:=current_asmdata.WeakRefAsmSymbol(s);
  627. list.concat(taicpu.op_sym(A_JSR,S_NO,sym));
  628. end;
  629. procedure tcg68k.a_call_reg(list : TAsmList;reg: tregister);
  630. var
  631. tmpref : treference;
  632. tmpreg : tregister;
  633. instr : taicpu;
  634. begin
  635. if isaddressregister(reg) then
  636. begin
  637. { if we have an address register, we can jump to the address directly }
  638. reference_reset_base(tmpref,reg,0,4);
  639. end
  640. else
  641. begin
  642. { if we have a data register, we need to move it to an address register first }
  643. tmpreg:=getaddressregister(list);
  644. reference_reset_base(tmpref,tmpreg,0,4);
  645. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,tmpreg);
  646. add_move_instruction(instr);
  647. list.concat(instr);
  648. end;
  649. list.concat(taicpu.op_ref(A_JSR,S_NO,tmpref));
  650. end;
  651. procedure tcg68k.a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);
  652. var
  653. opsize: topsize;
  654. begin
  655. opsize:=tcgsize2opsize[size];
  656. if isaddressregister(register) then
  657. begin
  658. { an m68k manual I have recommends SUB Ax,Ax to be used instead of CLR for address regs }
  659. if a = 0 then
  660. list.concat(taicpu.op_reg_reg(A_SUB,S_L,register,register))
  661. else
  662. { ISA B/C Coldfire has MOV3Q which can move -1 or 1..7 to any reg }
  663. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  664. ((longint(a) = -1) or ((longint(a) > 0) and (longint(a) < 8))) then
  665. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  666. else
  667. { We don't have to specify the size here, the assembler will decide the size of
  668. the operand it needs. If this ends up as a MOVEA.W, that will sign extend the
  669. value in the dest. reg to full 32 bits (specific to Ax regs only) }
  670. list.concat(taicpu.op_const_reg(A_MOVEA,S_NO,longint(a),register));
  671. end
  672. else
  673. if a = 0 then
  674. list.concat(taicpu.op_reg(A_CLR,S_L,register))
  675. else
  676. begin
  677. { Prefer MOV3Q if applicable, it allows replacement spilling for register }
  678. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  679. ((longint(a)=-1) or ((longint(a)>0) and (longint(a)<8))) then
  680. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  681. else if (longint(a) >= low(shortint)) and (longint(a) <= high(shortint)) then
  682. list.concat(taicpu.op_const_reg(A_MOVEQ,S_L,longint(a),register))
  683. else
  684. begin
  685. { ISA B/C Coldfire has sign extend/zero extend moves }
  686. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  687. (size in [OS_16, OS_8, OS_S16, OS_S8]) and
  688. ((longint(a) >= low(smallint)) and (longint(a) <= high(smallint))) then
  689. begin
  690. if size in [OS_16, OS_8] then
  691. list.concat(taicpu.op_const_reg(A_MVZ,opsize,longint(a),register))
  692. else
  693. list.concat(taicpu.op_const_reg(A_MVS,opsize,longint(a),register));
  694. end
  695. else
  696. begin
  697. { clear the register first, for unsigned and positive values, so
  698. we don't need to zero extend after }
  699. if (size in [OS_16,OS_8]) or
  700. ((size in [OS_S16,OS_S8]) and (a > 0)) then
  701. list.concat(taicpu.op_reg(A_CLR,S_L,register));
  702. list.concat(taicpu.op_const_reg(A_MOVE,opsize,longint(a),register));
  703. { only sign extend if we need to, zero extension is not necessary because the CLR.L above }
  704. if (size in [OS_S16,OS_S8]) and (a < 0) then
  705. sign_extend(list,size,register);
  706. end;
  707. end;
  708. end;
  709. end;
  710. procedure tcg68k.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  711. var
  712. hreg : tregister;
  713. href : treference;
  714. begin
  715. a:=longint(a);
  716. href:=ref;
  717. fixref(list,href);
  718. if (a=0) and not (current_settings.cputype = cpu_mc68000) then
  719. list.concat(taicpu.op_ref(A_CLR,tcgsize2opsize[tosize],href))
  720. else if (tcgsize2opsize[tosize]=S_L) and
  721. (current_settings.cputype in [cpu_isa_b,cpu_isa_c]) and
  722. ((a=-1) or ((a>0) and (a<8))) then
  723. list.concat(taicpu.op_const_ref(A_MOV3Q,S_L,a,href))
  724. { for coldfire we need to go through a temporary register if we have a
  725. offset, index or symbol given }
  726. else if (current_settings.cputype in cpu_coldfire) and
  727. (
  728. (href.offset<>0) or
  729. { TODO : check whether we really need this second condition }
  730. (href.index<>NR_NO) or
  731. assigned(href.symbol)
  732. ) then
  733. begin
  734. hreg:=getintregister(list,tosize);
  735. a_load_const_reg(list,tosize,a,hreg);
  736. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  737. end
  738. else
  739. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[tosize],longint(a),href));
  740. end;
  741. procedure tcg68k.a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  742. var
  743. href : treference;
  744. begin
  745. href := ref;
  746. fixref(list,href);
  747. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  748. a_load_reg_reg(list,fromsize,tosize,register,register);
  749. { move to destination reference }
  750. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],register,href));
  751. end;
  752. procedure tcg68k.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  753. var
  754. aref: treference;
  755. bref: treference;
  756. tmpref : treference;
  757. dofix : boolean;
  758. hreg: TRegister;
  759. begin
  760. aref := sref;
  761. bref := dref;
  762. fixref(list,aref);
  763. fixref(list,bref);
  764. if TCGSize2OpSize[fromsize]<>TCGSize2OpSize[tosize] then
  765. begin
  766. { if we need to change the size then always use a temporary
  767. register }
  768. hreg:=getintregister(list,fromsize);
  769. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],aref,hreg));
  770. sign_extend(list,fromsize,tosize,hreg);
  771. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,bref));
  772. exit;
  773. end;
  774. { Coldfire dislikes certain move combinations }
  775. if current_settings.cputype in cpu_coldfire then
  776. begin
  777. { TODO : move.b/w only allowed in newer coldfires... (ISA_B+) }
  778. dofix:=false;
  779. if { (d16,Ax) and (d8,Ax,Xi) }
  780. (
  781. (aref.base<>NR_NO) and
  782. (
  783. (aref.index<>NR_NO) or
  784. (aref.offset<>0)
  785. )
  786. ) or
  787. { (xxx) }
  788. assigned(aref.symbol) then
  789. begin
  790. if aref.index<>NR_NO then
  791. begin
  792. dofix:={ (d16,Ax) and (d8,Ax,Xi) }
  793. (
  794. (bref.base<>NR_NO) and
  795. (
  796. (bref.index<>NR_NO) or
  797. (bref.offset<>0)
  798. )
  799. ) or
  800. { (xxx) }
  801. assigned(bref.symbol);
  802. end
  803. else
  804. { offset <> 0, but no index }
  805. begin
  806. dofix:={ (d8,Ax,Xi) }
  807. (
  808. (bref.base<>NR_NO) and
  809. (bref.index<>NR_NO)
  810. ) or
  811. { (xxx) }
  812. assigned(bref.symbol);
  813. end;
  814. end;
  815. if dofix then
  816. begin
  817. hreg:=getaddressregister(list);
  818. reference_reset_base(tmpref,hreg,0,0);
  819. list.concat(taicpu.op_ref_reg(A_LEA,S_L,aref,hreg));
  820. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],tmpref,bref));
  821. exit;
  822. end;
  823. end;
  824. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],aref,bref));
  825. end;
  826. procedure tcg68k.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  827. var
  828. instr : taicpu;
  829. begin
  830. { move to destination register }
  831. if (reg1<>reg2) then
  832. begin
  833. instr:=taicpu.op_reg_reg(A_MOVE,TCGSize2OpSize[fromsize],reg1,reg2);
  834. add_move_instruction(instr);
  835. list.concat(instr);
  836. end;
  837. sign_extend(list, fromsize, reg2);
  838. end;
  839. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  840. var
  841. href : treference;
  842. size : tcgsize;
  843. begin
  844. href:=ref;
  845. fixref(list,href);
  846. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  847. size:=fromsize
  848. else
  849. size:=tosize;
  850. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[size],href,register));
  851. { extend the value in the register }
  852. sign_extend(list, size, register);
  853. end;
  854. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  855. var
  856. href : treference;
  857. hreg : tregister;
  858. begin
  859. href:=ref;
  860. fixref(list, href);
  861. if not isaddressregister(r) then
  862. begin
  863. hreg:=getaddressregister(list);
  864. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  865. a_load_reg_reg(list, OS_ADDR, OS_ADDR, hreg, r);
  866. end
  867. else
  868. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  869. end;
  870. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  871. var
  872. instr : taicpu;
  873. begin
  874. { in emulation mode, only 32-bit single is supported }
  875. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  876. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg1,reg2)
  877. else
  878. instr:=taicpu.op_reg_reg(A_FMOVE,tcgsize2opsize[tosize],reg1,reg2);
  879. add_move_instruction(instr);
  880. list.concat(instr);
  881. end;
  882. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  883. var
  884. opsize : topsize;
  885. href : treference;
  886. begin
  887. opsize := tcgsize2opsize[fromsize];
  888. { extended is not supported, since it is not available on Coldfire }
  889. if opsize = S_FX then
  890. internalerror(20020729);
  891. href := ref;
  892. fixref(list,href);
  893. { in emulation mode, only 32-bit single is supported }
  894. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  895. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,reg))
  896. else
  897. begin
  898. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  899. if (tosize < fromsize) then
  900. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  901. end;
  902. end;
  903. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  904. var
  905. opsize : topsize;
  906. begin
  907. opsize := tcgsize2opsize[tosize];
  908. { extended is not supported, since it is not available on Coldfire }
  909. if opsize = S_FX then
  910. internalerror(20020729);
  911. { in emulation mode, only 32-bit single is supported }
  912. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  913. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,reg, ref))
  914. else
  915. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg, ref));
  916. end;
  917. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  918. begin
  919. case cgpara.location^.loc of
  920. LOC_REFERENCE,LOC_CREFERENCE:
  921. begin
  922. case size of
  923. OS_F64:
  924. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  925. OS_F32:
  926. a_load_ref_cgpara(list,size,ref,cgpara);
  927. else
  928. internalerror(2013021201);
  929. end;
  930. end;
  931. else
  932. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  933. end;
  934. end;
  935. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  936. var
  937. scratch_reg : tregister;
  938. scratch_reg2: tregister;
  939. opcode : tasmop;
  940. begin
  941. optimize_op_const(size, op, a);
  942. opcode := topcg2tasmop[op];
  943. case op of
  944. OP_NONE :
  945. begin
  946. { Opcode is optimized away }
  947. end;
  948. OP_MOVE :
  949. begin
  950. { Optimized, replaced with a simple load }
  951. a_load_const_reg(list,size,a,reg);
  952. end;
  953. OP_ADD,
  954. OP_SUB:
  955. begin
  956. { add/sub works the same way, so have it unified here }
  957. if (a >= 1) and (a <= 8) then
  958. if (op = OP_ADD) then
  959. opcode:=A_ADDQ
  960. else
  961. opcode:=A_SUBQ;
  962. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  963. end;
  964. OP_AND,
  965. OP_OR,
  966. OP_XOR:
  967. begin
  968. scratch_reg := force_to_dataregister(list, size, reg);
  969. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  970. move_if_needed(list, size, scratch_reg, reg);
  971. end;
  972. OP_DIV,
  973. OP_IDIV:
  974. begin
  975. internalerror(20020816);
  976. end;
  977. OP_MUL,
  978. OP_IMUL:
  979. begin
  980. { NOTE: better have this as fast as possible on every CPU in all cases,
  981. because the compiler uses OP_IMUL for array indexing... (KB) }
  982. { ColdFire doesn't support MULS/MULU <imm>,dX }
  983. if current_settings.cputype in cpu_coldfire then
  984. begin
  985. { move const to a register first }
  986. scratch_reg := getintregister(list,OS_INT);
  987. a_load_const_reg(list, size, a, scratch_reg);
  988. { do the multiplication }
  989. scratch_reg2 := force_to_dataregister(list, size, reg);
  990. sign_extend(list, size, scratch_reg2);
  991. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  992. { move the value back to the original register }
  993. move_if_needed(list, size, scratch_reg2, reg);
  994. end
  995. else
  996. begin
  997. if current_settings.cputype = cpu_mc68020 then
  998. begin
  999. { do the multiplication }
  1000. scratch_reg := force_to_dataregister(list, size, reg);
  1001. sign_extend(list, size, scratch_reg);
  1002. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  1003. { move the value back to the original register }
  1004. move_if_needed(list, size, scratch_reg, reg);
  1005. end
  1006. else
  1007. { Fallback branch, plain 68000 for now }
  1008. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  1009. if op = OP_MUL then
  1010. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  1011. else
  1012. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1013. end;
  1014. end;
  1015. OP_ROL,
  1016. OP_ROR,
  1017. OP_SAR,
  1018. OP_SHL,
  1019. OP_SHR :
  1020. begin
  1021. scratch_reg := force_to_dataregister(list, size, reg);
  1022. sign_extend(list, size, scratch_reg);
  1023. { some special cases which can generate smarter code
  1024. using the SWAP instruction }
  1025. if (a = 16) then
  1026. begin
  1027. if (op = OP_SHL) then
  1028. begin
  1029. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1030. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1031. end
  1032. else if (op = OP_SHR) then
  1033. begin
  1034. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1035. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1036. end
  1037. else if (op = OP_SAR) then
  1038. begin
  1039. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1040. list.concat(taicpu.op_reg(A_EXT,S_L,scratch_reg));
  1041. end
  1042. else if (op = OP_ROR) or (op = OP_ROL) then
  1043. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg))
  1044. end
  1045. else if (a >= 1) and (a <= 8) then
  1046. begin
  1047. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1048. end
  1049. else if (a >= 9) and (a < 16) then
  1050. begin
  1051. { Use two ops instead of const -> reg + shift with reg, because
  1052. this way is the same in length and speed but has less register
  1053. pressure }
  1054. list.concat(taicpu.op_const_reg(opcode, S_L, 8, scratch_reg));
  1055. list.concat(taicpu.op_const_reg(opcode, S_L, a-8, scratch_reg));
  1056. end
  1057. else
  1058. begin
  1059. { move const to a register first }
  1060. scratch_reg2 := getintregister(list,OS_INT);
  1061. a_load_const_reg(list, size, a, scratch_reg2);
  1062. { do the operation }
  1063. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1064. end;
  1065. { move the value back to the original register }
  1066. move_if_needed(list, size, scratch_reg, reg);
  1067. end;
  1068. else
  1069. internalerror(20020729);
  1070. end;
  1071. end;
  1072. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1073. var
  1074. opcode: tasmop;
  1075. opsize: topsize;
  1076. href : treference;
  1077. begin
  1078. optimize_op_const(size, op, a);
  1079. opcode := topcg2tasmop[op];
  1080. opsize := TCGSize2OpSize[size];
  1081. { on ColdFire all arithmetic operations are only possible on 32bit }
  1082. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)
  1083. and not (op in [OP_NONE,OP_MOVE])) then
  1084. begin
  1085. inherited;
  1086. exit;
  1087. end;
  1088. case op of
  1089. OP_NONE :
  1090. begin
  1091. { opcode was optimized away }
  1092. end;
  1093. OP_MOVE :
  1094. begin
  1095. { Optimized, replaced with a simple load }
  1096. a_load_const_ref(list,size,a,ref);
  1097. end;
  1098. OP_ADD,
  1099. OP_SUB :
  1100. begin
  1101. href:=ref;
  1102. fixref(list,href);
  1103. { add/sub works the same way, so have it unified here }
  1104. if (a >= 1) and (a <= 8) then
  1105. begin
  1106. if (op = OP_ADD) then
  1107. opcode:=A_ADDQ
  1108. else
  1109. opcode:=A_SUBQ;
  1110. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1111. end
  1112. else
  1113. if not(current_settings.cputype in cpu_coldfire) then
  1114. list.concat(taicpu.op_const_ref(opcode, opsize, a, href))
  1115. else
  1116. { on ColdFire, ADDI/SUBI cannot act on memory
  1117. so we can only go through a register }
  1118. inherited;
  1119. end;
  1120. else begin
  1121. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1122. inherited;
  1123. end;
  1124. end;
  1125. end;
  1126. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1127. var
  1128. hreg1, hreg2: tregister;
  1129. opcode : tasmop;
  1130. opsize : topsize;
  1131. begin
  1132. opcode := topcg2tasmop[op];
  1133. if current_settings.cputype in cpu_coldfire then
  1134. opsize := S_L
  1135. else
  1136. opsize := TCGSize2OpSize[size];
  1137. case op of
  1138. OP_ADD,
  1139. OP_SUB:
  1140. begin
  1141. if current_settings.cputype in cpu_coldfire then
  1142. begin
  1143. { operation only allowed only a longword }
  1144. sign_extend(list, size, src);
  1145. sign_extend(list, size, dst);
  1146. end;
  1147. list.concat(taicpu.op_reg_reg(opcode, opsize, src, dst));
  1148. end;
  1149. OP_AND,OP_OR,
  1150. OP_SAR,OP_SHL,
  1151. OP_SHR,OP_XOR:
  1152. begin
  1153. { load to data registers }
  1154. hreg1 := force_to_dataregister(list, size, src);
  1155. hreg2 := force_to_dataregister(list, size, dst);
  1156. if current_settings.cputype in cpu_coldfire then
  1157. begin
  1158. { operation only allowed only a longword }
  1159. {!***************************************
  1160. in the case of shifts, the value to
  1161. shift by, should already be valid, so
  1162. no need to sign extend the value
  1163. !
  1164. }
  1165. if op in [OP_AND,OP_OR,OP_XOR] then
  1166. sign_extend(list, size, hreg1);
  1167. sign_extend(list, size, hreg2);
  1168. end;
  1169. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1170. { move back result into destination register }
  1171. move_if_needed(list, size, hreg2, dst);
  1172. end;
  1173. OP_DIV,
  1174. OP_IDIV :
  1175. begin
  1176. internalerror(20020816);
  1177. end;
  1178. OP_MUL,
  1179. OP_IMUL:
  1180. begin
  1181. if (current_settings.cputype <> cpu_mc68020) and
  1182. (not (current_settings.cputype in cpu_coldfire)) then
  1183. if op = OP_MUL then
  1184. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_dword')
  1185. else
  1186. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_longint')
  1187. else
  1188. begin
  1189. { 68020+ and ColdFire codepath, probably could be improved }
  1190. hreg1 := force_to_dataregister(list, size, src);
  1191. hreg2 := force_to_dataregister(list, size, dst);
  1192. sign_extend(list, size, hreg1);
  1193. sign_extend(list, size, hreg2);
  1194. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1195. { move back result into destination register }
  1196. move_if_needed(list, size, hreg2, dst);
  1197. end;
  1198. end;
  1199. OP_NEG,
  1200. OP_NOT :
  1201. begin
  1202. { if there are two operands, move the register,
  1203. since the operation will only be done on the result
  1204. register. }
  1205. if (src<>dst) then
  1206. a_load_reg_reg(list,size,size,src,dst);
  1207. hreg2 := force_to_dataregister(list, size, dst);
  1208. { coldfire only supports long version }
  1209. if current_settings.cputype in cpu_ColdFire then
  1210. sign_extend(list, size, hreg2);
  1211. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1212. { move back the result to the result register if needed }
  1213. move_if_needed(list, size, hreg2, dst);
  1214. end;
  1215. else
  1216. internalerror(20020729);
  1217. end;
  1218. end;
  1219. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1220. var
  1221. opcode : tasmop;
  1222. opsize : topsize;
  1223. href : treference;
  1224. begin
  1225. opcode := topcg2tasmop[op];
  1226. opsize := TCGSize2OpSize[size];
  1227. { on ColdFire all arithmetic operations are only possible on 32bit
  1228. and addressing modes are limited }
  1229. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1230. begin
  1231. inherited;
  1232. exit;
  1233. end;
  1234. case op of
  1235. OP_ADD,
  1236. OP_SUB :
  1237. begin
  1238. href:=ref;
  1239. fixref(list,href);
  1240. { add/sub works the same way, so have it unified here }
  1241. list.concat(taicpu.op_reg_ref(opcode, opsize, reg, href));
  1242. end;
  1243. else begin
  1244. // list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited')));
  1245. inherited;
  1246. end;
  1247. end;
  1248. end;
  1249. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1250. l : tasmlabel);
  1251. var
  1252. hregister : tregister;
  1253. instr : taicpu;
  1254. need_temp_reg : boolean;
  1255. temp_size: topsize;
  1256. begin
  1257. need_temp_reg := false;
  1258. { plain 68000 doesn't support address registers for TST }
  1259. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1260. (a = 0) and isaddressregister(reg);
  1261. { ColdFire doesn't support address registers for CMPI }
  1262. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1263. and (a <> 0) and isaddressregister(reg));
  1264. if need_temp_reg then
  1265. begin
  1266. hregister := getintregister(list,OS_INT);
  1267. temp_size := TCGSize2OpSize[size];
  1268. if temp_size < S_W then
  1269. temp_size := S_W;
  1270. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1271. add_move_instruction(instr);
  1272. list.concat(instr);
  1273. reg := hregister;
  1274. { do sign extension if size had to be modified }
  1275. if temp_size <> TCGSize2OpSize[size] then
  1276. begin
  1277. sign_extend(list, size, reg);
  1278. size:=OS_INT;
  1279. end;
  1280. end;
  1281. if a = 0 then
  1282. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1283. else
  1284. begin
  1285. { ColdFire ISA A also needs S_L for CMPI }
  1286. { Note: older QEMU pukes from CMPI sizes <> .L even on ISA B/C, but
  1287. it's actually *LEGAL*, see CFPRM, page 4-30, the bug also seems
  1288. fixed in recent QEMU, but only when CPU cfv4e is forced, not by
  1289. default. (KB) }
  1290. if current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c]} then
  1291. begin
  1292. sign_extend(list, size, reg);
  1293. size:=OS_INT;
  1294. end;
  1295. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1296. end;
  1297. { emit the actual jump to the label }
  1298. a_jmp_cond(list,cmp_op,l);
  1299. end;
  1300. procedure tcg68k.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel);
  1301. var
  1302. tmpref: treference;
  1303. begin
  1304. { optimize for usage of TST here, so ref compares against zero, which is the
  1305. most common case by far in the RTL code at least (KB) }
  1306. if (a = 0) then
  1307. begin
  1308. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label with TST')));
  1309. tmpref:=ref;
  1310. fixref(list,tmpref);
  1311. list.concat(taicpu.op_ref(A_TST,tcgsize2opsize[size],tmpref));
  1312. a_jmp_cond(list,cmp_op,l);
  1313. end
  1314. else
  1315. begin
  1316. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label inherited')));
  1317. inherited;
  1318. end;
  1319. end;
  1320. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1321. begin
  1322. if (current_settings.cputype in cpu_coldfire-[cpu_isa_b,cpu_isa_c]) then
  1323. begin
  1324. sign_extend(list,size,reg1);
  1325. sign_extend(list,size,reg2);
  1326. size:=OS_INT;
  1327. end;
  1328. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1329. { emit the actual jump to the label }
  1330. a_jmp_cond(list,cmp_op,l);
  1331. end;
  1332. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1333. var
  1334. ai: taicpu;
  1335. begin
  1336. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s));
  1337. ai.is_jmp := true;
  1338. list.concat(ai);
  1339. end;
  1340. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1341. var
  1342. ai: taicpu;
  1343. begin
  1344. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1345. ai.is_jmp := true;
  1346. list.concat(ai);
  1347. end;
  1348. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1349. var
  1350. ai : taicpu;
  1351. begin
  1352. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  1353. ai.SetCondition(flags_to_cond(f));
  1354. ai.is_jmp := true;
  1355. list.concat(ai);
  1356. end;
  1357. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1358. var
  1359. ai : taicpu;
  1360. hreg : tregister;
  1361. instr : taicpu;
  1362. begin
  1363. { move to a Dx register? }
  1364. if (isaddressregister(reg)) then
  1365. hreg:=getintregister(list,OS_INT)
  1366. else
  1367. hreg:=reg;
  1368. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  1369. ai.SetCondition(flags_to_cond(f));
  1370. list.concat(ai);
  1371. { Scc stores a complete byte of 1s, but the compiler expects only one
  1372. bit set, so ensure this is the case }
  1373. list.concat(taicpu.op_const_reg(A_AND,S_L,1,hreg));
  1374. if hreg<>reg then
  1375. begin
  1376. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg);
  1377. add_move_instruction(instr);
  1378. list.concat(instr);
  1379. end;
  1380. end;
  1381. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1382. var
  1383. helpsize : longint;
  1384. i : byte;
  1385. hregister : tregister;
  1386. iregister : tregister;
  1387. jregister : tregister;
  1388. hp1 : treference;
  1389. hp2 : treference;
  1390. hl : tasmlabel;
  1391. srcref,dstref : treference;
  1392. begin
  1393. hregister := getintregister(list,OS_INT);
  1394. { from 12 bytes movs is being used }
  1395. if ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=12))) then
  1396. begin
  1397. srcref := source;
  1398. dstref := dest;
  1399. helpsize:=len div 4;
  1400. { move a dword x times }
  1401. for i:=1 to helpsize do
  1402. begin
  1403. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  1404. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  1405. inc(srcref.offset,4);
  1406. inc(dstref.offset,4);
  1407. dec(len,4);
  1408. end;
  1409. { move a word }
  1410. if len>1 then
  1411. begin
  1412. a_load_ref_reg(list,OS_16,OS_16,srcref,hregister);
  1413. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  1414. inc(srcref.offset,2);
  1415. inc(dstref.offset,2);
  1416. dec(len,2);
  1417. end;
  1418. { move a single byte }
  1419. if len>0 then
  1420. begin
  1421. a_load_ref_reg(list,OS_8,OS_8,srcref,hregister);
  1422. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  1423. end
  1424. end
  1425. else
  1426. begin
  1427. iregister:=getaddressregister(list);
  1428. jregister:=getaddressregister(list);
  1429. { reference for move (An)+,(An)+ }
  1430. reference_reset(hp1,source.alignment);
  1431. hp1.base := iregister; { source register }
  1432. hp1.direction := dir_inc;
  1433. reference_reset(hp2,dest.alignment);
  1434. hp2.base := jregister;
  1435. hp2.direction := dir_inc;
  1436. { iregister = source }
  1437. { jregister = destination }
  1438. a_loadaddr_ref_reg(list,source,iregister);
  1439. a_loadaddr_ref_reg(list,dest,jregister);
  1440. { double word move only on 68020+ machines }
  1441. { because of possible alignment problems }
  1442. { use fast loop mode }
  1443. if (current_settings.cputype=cpu_MC68020) then
  1444. begin
  1445. //list.concat(tai_comment.create(strpnew('g_concatcopy tight copy loop 020+')));
  1446. helpsize := len - len mod 4;
  1447. len := len mod 4;
  1448. a_load_const_reg(list,OS_INT,(helpsize div 4)-1,hregister);
  1449. current_asmdata.getjumplabel(hl);
  1450. a_label(list,hl);
  1451. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  1452. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1453. if len > 1 then
  1454. begin
  1455. dec(len,2);
  1456. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  1457. end;
  1458. if len = 1 then
  1459. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1460. end
  1461. else
  1462. begin
  1463. { Fast 68010 loop mode with no possible alignment problems }
  1464. //list.concat(tai_comment.create(strpnew('g_concatcopy tight byte copy loop')));
  1465. a_load_const_reg(list,OS_INT,len - 1,hregister);
  1466. current_asmdata.getjumplabel(hl);
  1467. a_label(list,hl);
  1468. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1469. if current_settings.cputype in cpu_coldfire then
  1470. begin
  1471. { Coldfire does not support DBRA }
  1472. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1473. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1474. end
  1475. else
  1476. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1477. end;
  1478. end;
  1479. end;
  1480. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1481. var
  1482. hl : tasmlabel;
  1483. ai : taicpu;
  1484. cond : TAsmCond;
  1485. begin
  1486. if not(cs_check_overflow in current_settings.localswitches) then
  1487. exit;
  1488. current_asmdata.getjumplabel(hl);
  1489. if not ((def.typ=pointerdef) or
  1490. ((def.typ=orddef) and
  1491. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1492. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1493. cond:=C_VC
  1494. else
  1495. cond:=C_CC;
  1496. ai:=Taicpu.Op_Sym(A_Bxx,S_NO,hl);
  1497. ai.SetCondition(cond);
  1498. ai.is_jmp:=true;
  1499. list.concat(ai);
  1500. a_call_name(list,'FPC_OVERFLOW',false);
  1501. a_label(list,hl);
  1502. end;
  1503. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1504. begin
  1505. { Carl's original code used 2x MOVE instead of LINK when localsize = 0.
  1506. However, a LINK seems faster than two moves on everything from 68000
  1507. to '060, so the two move branch here was dropped. (KB) }
  1508. if not nostackframe then
  1509. begin
  1510. { size can't be negative }
  1511. if (localsize < 0) then
  1512. internalerror(2006122601);
  1513. if (localsize > high(smallint)) then
  1514. begin
  1515. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1516. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1517. end
  1518. else
  1519. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1520. end;
  1521. end;
  1522. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1523. var
  1524. r,hregister : TRegister;
  1525. ref : TReference;
  1526. ref2: TReference;
  1527. begin
  1528. if not nostackframe then
  1529. begin
  1530. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1531. { if parasize is less than zero here, we probably have a cdecl function.
  1532. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1533. 68k GCC uses two different methods to free the stack, depending if the target
  1534. architecture supports RTD or not, and one does callee side, the other does
  1535. caller side free, which looks like a PITA to support. We have to figure this
  1536. out later. More info welcomed. (KB) }
  1537. if (parasize > 0) and not (current_procinfo.procdef.proccalloption in clearstack_pocalls) then
  1538. begin
  1539. if current_settings.cputype=cpu_mc68020 then
  1540. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1541. else
  1542. begin
  1543. { We must pull the PC Counter from the stack, before }
  1544. { restoring the stack pointer, otherwise the PC would }
  1545. { point to nowhere! }
  1546. { Instead of doing a slow copy of the return address while trying }
  1547. { to feed it to the RTS instruction, load the PC to A0 (scratch reg) }
  1548. { then free up the stack allocated for paras, then use a JMP (A0) to }
  1549. { return to the caller with the paras freed. (KB) }
  1550. hregister:=NR_A0;
  1551. cg.a_reg_alloc(list,hregister);
  1552. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1553. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1554. { instead of using a postincrement above (which also writes the }
  1555. { stackpointer reg) simply add 4 to the parasize, the instructions }
  1556. { below then take that size into account as well, so SP reg is only }
  1557. { written once (KB) }
  1558. parasize:=parasize+4;
  1559. r:=NR_SP;
  1560. { can we do a quick addition ... }
  1561. if (parasize < 9) then
  1562. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1563. else { nope ... }
  1564. begin
  1565. reference_reset_base(ref2,NR_STACK_POINTER_REG,parasize,4);
  1566. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref2,r));
  1567. end;
  1568. reference_reset_base(ref,hregister,0,4);
  1569. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1570. end;
  1571. end
  1572. else
  1573. list.concat(taicpu.op_none(A_RTS,S_NO));
  1574. end
  1575. else
  1576. begin
  1577. list.concat(taicpu.op_none(A_RTS,S_NO));
  1578. end;
  1579. { Routines with the poclearstack flag set use only a ret.
  1580. also routines with parasize=0 }
  1581. { TODO: figure out if these are still relevant to us (KB) }
  1582. (*
  1583. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1584. begin
  1585. { complex return values are removed from stack in C code PM }
  1586. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1587. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1588. else
  1589. list.concat(taicpu.op_none(A_RTS,S_NO));
  1590. end
  1591. else if (parasize=0) then
  1592. begin
  1593. list.concat(taicpu.op_none(A_RTS,S_NO));
  1594. end
  1595. else
  1596. *)
  1597. end;
  1598. procedure tcg68k.g_save_registers(list:TAsmList);
  1599. var
  1600. dataregs: tcpuregisterset;
  1601. addrregs: tcpuregisterset;
  1602. href : treference;
  1603. hreg : tregister;
  1604. size : longint;
  1605. r : integer;
  1606. begin
  1607. { The code generated by the section below, particularly the movem.l
  1608. instruction is known to cause an issue when compiled by some GNU
  1609. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1610. when you run into this problem, just call inherited here instead
  1611. to skip the movem.l generation. But better just use working GNU
  1612. AS version instead. (KB) }
  1613. dataregs:=[];
  1614. addrregs:=[];
  1615. { calculate temp. size }
  1616. size:=0;
  1617. hreg:=NR_NO;
  1618. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1619. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1620. begin
  1621. hreg:=newreg(R_INTREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1622. inc(size,sizeof(aint));
  1623. dataregs:=dataregs + [saved_standard_registers[r]];
  1624. end;
  1625. if uses_registers(R_ADDRESSREGISTER) then
  1626. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1627. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1628. begin
  1629. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1630. inc(size,sizeof(aint));
  1631. addrregs:=addrregs + [saved_address_registers[r]];
  1632. end;
  1633. { 68k has no MM registers }
  1634. if uses_registers(R_MMREGISTER) then
  1635. internalerror(2014030201);
  1636. if size>0 then
  1637. begin
  1638. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1639. include(current_procinfo.flags,pi_has_saved_regs);
  1640. { Copy registers to temp }
  1641. { NOTE: virtual registers allocated here won't be translated --> no higher-level stuff. }
  1642. href:=current_procinfo.save_regs_ref;
  1643. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1644. begin
  1645. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1646. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1647. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1648. end;
  1649. if size = sizeof(aint) then
  1650. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href))
  1651. else
  1652. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,href));
  1653. end;
  1654. end;
  1655. procedure tcg68k.g_restore_registers(list:TAsmList);
  1656. var
  1657. dataregs: tcpuregisterset;
  1658. addrregs: tcpuregisterset;
  1659. href : treference;
  1660. r : integer;
  1661. hreg : tregister;
  1662. size : longint;
  1663. begin
  1664. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1665. dataregs:=[];
  1666. addrregs:=[];
  1667. if not(pi_has_saved_regs in current_procinfo.flags) then
  1668. exit;
  1669. { Copy registers from temp }
  1670. size:=0;
  1671. hreg:=NR_NO;
  1672. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1673. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1674. begin
  1675. inc(size,sizeof(aint));
  1676. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1677. { Allocate register so the optimizer does not remove the load }
  1678. a_reg_alloc(list,hreg);
  1679. dataregs:=dataregs + [saved_standard_registers[r]];
  1680. end;
  1681. if uses_registers(R_ADDRESSREGISTER) then
  1682. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1683. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1684. begin
  1685. inc(size,sizeof(aint));
  1686. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1687. { Allocate register so the optimizer does not remove the load }
  1688. a_reg_alloc(list,hreg);
  1689. addrregs:=addrregs + [saved_address_registers[r]];
  1690. end;
  1691. { 68k has no MM registers }
  1692. if uses_registers(R_MMREGISTER) then
  1693. internalerror(2014030202);
  1694. { Restore registers from temp }
  1695. href:=current_procinfo.save_regs_ref;
  1696. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1697. begin
  1698. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1699. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1700. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1701. end;
  1702. if size = sizeof(aint) then
  1703. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,hreg))
  1704. else
  1705. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs));
  1706. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1707. end;
  1708. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  1709. begin
  1710. case _newsize of
  1711. OS_S16, OS_16:
  1712. case _oldsize of
  1713. OS_S8:
  1714. begin { 8 -> 16 bit sign extend }
  1715. if (isaddressregister(reg)) then
  1716. internalerror(2014031201);
  1717. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1718. end;
  1719. OS_8: { 8 -> 16 bit zero extend }
  1720. begin
  1721. if (current_settings.cputype in cpu_coldfire) then
  1722. { ColdFire has no ANDI.W }
  1723. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  1724. else
  1725. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  1726. end;
  1727. end;
  1728. OS_S32, OS_32:
  1729. case _oldsize of
  1730. OS_S8:
  1731. begin { 8 -> 32 bit sign extend }
  1732. if (isaddressregister(reg)) then
  1733. internalerror(2014031202);
  1734. if (current_settings.cputype = cpu_MC68000) then
  1735. begin
  1736. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1737. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1738. end
  1739. else
  1740. begin
  1741. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  1742. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1743. end;
  1744. end;
  1745. OS_8: { 8 -> 32 bit zero extend }
  1746. begin
  1747. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1748. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1749. end;
  1750. OS_S16: { 16 -> 32 bit sign extend }
  1751. begin
  1752. if (isaddressregister(reg)) then
  1753. internalerror(2014031203);
  1754. //list.concat(tai_comment.create(strpnew('sign extend word')));
  1755. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1756. end;
  1757. OS_16:
  1758. begin
  1759. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1760. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1761. end;
  1762. end;
  1763. end; { otherwise the size is already correct }
  1764. end;
  1765. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1766. begin
  1767. sign_extend(list, _oldsize, OS_INT, reg);
  1768. end;
  1769. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1770. var
  1771. ai : taicpu;
  1772. begin
  1773. if cond=OC_None then
  1774. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1775. else
  1776. begin
  1777. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1778. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1779. end;
  1780. ai.is_jmp:=true;
  1781. list.concat(ai);
  1782. end;
  1783. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  1784. operations on an address register. if the register is a dataregister anyway, it
  1785. just returns it untouched.}
  1786. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  1787. var
  1788. scratch_reg: TRegister;
  1789. instr: Taicpu;
  1790. begin
  1791. if isaddressregister(reg) then
  1792. begin
  1793. scratch_reg:=getintregister(list,OS_INT);
  1794. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg);
  1795. add_move_instruction(instr);
  1796. list.concat(instr);
  1797. result:=scratch_reg;
  1798. end
  1799. else
  1800. result:=reg;
  1801. end;
  1802. { moves source register to destination register, if the two are not the same. can be used in pair
  1803. with force_to_dataregister() }
  1804. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  1805. var
  1806. instr: Taicpu;
  1807. begin
  1808. if (src <> dest) then
  1809. begin
  1810. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  1811. add_move_instruction(instr);
  1812. list.concat(instr);
  1813. end;
  1814. end;
  1815. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1816. var
  1817. hsym : tsym;
  1818. href : treference;
  1819. paraloc : Pcgparalocation;
  1820. begin
  1821. { calculate the parameter info for the procdef }
  1822. procdef.init_paraloc_info(callerside);
  1823. hsym:=tsym(procdef.parast.Find('self'));
  1824. if not(assigned(hsym) and
  1825. (hsym.typ=paravarsym)) then
  1826. internalerror(2013100702);
  1827. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1828. while paraloc<>nil do
  1829. with paraloc^ do
  1830. begin
  1831. case loc of
  1832. LOC_REGISTER:
  1833. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  1834. LOC_REFERENCE:
  1835. begin
  1836. { offset in the wrapper needs to be adjusted for the stored
  1837. return address }
  1838. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  1839. { plain 68k could use SUBI on href directly, but this way it works on Coldfire too
  1840. and it's probably smaller code for the majority of cases (if ioffset small, the
  1841. load will use MOVEQ) (KB) }
  1842. a_load_const_reg(list,OS_ADDR,ioffset,NR_D0);
  1843. list.concat(taicpu.op_reg_ref(A_SUB,S_L,NR_D0,href));
  1844. end
  1845. else
  1846. internalerror(2013100703);
  1847. end;
  1848. paraloc:=next;
  1849. end;
  1850. end;
  1851. procedure tcg68k.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1852. procedure getselftoa0(offs:longint);
  1853. var
  1854. href : treference;
  1855. selfoffsetfromsp : longint;
  1856. begin
  1857. { move.l offset(%sp),%a0 }
  1858. { framepointer is pushed for nested procs }
  1859. if procdef.parast.symtablelevel>normal_function_level then
  1860. selfoffsetfromsp:=sizeof(aint)
  1861. else
  1862. selfoffsetfromsp:=0;
  1863. reference_reset_base(href,NR_SP,selfoffsetfromsp+offs,4);
  1864. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_A0);
  1865. end;
  1866. procedure loadvmttoa0;
  1867. var
  1868. href : treference;
  1869. begin
  1870. { move.l (%a0),%a0 ; load vmt}
  1871. reference_reset_base(href,NR_A0,0,4);
  1872. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_A0);
  1873. end;
  1874. procedure op_ona0methodaddr;
  1875. var
  1876. href : treference;
  1877. begin
  1878. if (procdef.extnumber=$ffff) then
  1879. Internalerror(2013100701);
  1880. reference_reset_base(href,NR_A0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),4);
  1881. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,NR_A0));
  1882. reference_reset_base(href,NR_A0,0,4);
  1883. list.concat(taicpu.op_ref(A_JMP,S_NO,href));
  1884. end;
  1885. var
  1886. make_global : boolean;
  1887. begin
  1888. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1889. Internalerror(200006137);
  1890. if not assigned(procdef.struct) or
  1891. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1892. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1893. Internalerror(200006138);
  1894. if procdef.owner.symtabletype<>ObjectSymtable then
  1895. Internalerror(200109191);
  1896. make_global:=false;
  1897. if (not current_module.is_unit) or
  1898. create_smartlink or
  1899. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1900. make_global:=true;
  1901. if make_global then
  1902. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1903. else
  1904. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1905. { set param1 interface to self }
  1906. g_adjust_self_value(list,procdef,ioffset);
  1907. { case 4 }
  1908. if (po_virtualmethod in procdef.procoptions) and
  1909. not is_objectpascal_helper(procdef.struct) then
  1910. begin
  1911. getselftoa0(4);
  1912. loadvmttoa0;
  1913. op_ona0methodaddr;
  1914. end
  1915. { case 0 }
  1916. else
  1917. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1918. List.concat(Tai_symbol_end.Createname(labelname));
  1919. end;
  1920. procedure tcg68k.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1921. begin
  1922. list.concat(taicpu.op_const_reg(A_SUB,S_L,localsize,NR_STACK_POINTER_REG));
  1923. end;
  1924. {****************************************************************************}
  1925. { TCG64F68K }
  1926. {****************************************************************************}
  1927. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  1928. var
  1929. opcode : tasmop;
  1930. xopcode : tasmop;
  1931. instr : taicpu;
  1932. begin
  1933. opcode := topcg2tasmop[op];
  1934. xopcode := topcg2tasmopx[op];
  1935. case op of
  1936. OP_ADD,OP_SUB:
  1937. begin
  1938. { if one of these three registers is an address
  1939. register, we'll really get into problems! }
  1940. if isaddressregister(regdst.reglo) or
  1941. isaddressregister(regdst.reghi) or
  1942. isaddressregister(regsrc.reghi) then
  1943. internalerror(2014030101);
  1944. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  1945. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  1946. end;
  1947. OP_AND,OP_OR:
  1948. begin
  1949. { at least one of the registers must be a data register }
  1950. if (isaddressregister(regdst.reglo) and
  1951. isaddressregister(regsrc.reglo)) or
  1952. (isaddressregister(regsrc.reghi) and
  1953. isaddressregister(regdst.reghi)) then
  1954. internalerror(2014030102);
  1955. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1956. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1957. end;
  1958. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1959. OP_IDIV,OP_DIV,
  1960. OP_IMUL,OP_MUL:
  1961. internalerror(2002081701);
  1962. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1963. OP_SAR,OP_SHL,OP_SHR:
  1964. internalerror(2002081702);
  1965. OP_XOR:
  1966. begin
  1967. if isaddressregister(regdst.reglo) or
  1968. isaddressregister(regsrc.reglo) or
  1969. isaddressregister(regsrc.reghi) or
  1970. isaddressregister(regdst.reghi) then
  1971. internalerror(2014030103);
  1972. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1973. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1974. end;
  1975. OP_NEG,OP_NOT:
  1976. begin
  1977. if isaddressregister(regdst.reglo) or
  1978. isaddressregister(regdst.reghi) then
  1979. internalerror(2014030104);
  1980. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  1981. cg.add_move_instruction(instr);
  1982. list.concat(instr);
  1983. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  1984. cg.add_move_instruction(instr);
  1985. list.concat(instr);
  1986. if (op = OP_NOT) then
  1987. xopcode:=opcode;
  1988. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  1989. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  1990. end;
  1991. end; { end case }
  1992. end;
  1993. procedure tcg64f68k.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  1994. var
  1995. tempref : treference;
  1996. begin
  1997. case op of
  1998. OP_NEG,OP_NOT:
  1999. begin
  2000. a_load64_ref_reg(list,ref,reg);
  2001. a_op64_reg_reg(list,op,size,reg,reg);
  2002. end;
  2003. OP_AND,OP_OR:
  2004. begin
  2005. tempref:=ref;
  2006. tcg68k(cg).fixref(list,tempref);
  2007. inc(tempref.offset,4);
  2008. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reglo));
  2009. dec(tempref.offset,4);
  2010. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reghi));
  2011. end;
  2012. else
  2013. { XOR does not allow reference for source; ADD/SUB do not allow reference for
  2014. high dword, although low dword can still be handled directly. }
  2015. inherited a_op64_ref_reg(list,op,size,ref,reg);
  2016. end;
  2017. end;
  2018. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  2019. var
  2020. lowvalue : cardinal;
  2021. highvalue : cardinal;
  2022. opcode : tasmop;
  2023. xopcode : tasmop;
  2024. hreg : tregister;
  2025. begin
  2026. { is it optimized out ? }
  2027. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  2028. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  2029. exit; }
  2030. lowvalue := cardinal(value);
  2031. highvalue := value shr 32;
  2032. opcode := topcg2tasmop[op];
  2033. xopcode := topcg2tasmopx[op];
  2034. { the destination registers must be data registers }
  2035. if isaddressregister(regdst.reglo) or
  2036. isaddressregister(regdst.reghi) then
  2037. internalerror(2014030105);
  2038. case op of
  2039. OP_ADD,OP_SUB:
  2040. begin
  2041. hreg:=cg.getintregister(list,OS_INT);
  2042. { cg.a_load_const_reg provides optimized loading to register for special cases }
  2043. cg.a_load_const_reg(list,OS_S32,longint(highvalue),hreg);
  2044. { don't use cg.a_op_const_reg() here, because a possible optimized
  2045. ADDQ/SUBQ wouldn't set the eXtend bit }
  2046. list.concat(taicpu.op_const_reg(opcode,S_L,lowvalue,regdst.reglo));
  2047. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  2048. end;
  2049. OP_AND,OP_OR,OP_XOR:
  2050. begin
  2051. cg.a_op_const_reg(list,op,OS_S32,longint(lowvalue),regdst.reglo);
  2052. cg.a_op_const_reg(list,op,OS_S32,longint(highvalue),regdst.reghi);
  2053. end;
  2054. { this is handled in 1st pass for 32-bit cpus (helper call) }
  2055. OP_IDIV,OP_DIV,
  2056. OP_IMUL,OP_MUL:
  2057. internalerror(2002081701);
  2058. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  2059. OP_SAR,OP_SHL,OP_SHR:
  2060. internalerror(2002081702);
  2061. { these should have been handled already by earlier passes }
  2062. OP_NOT,OP_NEG:
  2063. internalerror(2012110403);
  2064. end; { end case }
  2065. end;
  2066. procedure create_codegen;
  2067. begin
  2068. cg := tcg68k.create;
  2069. cg64 :=tcg64f68k.create;
  2070. end;
  2071. end.