cgcpu.pas 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  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. begin
  858. href:=ref;
  859. fixref(list, href);
  860. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  861. end;
  862. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  863. var
  864. instr : taicpu;
  865. begin
  866. { in emulation mode, only 32-bit single is supported }
  867. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  868. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg1,reg2)
  869. else
  870. instr:=taicpu.op_reg_reg(A_FMOVE,tcgsize2opsize[tosize],reg1,reg2);
  871. add_move_instruction(instr);
  872. list.concat(instr);
  873. end;
  874. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  875. var
  876. opsize : topsize;
  877. href : treference;
  878. begin
  879. opsize := tcgsize2opsize[fromsize];
  880. { extended is not supported, since it is not available on Coldfire }
  881. if opsize = S_FX then
  882. internalerror(20020729);
  883. href := ref;
  884. fixref(list,href);
  885. { in emulation mode, only 32-bit single is supported }
  886. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  887. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,reg))
  888. else
  889. begin
  890. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  891. if (tosize < fromsize) then
  892. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  893. end;
  894. end;
  895. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  896. var
  897. opsize : topsize;
  898. begin
  899. opsize := tcgsize2opsize[tosize];
  900. { extended is not supported, since it is not available on Coldfire }
  901. if opsize = S_FX then
  902. internalerror(20020729);
  903. { in emulation mode, only 32-bit single is supported }
  904. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  905. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,reg, ref))
  906. else
  907. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg, ref));
  908. end;
  909. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  910. begin
  911. case cgpara.location^.loc of
  912. LOC_REFERENCE,LOC_CREFERENCE:
  913. begin
  914. case size of
  915. OS_F64:
  916. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  917. OS_F32:
  918. a_load_ref_cgpara(list,size,ref,cgpara);
  919. else
  920. internalerror(2013021201);
  921. end;
  922. end;
  923. else
  924. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  925. end;
  926. end;
  927. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  928. var
  929. scratch_reg : tregister;
  930. scratch_reg2: tregister;
  931. opcode : tasmop;
  932. begin
  933. optimize_op_const(size, op, a);
  934. opcode := topcg2tasmop[op];
  935. case op of
  936. OP_NONE :
  937. begin
  938. { Opcode is optimized away }
  939. end;
  940. OP_MOVE :
  941. begin
  942. { Optimized, replaced with a simple load }
  943. a_load_const_reg(list,size,a,reg);
  944. end;
  945. OP_ADD,
  946. OP_SUB:
  947. begin
  948. { add/sub works the same way, so have it unified here }
  949. if (a >= 1) and (a <= 8) then
  950. if (op = OP_ADD) then
  951. opcode:=A_ADDQ
  952. else
  953. opcode:=A_SUBQ;
  954. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  955. end;
  956. OP_AND,
  957. OP_OR,
  958. OP_XOR:
  959. begin
  960. scratch_reg := force_to_dataregister(list, size, reg);
  961. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  962. move_if_needed(list, size, scratch_reg, reg);
  963. end;
  964. OP_DIV,
  965. OP_IDIV:
  966. begin
  967. internalerror(20020816);
  968. end;
  969. OP_MUL,
  970. OP_IMUL:
  971. begin
  972. { NOTE: better have this as fast as possible on every CPU in all cases,
  973. because the compiler uses OP_IMUL for array indexing... (KB) }
  974. { ColdFire doesn't support MULS/MULU <imm>,dX }
  975. if current_settings.cputype in cpu_coldfire then
  976. begin
  977. { move const to a register first }
  978. scratch_reg := getintregister(list,OS_INT);
  979. a_load_const_reg(list, size, a, scratch_reg);
  980. { do the multiplication }
  981. scratch_reg2 := force_to_dataregister(list, size, reg);
  982. sign_extend(list, size, scratch_reg2);
  983. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  984. { move the value back to the original register }
  985. move_if_needed(list, size, scratch_reg2, reg);
  986. end
  987. else
  988. begin
  989. if current_settings.cputype = cpu_mc68020 then
  990. begin
  991. { do the multiplication }
  992. scratch_reg := force_to_dataregister(list, size, reg);
  993. sign_extend(list, size, scratch_reg);
  994. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  995. { move the value back to the original register }
  996. move_if_needed(list, size, scratch_reg, reg);
  997. end
  998. else
  999. { Fallback branch, plain 68000 for now }
  1000. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  1001. if op = OP_MUL then
  1002. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  1003. else
  1004. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1005. end;
  1006. end;
  1007. OP_ROL,
  1008. OP_ROR,
  1009. OP_SAR,
  1010. OP_SHL,
  1011. OP_SHR :
  1012. begin
  1013. scratch_reg := force_to_dataregister(list, size, reg);
  1014. sign_extend(list, size, scratch_reg);
  1015. if (a >= 1) and (a <= 8) then
  1016. begin
  1017. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1018. end
  1019. else
  1020. begin
  1021. { move const to a register first }
  1022. scratch_reg2 := getintregister(list,OS_INT);
  1023. a_load_const_reg(list, size, a, scratch_reg2);
  1024. { do the operation }
  1025. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1026. end;
  1027. { move the value back to the original register }
  1028. move_if_needed(list, size, scratch_reg, reg);
  1029. end;
  1030. else
  1031. internalerror(20020729);
  1032. end;
  1033. end;
  1034. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1035. var
  1036. opcode: tasmop;
  1037. opsize: topsize;
  1038. href : treference;
  1039. begin
  1040. optimize_op_const(size, op, a);
  1041. opcode := topcg2tasmop[op];
  1042. opsize := TCGSize2OpSize[size];
  1043. { on ColdFire all arithmetic operations are only possible on 32bit }
  1044. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)
  1045. and not (op in [OP_NONE,OP_MOVE])) then
  1046. begin
  1047. inherited;
  1048. exit;
  1049. end;
  1050. case op of
  1051. OP_NONE :
  1052. begin
  1053. { opcode was optimized away }
  1054. end;
  1055. OP_MOVE :
  1056. begin
  1057. { Optimized, replaced with a simple load }
  1058. a_load_const_ref(list,size,a,ref);
  1059. end;
  1060. OP_ADD,
  1061. OP_SUB :
  1062. begin
  1063. href:=ref;
  1064. fixref(list,href);
  1065. { add/sub works the same way, so have it unified here }
  1066. if (a >= 1) and (a <= 8) then
  1067. begin
  1068. if (op = OP_ADD) then
  1069. opcode:=A_ADDQ
  1070. else
  1071. opcode:=A_SUBQ;
  1072. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1073. end
  1074. else
  1075. if not(current_settings.cputype in cpu_coldfire) then
  1076. list.concat(taicpu.op_const_ref(opcode, opsize, a, href))
  1077. else
  1078. { on ColdFire, ADDI/SUBI cannot act on memory
  1079. so we can only go through a register }
  1080. inherited;
  1081. end;
  1082. else begin
  1083. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1084. inherited;
  1085. end;
  1086. end;
  1087. end;
  1088. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1089. var
  1090. hreg1, hreg2: tregister;
  1091. opcode : tasmop;
  1092. opsize : topsize;
  1093. begin
  1094. opcode := topcg2tasmop[op];
  1095. if current_settings.cputype in cpu_coldfire then
  1096. opsize := S_L
  1097. else
  1098. opsize := TCGSize2OpSize[size];
  1099. case op of
  1100. OP_ADD,
  1101. OP_SUB:
  1102. begin
  1103. if current_settings.cputype in cpu_coldfire then
  1104. begin
  1105. { operation only allowed only a longword }
  1106. sign_extend(list, size, src);
  1107. sign_extend(list, size, dst);
  1108. end;
  1109. list.concat(taicpu.op_reg_reg(opcode, opsize, src, dst));
  1110. end;
  1111. OP_AND,OP_OR,
  1112. OP_SAR,OP_SHL,
  1113. OP_SHR,OP_XOR:
  1114. begin
  1115. { load to data registers }
  1116. hreg1 := force_to_dataregister(list, size, src);
  1117. hreg2 := force_to_dataregister(list, size, dst);
  1118. if current_settings.cputype in cpu_coldfire then
  1119. begin
  1120. { operation only allowed only a longword }
  1121. {!***************************************
  1122. in the case of shifts, the value to
  1123. shift by, should already be valid, so
  1124. no need to sign extend the value
  1125. !
  1126. }
  1127. if op in [OP_AND,OP_OR,OP_XOR] then
  1128. sign_extend(list, size, hreg1);
  1129. sign_extend(list, size, hreg2);
  1130. end;
  1131. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1132. { move back result into destination register }
  1133. move_if_needed(list, size, hreg2, dst);
  1134. end;
  1135. OP_DIV,
  1136. OP_IDIV :
  1137. begin
  1138. internalerror(20020816);
  1139. end;
  1140. OP_MUL,
  1141. OP_IMUL:
  1142. begin
  1143. if (current_settings.cputype <> cpu_mc68020) and
  1144. (not (current_settings.cputype in cpu_coldfire)) then
  1145. if op = OP_MUL then
  1146. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_dword')
  1147. else
  1148. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_longint')
  1149. else
  1150. begin
  1151. { 68020+ and ColdFire codepath, probably could be improved }
  1152. hreg1 := force_to_dataregister(list, size, src);
  1153. hreg2 := force_to_dataregister(list, size, dst);
  1154. sign_extend(list, size, hreg1);
  1155. sign_extend(list, size, hreg2);
  1156. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1157. { move back result into destination register }
  1158. move_if_needed(list, size, hreg2, dst);
  1159. end;
  1160. end;
  1161. OP_NEG,
  1162. OP_NOT :
  1163. begin
  1164. { if there are two operands, move the register,
  1165. since the operation will only be done on the result
  1166. register. }
  1167. if (src<>dst) then
  1168. a_load_reg_reg(list,size,size,src,dst);
  1169. hreg2 := force_to_dataregister(list, size, dst);
  1170. { coldfire only supports long version }
  1171. if current_settings.cputype in cpu_ColdFire then
  1172. sign_extend(list, size, hreg2);
  1173. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1174. { move back the result to the result register if needed }
  1175. move_if_needed(list, size, hreg2, dst);
  1176. end;
  1177. else
  1178. internalerror(20020729);
  1179. end;
  1180. end;
  1181. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1182. var
  1183. opcode : tasmop;
  1184. opsize : topsize;
  1185. href : treference;
  1186. begin
  1187. opcode := topcg2tasmop[op];
  1188. opsize := TCGSize2OpSize[size];
  1189. { on ColdFire all arithmetic operations are only possible on 32bit
  1190. and addressing modes are limited }
  1191. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1192. begin
  1193. inherited;
  1194. exit;
  1195. end;
  1196. case op of
  1197. OP_ADD,
  1198. OP_SUB :
  1199. begin
  1200. href:=ref;
  1201. fixref(list,href);
  1202. { add/sub works the same way, so have it unified here }
  1203. list.concat(taicpu.op_reg_ref(opcode, opsize, reg, href));
  1204. end;
  1205. else begin
  1206. // list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited')));
  1207. inherited;
  1208. end;
  1209. end;
  1210. end;
  1211. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1212. l : tasmlabel);
  1213. var
  1214. hregister : tregister;
  1215. instr : taicpu;
  1216. need_temp_reg : boolean;
  1217. temp_size: topsize;
  1218. begin
  1219. need_temp_reg := false;
  1220. { plain 68000 doesn't support address registers for TST }
  1221. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1222. (a = 0) and isaddressregister(reg);
  1223. { ColdFire doesn't support address registers for CMPI }
  1224. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1225. and (a <> 0) and isaddressregister(reg));
  1226. if need_temp_reg then
  1227. begin
  1228. hregister := getintregister(list,OS_INT);
  1229. temp_size := TCGSize2OpSize[size];
  1230. if temp_size < S_W then
  1231. temp_size := S_W;
  1232. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1233. add_move_instruction(instr);
  1234. list.concat(instr);
  1235. reg := hregister;
  1236. { do sign extension if size had to be modified }
  1237. if temp_size <> TCGSize2OpSize[size] then
  1238. begin
  1239. sign_extend(list, size, reg);
  1240. size:=OS_INT;
  1241. end;
  1242. end;
  1243. if a = 0 then
  1244. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1245. else
  1246. begin
  1247. { ColdFire ISA A also needs S_L for CMPI }
  1248. { Note: older QEMU pukes from CMPI sizes <> .L even on ISA B/C, but
  1249. it's actually *LEGAL*, see CFPRM, page 4-30, the bug also seems
  1250. fixed in recent QEMU, but only when CPU cfv4e is forced, not by
  1251. default. (KB) }
  1252. if current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c]} then
  1253. begin
  1254. sign_extend(list, size, reg);
  1255. size:=OS_INT;
  1256. end;
  1257. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1258. end;
  1259. { emit the actual jump to the label }
  1260. a_jmp_cond(list,cmp_op,l);
  1261. end;
  1262. procedure tcg68k.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel);
  1263. var
  1264. tmpref: treference;
  1265. begin
  1266. { optimize for usage of TST here, so ref compares against zero, which is the
  1267. most common case by far in the RTL code at least (KB) }
  1268. if (a = 0) then
  1269. begin
  1270. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label with TST')));
  1271. tmpref:=ref;
  1272. fixref(list,tmpref);
  1273. list.concat(taicpu.op_ref(A_TST,tcgsize2opsize[size],tmpref));
  1274. a_jmp_cond(list,cmp_op,l);
  1275. end
  1276. else
  1277. begin
  1278. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label inherited')));
  1279. inherited;
  1280. end;
  1281. end;
  1282. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1283. begin
  1284. if (current_settings.cputype in cpu_coldfire-[cpu_isa_b,cpu_isa_c]) then
  1285. begin
  1286. sign_extend(list,size,reg1);
  1287. sign_extend(list,size,reg2);
  1288. size:=OS_INT;
  1289. end;
  1290. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1291. { emit the actual jump to the label }
  1292. a_jmp_cond(list,cmp_op,l);
  1293. end;
  1294. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1295. var
  1296. ai: taicpu;
  1297. begin
  1298. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s));
  1299. ai.is_jmp := true;
  1300. list.concat(ai);
  1301. end;
  1302. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1303. var
  1304. ai: taicpu;
  1305. begin
  1306. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1307. ai.is_jmp := true;
  1308. list.concat(ai);
  1309. end;
  1310. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1311. var
  1312. ai : taicpu;
  1313. begin
  1314. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  1315. ai.SetCondition(flags_to_cond(f));
  1316. ai.is_jmp := true;
  1317. list.concat(ai);
  1318. end;
  1319. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1320. var
  1321. ai : taicpu;
  1322. hreg : tregister;
  1323. instr : taicpu;
  1324. begin
  1325. { move to a Dx register? }
  1326. if (isaddressregister(reg)) then
  1327. hreg:=getintregister(list,OS_INT)
  1328. else
  1329. hreg:=reg;
  1330. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  1331. ai.SetCondition(flags_to_cond(f));
  1332. list.concat(ai);
  1333. { Scc stores a complete byte of 1s, but the compiler expects only one
  1334. bit set, so ensure this is the case }
  1335. list.concat(taicpu.op_const_reg(A_AND,S_L,1,hreg));
  1336. if hreg<>reg then
  1337. begin
  1338. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg);
  1339. add_move_instruction(instr);
  1340. list.concat(instr);
  1341. end;
  1342. end;
  1343. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1344. var
  1345. helpsize : longint;
  1346. i : byte;
  1347. hregister : tregister;
  1348. iregister : tregister;
  1349. jregister : tregister;
  1350. hp1 : treference;
  1351. hp2 : treference;
  1352. hl : tasmlabel;
  1353. srcref,dstref : treference;
  1354. begin
  1355. hregister := getintregister(list,OS_INT);
  1356. { from 12 bytes movs is being used }
  1357. if ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=12))) then
  1358. begin
  1359. srcref := source;
  1360. dstref := dest;
  1361. helpsize:=len div 4;
  1362. { move a dword x times }
  1363. for i:=1 to helpsize do
  1364. begin
  1365. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  1366. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  1367. inc(srcref.offset,4);
  1368. inc(dstref.offset,4);
  1369. dec(len,4);
  1370. end;
  1371. { move a word }
  1372. if len>1 then
  1373. begin
  1374. a_load_ref_reg(list,OS_16,OS_16,srcref,hregister);
  1375. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  1376. inc(srcref.offset,2);
  1377. inc(dstref.offset,2);
  1378. dec(len,2);
  1379. end;
  1380. { move a single byte }
  1381. if len>0 then
  1382. begin
  1383. a_load_ref_reg(list,OS_8,OS_8,srcref,hregister);
  1384. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  1385. end
  1386. end
  1387. else
  1388. begin
  1389. iregister:=getaddressregister(list);
  1390. jregister:=getaddressregister(list);
  1391. { reference for move (An)+,(An)+ }
  1392. reference_reset(hp1,source.alignment);
  1393. hp1.base := iregister; { source register }
  1394. hp1.direction := dir_inc;
  1395. reference_reset(hp2,dest.alignment);
  1396. hp2.base := jregister;
  1397. hp2.direction := dir_inc;
  1398. { iregister = source }
  1399. { jregister = destination }
  1400. a_loadaddr_ref_reg(list,source,iregister);
  1401. a_loadaddr_ref_reg(list,dest,jregister);
  1402. { double word move only on 68020+ machines }
  1403. { because of possible alignment problems }
  1404. { use fast loop mode }
  1405. if (current_settings.cputype=cpu_MC68020) then
  1406. begin
  1407. //list.concat(tai_comment.create(strpnew('g_concatcopy tight copy loop 020+')));
  1408. helpsize := len - len mod 4;
  1409. len := len mod 4;
  1410. a_load_const_reg(list,OS_INT,(helpsize div 4)-1,hregister);
  1411. current_asmdata.getjumplabel(hl);
  1412. a_label(list,hl);
  1413. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  1414. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1415. if len > 1 then
  1416. begin
  1417. dec(len,2);
  1418. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  1419. end;
  1420. if len = 1 then
  1421. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1422. end
  1423. else
  1424. begin
  1425. { Fast 68010 loop mode with no possible alignment problems }
  1426. //list.concat(tai_comment.create(strpnew('g_concatcopy tight byte copy loop')));
  1427. a_load_const_reg(list,OS_INT,len - 1,hregister);
  1428. current_asmdata.getjumplabel(hl);
  1429. a_label(list,hl);
  1430. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1431. if current_settings.cputype in cpu_coldfire then
  1432. begin
  1433. { Coldfire does not support DBRA }
  1434. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1435. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1436. end
  1437. else
  1438. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1439. end;
  1440. end;
  1441. end;
  1442. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1443. var
  1444. hl : tasmlabel;
  1445. ai : taicpu;
  1446. cond : TAsmCond;
  1447. begin
  1448. if not(cs_check_overflow in current_settings.localswitches) then
  1449. exit;
  1450. current_asmdata.getjumplabel(hl);
  1451. if not ((def.typ=pointerdef) or
  1452. ((def.typ=orddef) and
  1453. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1454. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1455. cond:=C_VC
  1456. else
  1457. cond:=C_CC;
  1458. ai:=Taicpu.Op_Sym(A_Bxx,S_NO,hl);
  1459. ai.SetCondition(cond);
  1460. ai.is_jmp:=true;
  1461. list.concat(ai);
  1462. a_call_name(list,'FPC_OVERFLOW',false);
  1463. a_label(list,hl);
  1464. end;
  1465. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1466. begin
  1467. { Carl's original code used 2x MOVE instead of LINK when localsize = 0.
  1468. However, a LINK seems faster than two moves on everything from 68000
  1469. to '060, so the two move branch here was dropped. (KB) }
  1470. if not nostackframe then
  1471. begin
  1472. { size can't be negative }
  1473. if (localsize < 0) then
  1474. internalerror(2006122601);
  1475. if (localsize > high(smallint)) then
  1476. begin
  1477. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1478. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1479. end
  1480. else
  1481. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1482. end;
  1483. end;
  1484. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1485. var
  1486. r,hregister : TRegister;
  1487. ref : TReference;
  1488. ref2: TReference;
  1489. begin
  1490. if not nostackframe then
  1491. begin
  1492. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1493. { if parasize is less than zero here, we probably have a cdecl function.
  1494. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1495. 68k GCC uses two different methods to free the stack, depending if the target
  1496. architecture supports RTD or not, and one does callee side, the other does
  1497. caller side free, which looks like a PITA to support. We have to figure this
  1498. out later. More info welcomed. (KB) }
  1499. if (parasize > 0) and not (current_procinfo.procdef.proccalloption in clearstack_pocalls) then
  1500. begin
  1501. if current_settings.cputype=cpu_mc68020 then
  1502. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1503. else
  1504. begin
  1505. { We must pull the PC Counter from the stack, before }
  1506. { restoring the stack pointer, otherwise the PC would }
  1507. { point to nowhere! }
  1508. { Instead of doing a slow copy of the return address while trying }
  1509. { to feed it to the RTS instruction, load the PC to A0 (scratch reg) }
  1510. { then free up the stack allocated for paras, then use a JMP (A0) to }
  1511. { return to the caller with the paras freed. (KB) }
  1512. hregister:=NR_A0;
  1513. cg.a_reg_alloc(list,hregister);
  1514. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1515. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1516. { instead of using a postincrement above (which also writes the }
  1517. { stackpointer reg) simply add 4 to the parasize, the instructions }
  1518. { below then take that size into account as well, so SP reg is only }
  1519. { written once (KB) }
  1520. parasize:=parasize+4;
  1521. r:=NR_SP;
  1522. { can we do a quick addition ... }
  1523. if (parasize < 9) then
  1524. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1525. else { nope ... }
  1526. begin
  1527. reference_reset_base(ref2,NR_STACK_POINTER_REG,parasize,4);
  1528. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref2,r));
  1529. end;
  1530. reference_reset_base(ref,hregister,0,4);
  1531. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1532. end;
  1533. end
  1534. else
  1535. list.concat(taicpu.op_none(A_RTS,S_NO));
  1536. end
  1537. else
  1538. begin
  1539. list.concat(taicpu.op_none(A_RTS,S_NO));
  1540. end;
  1541. { Routines with the poclearstack flag set use only a ret.
  1542. also routines with parasize=0 }
  1543. { TODO: figure out if these are still relevant to us (KB) }
  1544. (*
  1545. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1546. begin
  1547. { complex return values are removed from stack in C code PM }
  1548. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1549. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1550. else
  1551. list.concat(taicpu.op_none(A_RTS,S_NO));
  1552. end
  1553. else if (parasize=0) then
  1554. begin
  1555. list.concat(taicpu.op_none(A_RTS,S_NO));
  1556. end
  1557. else
  1558. *)
  1559. end;
  1560. procedure tcg68k.g_save_registers(list:TAsmList);
  1561. var
  1562. dataregs: tcpuregisterset;
  1563. addrregs: tcpuregisterset;
  1564. href : treference;
  1565. hreg : tregister;
  1566. size : longint;
  1567. r : integer;
  1568. begin
  1569. { The code generated by the section below, particularly the movem.l
  1570. instruction is known to cause an issue when compiled by some GNU
  1571. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1572. when you run into this problem, just call inherited here instead
  1573. to skip the movem.l generation. But better just use working GNU
  1574. AS version instead. (KB) }
  1575. dataregs:=[];
  1576. addrregs:=[];
  1577. { calculate temp. size }
  1578. size:=0;
  1579. hreg:=NR_NO;
  1580. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1581. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1582. begin
  1583. hreg:=newreg(R_INTREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1584. inc(size,sizeof(aint));
  1585. dataregs:=dataregs + [saved_standard_registers[r]];
  1586. end;
  1587. if uses_registers(R_ADDRESSREGISTER) then
  1588. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1589. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1590. begin
  1591. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1592. inc(size,sizeof(aint));
  1593. addrregs:=addrregs + [saved_address_registers[r]];
  1594. end;
  1595. { 68k has no MM registers }
  1596. if uses_registers(R_MMREGISTER) then
  1597. internalerror(2014030201);
  1598. if size>0 then
  1599. begin
  1600. tg.GetTemp(list,size,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1601. include(current_procinfo.flags,pi_has_saved_regs);
  1602. { Copy registers to temp }
  1603. { NOTE: virtual registers allocated here won't be translated --> no higher-level stuff. }
  1604. href:=current_procinfo.save_regs_ref;
  1605. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1606. begin
  1607. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1608. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1609. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1610. end;
  1611. if size = sizeof(aint) then
  1612. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href))
  1613. else
  1614. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,href));
  1615. end;
  1616. end;
  1617. procedure tcg68k.g_restore_registers(list:TAsmList);
  1618. var
  1619. dataregs: tcpuregisterset;
  1620. addrregs: tcpuregisterset;
  1621. href : treference;
  1622. r : integer;
  1623. hreg : tregister;
  1624. size : longint;
  1625. begin
  1626. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1627. dataregs:=[];
  1628. addrregs:=[];
  1629. if not(pi_has_saved_regs in current_procinfo.flags) then
  1630. exit;
  1631. { Copy registers from temp }
  1632. size:=0;
  1633. hreg:=NR_NO;
  1634. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1635. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1636. begin
  1637. inc(size,sizeof(aint));
  1638. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1639. { Allocate register so the optimizer does not remove the load }
  1640. a_reg_alloc(list,hreg);
  1641. dataregs:=dataregs + [saved_standard_registers[r]];
  1642. end;
  1643. if uses_registers(R_ADDRESSREGISTER) then
  1644. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1645. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1646. begin
  1647. inc(size,sizeof(aint));
  1648. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1649. { Allocate register so the optimizer does not remove the load }
  1650. a_reg_alloc(list,hreg);
  1651. addrregs:=addrregs + [saved_address_registers[r]];
  1652. end;
  1653. { 68k has no MM registers }
  1654. if uses_registers(R_MMREGISTER) then
  1655. internalerror(2014030202);
  1656. { Restore registers from temp }
  1657. href:=current_procinfo.save_regs_ref;
  1658. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1659. begin
  1660. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1661. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1662. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1663. end;
  1664. if size = sizeof(aint) then
  1665. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,hreg))
  1666. else
  1667. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs));
  1668. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1669. end;
  1670. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  1671. begin
  1672. case _newsize of
  1673. OS_S16, OS_16:
  1674. case _oldsize of
  1675. OS_S8:
  1676. begin { 8 -> 16 bit sign extend }
  1677. if (isaddressregister(reg)) then
  1678. internalerror(2014031201);
  1679. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1680. end;
  1681. OS_8: { 8 -> 16 bit zero extend }
  1682. begin
  1683. if (current_settings.cputype in cpu_coldfire) then
  1684. { ColdFire has no ANDI.W }
  1685. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  1686. else
  1687. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  1688. end;
  1689. end;
  1690. OS_S32, OS_32:
  1691. case _oldsize of
  1692. OS_S8:
  1693. begin { 8 -> 32 bit sign extend }
  1694. if (isaddressregister(reg)) then
  1695. internalerror(2014031202);
  1696. if (current_settings.cputype = cpu_MC68000) then
  1697. begin
  1698. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1699. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1700. end
  1701. else
  1702. begin
  1703. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  1704. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1705. end;
  1706. end;
  1707. OS_8: { 8 -> 32 bit zero extend }
  1708. begin
  1709. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1710. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1711. end;
  1712. OS_S16: { 16 -> 32 bit sign extend }
  1713. begin
  1714. if (isaddressregister(reg)) then
  1715. internalerror(2014031203);
  1716. //list.concat(tai_comment.create(strpnew('sign extend word')));
  1717. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1718. end;
  1719. OS_16:
  1720. begin
  1721. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1722. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1723. end;
  1724. end;
  1725. end; { otherwise the size is already correct }
  1726. end;
  1727. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1728. begin
  1729. sign_extend(list, _oldsize, OS_INT, reg);
  1730. end;
  1731. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1732. var
  1733. ai : taicpu;
  1734. begin
  1735. if cond=OC_None then
  1736. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1737. else
  1738. begin
  1739. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1740. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1741. end;
  1742. ai.is_jmp:=true;
  1743. list.concat(ai);
  1744. end;
  1745. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  1746. operations on an address register. if the register is a dataregister anyway, it
  1747. just returns it untouched.}
  1748. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  1749. var
  1750. scratch_reg: TRegister;
  1751. instr: Taicpu;
  1752. begin
  1753. if isaddressregister(reg) then
  1754. begin
  1755. scratch_reg:=getintregister(list,OS_INT);
  1756. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg);
  1757. add_move_instruction(instr);
  1758. list.concat(instr);
  1759. result:=scratch_reg;
  1760. end
  1761. else
  1762. result:=reg;
  1763. end;
  1764. { moves source register to destination register, if the two are not the same. can be used in pair
  1765. with force_to_dataregister() }
  1766. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  1767. var
  1768. instr: Taicpu;
  1769. begin
  1770. if (src <> dest) then
  1771. begin
  1772. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  1773. add_move_instruction(instr);
  1774. list.concat(instr);
  1775. end;
  1776. end;
  1777. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1778. var
  1779. hsym : tsym;
  1780. href : treference;
  1781. paraloc : Pcgparalocation;
  1782. begin
  1783. { calculate the parameter info for the procdef }
  1784. procdef.init_paraloc_info(callerside);
  1785. hsym:=tsym(procdef.parast.Find('self'));
  1786. if not(assigned(hsym) and
  1787. (hsym.typ=paravarsym)) then
  1788. internalerror(2013100702);
  1789. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1790. while paraloc<>nil do
  1791. with paraloc^ do
  1792. begin
  1793. case loc of
  1794. LOC_REGISTER:
  1795. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  1796. LOC_REFERENCE:
  1797. begin
  1798. { offset in the wrapper needs to be adjusted for the stored
  1799. return address }
  1800. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  1801. { plain 68k could use SUBI on href directly, but this way it works on Coldfire too
  1802. and it's probably smaller code for the majority of cases (if ioffset small, the
  1803. load will use MOVEQ) (KB) }
  1804. a_load_const_reg(list,OS_ADDR,ioffset,NR_D0);
  1805. list.concat(taicpu.op_reg_ref(A_SUB,S_L,NR_D0,href));
  1806. end
  1807. else
  1808. internalerror(2013100703);
  1809. end;
  1810. paraloc:=next;
  1811. end;
  1812. end;
  1813. procedure tcg68k.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1814. procedure getselftoa0(offs:longint);
  1815. var
  1816. href : treference;
  1817. selfoffsetfromsp : longint;
  1818. begin
  1819. { move.l offset(%sp),%a0 }
  1820. { framepointer is pushed for nested procs }
  1821. if procdef.parast.symtablelevel>normal_function_level then
  1822. selfoffsetfromsp:=sizeof(aint)
  1823. else
  1824. selfoffsetfromsp:=0;
  1825. reference_reset_base(href,NR_SP,selfoffsetfromsp+offs,4);
  1826. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_A0);
  1827. end;
  1828. procedure loadvmttoa0;
  1829. var
  1830. href : treference;
  1831. begin
  1832. { move.l (%a0),%a0 ; load vmt}
  1833. reference_reset_base(href,NR_A0,0,4);
  1834. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_A0);
  1835. end;
  1836. procedure op_ona0methodaddr;
  1837. var
  1838. href : treference;
  1839. begin
  1840. if (procdef.extnumber=$ffff) then
  1841. Internalerror(2013100701);
  1842. reference_reset_base(href,NR_A0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),4);
  1843. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,NR_A0));
  1844. reference_reset_base(href,NR_A0,0,4);
  1845. list.concat(taicpu.op_ref(A_JMP,S_NO,href));
  1846. end;
  1847. var
  1848. make_global : boolean;
  1849. begin
  1850. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1851. Internalerror(200006137);
  1852. if not assigned(procdef.struct) or
  1853. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1854. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1855. Internalerror(200006138);
  1856. if procdef.owner.symtabletype<>ObjectSymtable then
  1857. Internalerror(200109191);
  1858. make_global:=false;
  1859. if (not current_module.is_unit) or
  1860. create_smartlink or
  1861. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1862. make_global:=true;
  1863. if make_global then
  1864. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1865. else
  1866. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1867. { set param1 interface to self }
  1868. g_adjust_self_value(list,procdef,ioffset);
  1869. { case 4 }
  1870. if (po_virtualmethod in procdef.procoptions) and
  1871. not is_objectpascal_helper(procdef.struct) then
  1872. begin
  1873. getselftoa0(4);
  1874. loadvmttoa0;
  1875. op_ona0methodaddr;
  1876. end
  1877. { case 0 }
  1878. else
  1879. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1880. List.concat(Tai_symbol_end.Createname(labelname));
  1881. end;
  1882. procedure tcg68k.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1883. begin
  1884. list.concat(taicpu.op_const_reg(A_SUB,S_L,localsize,NR_STACK_POINTER_REG));
  1885. end;
  1886. {****************************************************************************}
  1887. { TCG64F68K }
  1888. {****************************************************************************}
  1889. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  1890. var
  1891. opcode : tasmop;
  1892. xopcode : tasmop;
  1893. instr : taicpu;
  1894. begin
  1895. opcode := topcg2tasmop[op];
  1896. xopcode := topcg2tasmopx[op];
  1897. case op of
  1898. OP_ADD,OP_SUB:
  1899. begin
  1900. { if one of these three registers is an address
  1901. register, we'll really get into problems! }
  1902. if isaddressregister(regdst.reglo) or
  1903. isaddressregister(regdst.reghi) or
  1904. isaddressregister(regsrc.reghi) then
  1905. internalerror(2014030101);
  1906. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  1907. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  1908. end;
  1909. OP_AND,OP_OR:
  1910. begin
  1911. { at least one of the registers must be a data register }
  1912. if (isaddressregister(regdst.reglo) and
  1913. isaddressregister(regsrc.reglo)) or
  1914. (isaddressregister(regsrc.reghi) and
  1915. isaddressregister(regdst.reghi)) then
  1916. internalerror(2014030102);
  1917. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1918. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1919. end;
  1920. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1921. OP_IDIV,OP_DIV,
  1922. OP_IMUL,OP_MUL:
  1923. internalerror(2002081701);
  1924. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1925. OP_SAR,OP_SHL,OP_SHR:
  1926. internalerror(2002081702);
  1927. OP_XOR:
  1928. begin
  1929. if isaddressregister(regdst.reglo) or
  1930. isaddressregister(regsrc.reglo) or
  1931. isaddressregister(regsrc.reghi) or
  1932. isaddressregister(regdst.reghi) then
  1933. internalerror(2014030103);
  1934. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1935. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1936. end;
  1937. OP_NEG,OP_NOT:
  1938. begin
  1939. if isaddressregister(regdst.reglo) or
  1940. isaddressregister(regdst.reghi) then
  1941. internalerror(2014030104);
  1942. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  1943. cg.add_move_instruction(instr);
  1944. list.concat(instr);
  1945. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  1946. cg.add_move_instruction(instr);
  1947. list.concat(instr);
  1948. if (op = OP_NOT) then
  1949. xopcode:=opcode;
  1950. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  1951. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  1952. end;
  1953. end; { end case }
  1954. end;
  1955. procedure tcg64f68k.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  1956. var
  1957. tempref : treference;
  1958. begin
  1959. case op of
  1960. OP_NEG,OP_NOT:
  1961. begin
  1962. a_load64_ref_reg(list,ref,reg);
  1963. a_op64_reg_reg(list,op,size,reg,reg);
  1964. end;
  1965. OP_AND,OP_OR:
  1966. begin
  1967. tempref:=ref;
  1968. tcg68k(cg).fixref(list,tempref);
  1969. inc(tempref.offset,4);
  1970. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reglo));
  1971. dec(tempref.offset,4);
  1972. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reghi));
  1973. end;
  1974. else
  1975. { XOR does not allow reference for source; ADD/SUB do not allow reference for
  1976. high dword, although low dword can still be handled directly. }
  1977. inherited a_op64_ref_reg(list,op,size,ref,reg);
  1978. end;
  1979. end;
  1980. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  1981. var
  1982. lowvalue : cardinal;
  1983. highvalue : cardinal;
  1984. opcode : tasmop;
  1985. xopcode : tasmop;
  1986. hreg : tregister;
  1987. begin
  1988. { is it optimized out ? }
  1989. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  1990. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  1991. exit; }
  1992. lowvalue := cardinal(value);
  1993. highvalue := value shr 32;
  1994. opcode := topcg2tasmop[op];
  1995. xopcode := topcg2tasmopx[op];
  1996. { the destination registers must be data registers }
  1997. if isaddressregister(regdst.reglo) or
  1998. isaddressregister(regdst.reghi) then
  1999. internalerror(2014030105);
  2000. case op of
  2001. OP_ADD,OP_SUB:
  2002. begin
  2003. hreg:=cg.getintregister(list,OS_INT);
  2004. { cg.a_load_const_reg provides optimized loading to register for special cases }
  2005. cg.a_load_const_reg(list,OS_S32,longint(highvalue),hreg);
  2006. { don't use cg.a_op_const_reg() here, because a possible optimized
  2007. ADDQ/SUBQ wouldn't set the eXtend bit }
  2008. list.concat(taicpu.op_const_reg(opcode,S_L,lowvalue,regdst.reglo));
  2009. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  2010. end;
  2011. OP_AND,OP_OR,OP_XOR:
  2012. begin
  2013. cg.a_op_const_reg(list,op,OS_S32,longint(lowvalue),regdst.reglo);
  2014. cg.a_op_const_reg(list,op,OS_S32,longint(highvalue),regdst.reghi);
  2015. end;
  2016. { this is handled in 1st pass for 32-bit cpus (helper call) }
  2017. OP_IDIV,OP_DIV,
  2018. OP_IMUL,OP_MUL:
  2019. internalerror(2002081701);
  2020. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  2021. OP_SAR,OP_SHL,OP_SHR:
  2022. internalerror(2002081702);
  2023. { these should have been handled already by earlier passes }
  2024. OP_NOT,OP_NEG:
  2025. internalerror(2012110403);
  2026. end; { end case }
  2027. end;
  2028. procedure create_codegen;
  2029. begin
  2030. cg := tcg68k.create;
  2031. cg64 :=tcg64f68k.create;
  2032. end;
  2033. end.