cgcpu.pas 93 KB

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