cgcpu.pas 99 KB

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