cgcpu.pas 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577
  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, href2 : 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. //list.concat(tai_comment.create(strpnew('a_loadfpu_ref_cgpara copy')));
  973. cgpara.check_simple_location;
  974. reference_reset_base(href, NR_STACK_POINTER_REG, 0, cgpara.alignment, []);
  975. href.direction := dir_dec;
  976. case size of
  977. OS_F64:
  978. begin
  979. href2:=ref;
  980. inc(href2.offset,8);
  981. fixref(list,href2,true);
  982. href2.direction := dir_dec;
  983. cg.a_load_ref_ref(list,OS_32,OS_32,href2,href);
  984. cg.a_load_ref_ref(list,OS_32,OS_32,href2,href);
  985. end;
  986. OS_F32:
  987. cg.a_load_ref_ref(list,OS_32,OS_32,ref,href);
  988. else
  989. internalerror(2017052110);
  990. end;
  991. end
  992. else
  993. begin
  994. //list.concat(tai_comment.create(strpnew('a_loadfpu_ref_cgpara inherited')));
  995. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  996. end;
  997. end;
  998. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  999. var
  1000. scratch_reg : tregister;
  1001. scratch_reg2: tregister;
  1002. opcode : tasmop;
  1003. begin
  1004. optimize_op_const(size, op, a);
  1005. opcode := topcg2tasmop[op];
  1006. case op of
  1007. OP_NONE :
  1008. begin
  1009. { Opcode is optimized away }
  1010. end;
  1011. OP_MOVE :
  1012. begin
  1013. { Optimized, replaced with a simple load }
  1014. a_load_const_reg(list,size,a,reg);
  1015. end;
  1016. OP_ADD,
  1017. OP_SUB:
  1018. begin
  1019. { add/sub works the same way, so have it unified here }
  1020. if (a >= 1) and (a <= 8) then
  1021. if (op = OP_ADD) then
  1022. opcode:=A_ADDQ
  1023. else
  1024. opcode:=A_SUBQ;
  1025. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  1026. end;
  1027. OP_AND,
  1028. OP_OR,
  1029. OP_XOR:
  1030. begin
  1031. scratch_reg := force_to_dataregister(list, size, reg);
  1032. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1033. move_if_needed(list, size, scratch_reg, reg);
  1034. end;
  1035. OP_DIV,
  1036. OP_IDIV:
  1037. begin
  1038. internalerror(20020816);
  1039. end;
  1040. OP_MUL,
  1041. OP_IMUL:
  1042. begin
  1043. { NOTE: better have this as fast as possible on every CPU in all cases,
  1044. because the compiler uses OP_IMUL for array indexing... (KB) }
  1045. { ColdFire doesn't support MULS/MULU <imm>,dX }
  1046. if current_settings.cputype in cpu_coldfire then
  1047. begin
  1048. { move const to a register first }
  1049. scratch_reg := getintregister(list,OS_INT);
  1050. a_load_const_reg(list, size, a, scratch_reg);
  1051. { do the multiplication }
  1052. scratch_reg2 := force_to_dataregister(list, size, reg);
  1053. sign_extend(list, size, scratch_reg2);
  1054. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  1055. { move the value back to the original register }
  1056. move_if_needed(list, size, scratch_reg2, reg);
  1057. end
  1058. else
  1059. begin
  1060. if current_settings.cputype = cpu_mc68020 then
  1061. begin
  1062. { do the multiplication }
  1063. scratch_reg := force_to_dataregister(list, size, reg);
  1064. sign_extend(list, size, scratch_reg);
  1065. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  1066. { move the value back to the original register }
  1067. move_if_needed(list, size, scratch_reg, reg);
  1068. end
  1069. else
  1070. { Fallback branch, plain 68000 for now }
  1071. if not optimize_const_mul_to_shift_sub_add(list, 5, a, size, reg) then
  1072. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  1073. if op = OP_MUL then
  1074. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  1075. else
  1076. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1077. end;
  1078. end;
  1079. OP_ROL,
  1080. OP_ROR,
  1081. OP_SAR,
  1082. OP_SHL,
  1083. OP_SHR :
  1084. begin
  1085. scratch_reg := force_to_dataregister(list, size, reg);
  1086. sign_extend(list, size, scratch_reg);
  1087. { some special cases which can generate smarter code
  1088. using the SWAP instruction }
  1089. if (a = 16) then
  1090. begin
  1091. if (op = OP_SHL) then
  1092. begin
  1093. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1094. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1095. end
  1096. else if (op = OP_SHR) then
  1097. begin
  1098. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1099. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1100. end
  1101. else if (op = OP_SAR) then
  1102. begin
  1103. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1104. list.concat(taicpu.op_reg(A_EXT,S_L,scratch_reg));
  1105. end
  1106. else if (op = OP_ROR) or (op = OP_ROL) then
  1107. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg))
  1108. end
  1109. else if (a >= 1) and (a <= 8) then
  1110. begin
  1111. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1112. end
  1113. else if (a >= 9) and (a < 16) then
  1114. begin
  1115. { Use two ops instead of const -> reg + shift with reg, because
  1116. this way is the same in length and speed but has less register
  1117. pressure }
  1118. list.concat(taicpu.op_const_reg(opcode, S_L, 8, scratch_reg));
  1119. list.concat(taicpu.op_const_reg(opcode, S_L, a-8, scratch_reg));
  1120. end
  1121. else
  1122. begin
  1123. { move const to a register first }
  1124. scratch_reg2 := getintregister(list,OS_INT);
  1125. a_load_const_reg(list, size, a, scratch_reg2);
  1126. { do the operation }
  1127. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1128. end;
  1129. { move the value back to the original register }
  1130. move_if_needed(list, size, scratch_reg, reg);
  1131. end;
  1132. else
  1133. internalerror(20020729);
  1134. end;
  1135. end;
  1136. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1137. var
  1138. opcode: tasmop;
  1139. opsize: topsize;
  1140. href : treference;
  1141. hreg : tregister;
  1142. begin
  1143. optimize_op_const(size, op, a);
  1144. opcode := topcg2tasmop[op];
  1145. opsize := TCGSize2OpSize[size];
  1146. { on ColdFire all arithmetic operations are only possible on 32bit }
  1147. if needs_unaligned(ref.alignment,size) or
  1148. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)
  1149. and not (op in [OP_NONE,OP_MOVE])) then
  1150. begin
  1151. inherited;
  1152. exit;
  1153. end;
  1154. case op of
  1155. OP_NONE :
  1156. begin
  1157. { opcode was optimized away }
  1158. end;
  1159. OP_MOVE :
  1160. begin
  1161. { Optimized, replaced with a simple load }
  1162. a_load_const_ref(list,size,a,ref);
  1163. end;
  1164. OP_AND,
  1165. OP_OR,
  1166. OP_XOR :
  1167. begin
  1168. //list.concat(tai_comment.create(strpnew('a_op_const_ref: bitwise')));
  1169. hreg:=getintregister(list,size);
  1170. a_load_const_reg(list,size,a,hreg);
  1171. href:=ref;
  1172. fixref(list,href,false);
  1173. list.concat(taicpu.op_reg_ref(opcode, opsize, hreg, href));
  1174. end;
  1175. OP_ADD,
  1176. OP_SUB :
  1177. begin
  1178. href:=ref;
  1179. { add/sub works the same way, so have it unified here }
  1180. if (a >= 1) and (a <= 8) then
  1181. begin
  1182. fixref(list,href,false);
  1183. if (op = OP_ADD) then
  1184. opcode:=A_ADDQ
  1185. else
  1186. opcode:=A_SUBQ;
  1187. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1188. end
  1189. else
  1190. if not(current_settings.cputype in cpu_coldfire) then
  1191. begin
  1192. fixref(list,href,false);
  1193. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1194. end
  1195. else
  1196. { on ColdFire, ADDI/SUBI cannot act on memory
  1197. so we can only go through a register }
  1198. inherited;
  1199. end;
  1200. else begin
  1201. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1202. inherited;
  1203. end;
  1204. end;
  1205. end;
  1206. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1207. var
  1208. hreg1, hreg2: tregister;
  1209. opcode : tasmop;
  1210. opsize : topsize;
  1211. begin
  1212. opcode := topcg2tasmop[op];
  1213. if current_settings.cputype in cpu_coldfire then
  1214. opsize := S_L
  1215. else
  1216. opsize := TCGSize2OpSize[size];
  1217. case op of
  1218. OP_ADD,
  1219. OP_SUB:
  1220. begin
  1221. if current_settings.cputype in cpu_coldfire then
  1222. begin
  1223. { operation only allowed only a longword }
  1224. sign_extend(list, size, src);
  1225. sign_extend(list, size, dst);
  1226. end;
  1227. list.concat(taicpu.op_reg_reg(opcode, opsize, src, dst));
  1228. end;
  1229. OP_AND,OP_OR,
  1230. OP_SAR,OP_SHL,
  1231. OP_SHR,OP_XOR:
  1232. begin
  1233. { load to data registers }
  1234. hreg1 := force_to_dataregister(list, size, src);
  1235. hreg2 := force_to_dataregister(list, size, dst);
  1236. if current_settings.cputype in cpu_coldfire then
  1237. begin
  1238. { operation only allowed only a longword }
  1239. {!***************************************
  1240. in the case of shifts, the value to
  1241. shift by, should already be valid, so
  1242. no need to sign extend the value
  1243. !
  1244. }
  1245. if op in [OP_AND,OP_OR,OP_XOR] then
  1246. sign_extend(list, size, hreg1);
  1247. sign_extend(list, size, hreg2);
  1248. end;
  1249. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1250. { move back result into destination register }
  1251. move_if_needed(list, size, hreg2, dst);
  1252. end;
  1253. OP_DIV,
  1254. OP_IDIV :
  1255. begin
  1256. internalerror(20020816);
  1257. end;
  1258. OP_MUL,
  1259. OP_IMUL:
  1260. begin
  1261. if (current_settings.cputype <> cpu_mc68020) and
  1262. (not (current_settings.cputype in cpu_coldfire)) then
  1263. if op = OP_MUL then
  1264. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_dword')
  1265. else
  1266. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_longint')
  1267. else
  1268. begin
  1269. { 68020+ and ColdFire codepath, probably could be improved }
  1270. hreg1 := force_to_dataregister(list, size, src);
  1271. hreg2 := force_to_dataregister(list, size, dst);
  1272. sign_extend(list, size, hreg1);
  1273. sign_extend(list, size, hreg2);
  1274. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1275. { move back result into destination register }
  1276. move_if_needed(list, size, hreg2, dst);
  1277. end;
  1278. end;
  1279. OP_NEG,
  1280. OP_NOT :
  1281. begin
  1282. { if there are two operands, move the register,
  1283. since the operation will only be done on the result
  1284. register. }
  1285. if (src<>dst) then
  1286. a_load_reg_reg(list,size,size,src,dst);
  1287. hreg2 := force_to_dataregister(list, size, dst);
  1288. { coldfire only supports long version }
  1289. if current_settings.cputype in cpu_ColdFire then
  1290. sign_extend(list, size, hreg2);
  1291. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1292. { move back the result to the result register if needed }
  1293. move_if_needed(list, size, hreg2, dst);
  1294. end;
  1295. else
  1296. internalerror(20020729);
  1297. end;
  1298. end;
  1299. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1300. var
  1301. opcode : tasmop;
  1302. opsize : topsize;
  1303. href : treference;
  1304. hreg : tregister;
  1305. begin
  1306. opcode := topcg2tasmop[op];
  1307. opsize := TCGSize2OpSize[size];
  1308. { on ColdFire all arithmetic operations are only possible on 32bit
  1309. and addressing modes are limited }
  1310. if needs_unaligned(ref.alignment,size) or
  1311. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1312. begin
  1313. //list.concat(tai_comment.create(strpnew('a_op_reg_ref: inherited #1')));
  1314. inherited;
  1315. exit;
  1316. end;
  1317. case op of
  1318. OP_ADD,
  1319. OP_SUB,
  1320. OP_OR,
  1321. OP_XOR,
  1322. OP_AND:
  1323. begin
  1324. //list.concat(tai_comment.create(strpnew('a_op_reg_ref: normal op')));
  1325. href:=ref;
  1326. fixref(list,href,false);
  1327. { areg -> ref arithmetic operations are impossible on 68k }
  1328. hreg:=force_to_dataregister(list,size,reg);
  1329. { add/sub works the same way, so have it unified here }
  1330. list.concat(taicpu.op_reg_ref(opcode, opsize, hreg, href));
  1331. end;
  1332. else begin
  1333. //list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited #2')));
  1334. inherited;
  1335. end;
  1336. end;
  1337. end;
  1338. procedure tcg68k.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1339. var
  1340. opcode : tasmop;
  1341. opsize : topsize;
  1342. href : treference;
  1343. hreg : tregister;
  1344. begin
  1345. opcode := topcg2tasmop[op];
  1346. opsize := TCGSize2OpSize[size];
  1347. { on ColdFire all arithmetic operations are only possible on 32bit
  1348. and addressing modes are limited }
  1349. if needs_unaligned(ref.alignment,size) or
  1350. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1351. begin
  1352. //list.concat(tai_comment.create(strpnew('a_op_ref_reg: inherited #1')));
  1353. inherited;
  1354. exit;
  1355. end;
  1356. case op of
  1357. OP_ADD,
  1358. OP_SUB,
  1359. OP_OR,
  1360. OP_AND,
  1361. OP_MUL,
  1362. OP_IMUL:
  1363. begin
  1364. //list.concat(tai_comment.create(strpnew('a_op_ref_reg: normal op')));
  1365. href:=ref;
  1366. { Coldfire doesn't support d(Ax,Dx) for long MULx... }
  1367. fixref(list,href,(op in [OP_MUL,OP_IMUL]) and
  1368. (current_settings.cputype in cpu_coldfire));
  1369. list.concat(taicpu.op_ref_reg(opcode, opsize, href, reg));
  1370. end;
  1371. else begin
  1372. //list.concat(tai_comment.create(strpnew('a_op_ref_reg inherited #2')));
  1373. inherited;
  1374. end;
  1375. end;
  1376. end;
  1377. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1378. l : tasmlabel);
  1379. var
  1380. hregister : tregister;
  1381. instr : taicpu;
  1382. need_temp_reg : boolean;
  1383. temp_size: topsize;
  1384. begin
  1385. need_temp_reg := false;
  1386. { plain 68000 doesn't support address registers for TST }
  1387. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1388. (a = 0) and isaddressregister(reg);
  1389. { ColdFire doesn't support address registers for CMPI }
  1390. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1391. and (a <> 0) and isaddressregister(reg));
  1392. if need_temp_reg then
  1393. begin
  1394. hregister := getintregister(list,OS_INT);
  1395. temp_size := TCGSize2OpSize[size];
  1396. if temp_size < S_W then
  1397. temp_size := S_W;
  1398. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1399. add_move_instruction(instr);
  1400. list.concat(instr);
  1401. reg := hregister;
  1402. { do sign extension if size had to be modified }
  1403. if temp_size <> TCGSize2OpSize[size] then
  1404. begin
  1405. sign_extend(list, size, reg);
  1406. size:=OS_INT;
  1407. end;
  1408. end;
  1409. if a = 0 then
  1410. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1411. else
  1412. begin
  1413. { ColdFire ISA A also needs S_L for CMPI }
  1414. { Note: older QEMU pukes from CMPI sizes <> .L even on ISA B/C, but
  1415. it's actually *LEGAL*, see CFPRM, page 4-30, the bug also seems
  1416. fixed in recent QEMU, but only when CPU cfv4e is forced, not by
  1417. default. (KB) }
  1418. if current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c,cpu_cfv4e]} then
  1419. begin
  1420. sign_extend(list, size, reg);
  1421. size:=OS_INT;
  1422. end;
  1423. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1424. end;
  1425. { emit the actual jump to the label }
  1426. a_jmp_cond(list,cmp_op,l);
  1427. end;
  1428. procedure tcg68k.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel);
  1429. var
  1430. tmpref: treference;
  1431. begin
  1432. { optimize for usage of TST here, so ref compares against zero, which is the
  1433. most common case by far in the RTL code at least (KB) }
  1434. if not needs_unaligned(ref.alignment,size) and (a = 0) then
  1435. begin
  1436. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label with TST')));
  1437. tmpref:=ref;
  1438. fixref(list,tmpref,false);
  1439. list.concat(taicpu.op_ref(A_TST,tcgsize2opsize[size],tmpref));
  1440. a_jmp_cond(list,cmp_op,l);
  1441. end
  1442. else
  1443. begin
  1444. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label inherited')));
  1445. inherited;
  1446. end;
  1447. end;
  1448. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1449. begin
  1450. if (current_settings.cputype in cpu_coldfire-[cpu_isa_b,cpu_isa_c,cpu_cfv4e]) then
  1451. begin
  1452. sign_extend(list,size,reg1);
  1453. sign_extend(list,size,reg2);
  1454. size:=OS_INT;
  1455. end;
  1456. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1457. { emit the actual jump to the label }
  1458. a_jmp_cond(list,cmp_op,l);
  1459. end;
  1460. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1461. var
  1462. ai: taicpu;
  1463. begin
  1464. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1465. ai.is_jmp := true;
  1466. list.concat(ai);
  1467. end;
  1468. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1469. var
  1470. ai: taicpu;
  1471. begin
  1472. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1473. ai.is_jmp := true;
  1474. list.concat(ai);
  1475. end;
  1476. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1477. var
  1478. ai : taicpu;
  1479. begin
  1480. if not (f in FloatResFlags) then
  1481. ai := Taicpu.op_sym(A_BXX,S_NO,l)
  1482. else
  1483. ai := Taicpu.op_sym(A_FBXX,S_NO,l);
  1484. ai.SetCondition(flags_to_cond(f));
  1485. ai.is_jmp := true;
  1486. list.concat(ai);
  1487. end;
  1488. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1489. var
  1490. ai : taicpu;
  1491. htrue: tasmlabel;
  1492. begin
  1493. if isaddressregister(reg) then
  1494. internalerror(2017051701);
  1495. if (f in FloatResFlags) then
  1496. begin
  1497. //list.concat(tai_comment.create(strpnew('flags2reg: float resflags')));
  1498. current_asmdata.getjumplabel(htrue);
  1499. a_load_const_reg(current_asmdata.CurrAsmList,OS_32,1,reg);
  1500. a_jmp_flags(list, f, htrue);
  1501. a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,reg);
  1502. a_label(current_asmdata.CurrAsmList,htrue);
  1503. exit;
  1504. end;
  1505. ai:=Taicpu.Op_reg(A_Sxx,S_B,reg);
  1506. ai.SetCondition(flags_to_cond(f));
  1507. list.concat(ai);
  1508. { Scc stores a complete byte of 1s, but the compiler expects only one
  1509. bit set, so ensure this is the case }
  1510. if not (current_settings.cputype in cpu_coldfire) then
  1511. begin
  1512. if size in [OS_S8,OS_8] then
  1513. list.concat(taicpu.op_reg(A_NEG,S_B,reg))
  1514. else
  1515. list.concat(taicpu.op_const_reg(A_AND,TCgSize2OpSize[size],1,reg));
  1516. end
  1517. else
  1518. list.concat(taicpu.op_const_reg(A_AND,S_L,1,reg));
  1519. end;
  1520. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1521. const
  1522. lentocgsize: array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  1523. var
  1524. helpsize : longint;
  1525. i : byte;
  1526. hregister : tregister;
  1527. iregister : tregister;
  1528. jregister : tregister;
  1529. hl : tasmlabel;
  1530. srcrefp,dstrefp : treference;
  1531. srcref,dstref : treference;
  1532. begin
  1533. if (len = 1) or ((len in [2,4]) and (current_settings.cputype <> cpu_mc68000)) then
  1534. begin
  1535. //list.concat(tai_comment.create(strpnew('g_concatcopy: small')));
  1536. a_load_ref_ref(list,lentocgsize[len],lentocgsize[len],source,dest);
  1537. exit;
  1538. end;
  1539. //list.concat(tai_comment.create(strpnew('g_concatcopy')));
  1540. hregister := getintregister(list,OS_INT);
  1541. iregister:=getaddressregister(list);
  1542. reference_reset_base(srcref,iregister,0,source.alignment,source.volatility);
  1543. srcrefp:=srcref;
  1544. srcrefp.direction := dir_inc;
  1545. jregister:=getaddressregister(list);
  1546. reference_reset_base(dstref,jregister,0,dest.alignment,dest.volatility);
  1547. dstrefp:=dstref;
  1548. dstrefp.direction := dir_inc;
  1549. { iregister = source }
  1550. { jregister = destination }
  1551. a_loadaddr_ref_reg(list,source,iregister);
  1552. a_loadaddr_ref_reg(list,dest,jregister);
  1553. if not (needs_unaligned(source.alignment,OS_INT) or needs_unaligned(dest.alignment,OS_INT)) then
  1554. begin
  1555. if not ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=16))) then
  1556. begin
  1557. //list.concat(tai_comment.create(strpnew('g_concatcopy tight copy loop 020+')));
  1558. helpsize := len - len mod 4;
  1559. len := len mod 4;
  1560. a_load_const_reg(list,OS_INT,(helpsize div 4)-1,hregister);
  1561. current_asmdata.getjumplabel(hl);
  1562. a_label(list,hl);
  1563. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcrefp,dstrefp));
  1564. if (current_settings.cputype in cpu_coldfire) or ((helpsize div 4)-1 > high(smallint)) then
  1565. begin
  1566. { Coldfire does not support DBRA, also it is word only }
  1567. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1568. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1569. end
  1570. else
  1571. list.concat(taicpu.op_reg_sym(A_DBRA,S_NO,hregister,hl));
  1572. end;
  1573. helpsize:=len div 4;
  1574. { move a dword x times }
  1575. for i:=1 to helpsize do
  1576. begin
  1577. dec(len,4);
  1578. if (len > 0) then
  1579. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcrefp,dstrefp))
  1580. else
  1581. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcref,dstref));
  1582. end;
  1583. { move a word }
  1584. if len>1 then
  1585. begin
  1586. dec(len,2);
  1587. if (len > 0) then
  1588. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,srcrefp,dstrefp))
  1589. else
  1590. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,srcref,dstref));
  1591. end;
  1592. { move a single byte }
  1593. if len>0 then
  1594. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,srcref,dstref));
  1595. end
  1596. else
  1597. begin
  1598. { Fast 68010 loop mode with no possible alignment problems }
  1599. //list.concat(tai_comment.create(strpnew('g_concatcopy tight byte copy loop')));
  1600. a_load_const_reg(list,OS_INT,len - 1,hregister);
  1601. current_asmdata.getjumplabel(hl);
  1602. a_label(list,hl);
  1603. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,srcrefp,dstrefp));
  1604. if (len - 1) > high(smallint) then
  1605. begin
  1606. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1607. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1608. end
  1609. else
  1610. list.concat(taicpu.op_reg_sym(A_DBRA,S_NO,hregister,hl));
  1611. end;
  1612. end;
  1613. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1614. var
  1615. hl : tasmlabel;
  1616. ai : taicpu;
  1617. cond : TAsmCond;
  1618. begin
  1619. if not(cs_check_overflow in current_settings.localswitches) then
  1620. exit;
  1621. current_asmdata.getjumplabel(hl);
  1622. if not ((def.typ=pointerdef) or
  1623. ((def.typ=orddef) and
  1624. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1625. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1626. cond:=C_VC
  1627. else
  1628. cond:=C_CC;
  1629. ai:=Taicpu.Op_Sym(A_Bxx,S_NO,hl);
  1630. ai.SetCondition(cond);
  1631. ai.is_jmp:=true;
  1632. list.concat(ai);
  1633. a_call_name(list,'FPC_OVERFLOW',false);
  1634. a_label(list,hl);
  1635. end;
  1636. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1637. begin
  1638. { Carl's original code used 2x MOVE instead of LINK when localsize = 0.
  1639. However, a LINK seems faster than two moves on everything from 68000
  1640. to '060, so the two move branch here was dropped. (KB) }
  1641. if not nostackframe then
  1642. begin
  1643. { size can't be negative }
  1644. localsize:=align(localsize,4);
  1645. if (localsize < 0) then
  1646. internalerror(2006122601);
  1647. if (localsize > high(smallint)) then
  1648. begin
  1649. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1650. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1651. end
  1652. else
  1653. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1654. end;
  1655. end;
  1656. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1657. var
  1658. r,hregister : TRegister;
  1659. ref : TReference;
  1660. ref2: TReference;
  1661. begin
  1662. if not nostackframe then
  1663. begin
  1664. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1665. { if parasize is less than zero here, we probably have a cdecl function.
  1666. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1667. 68k GCC uses two different methods to free the stack, depending if the target
  1668. architecture supports RTD or not, and one does callee side, the other does
  1669. caller side free, which looks like a PITA to support. We have to figure this
  1670. out later. More info welcomed. (KB) }
  1671. if (parasize > 0) and not (current_procinfo.procdef.proccalloption in clearstack_pocalls) then
  1672. begin
  1673. if current_settings.cputype=cpu_mc68020 then
  1674. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1675. else
  1676. begin
  1677. { We must pull the PC Counter from the stack, before }
  1678. { restoring the stack pointer, otherwise the PC would }
  1679. { point to nowhere! }
  1680. { Instead of doing a slow copy of the return address while trying }
  1681. { to feed it to the RTS instruction, load the PC to A0 (scratch reg) }
  1682. { then free up the stack allocated for paras, then use a JMP (A0) to }
  1683. { return to the caller with the paras freed. (KB) }
  1684. hregister:=NR_A0;
  1685. cg.a_reg_alloc(list,hregister);
  1686. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4,[]);
  1687. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1688. { instead of using a postincrement above (which also writes the }
  1689. { stackpointer reg) simply add 4 to the parasize, the instructions }
  1690. { below then take that size into account as well, so SP reg is only }
  1691. { written once (KB) }
  1692. parasize:=parasize+4;
  1693. r:=NR_SP;
  1694. { can we do a quick addition ... }
  1695. if (parasize < 9) then
  1696. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1697. else { nope ... }
  1698. begin
  1699. reference_reset_base(ref2,NR_STACK_POINTER_REG,parasize,4,[]);
  1700. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref2,r));
  1701. end;
  1702. reference_reset_base(ref,hregister,0,4,[]);
  1703. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1704. end;
  1705. end
  1706. else
  1707. list.concat(taicpu.op_none(A_RTS,S_NO));
  1708. end
  1709. else
  1710. begin
  1711. list.concat(taicpu.op_none(A_RTS,S_NO));
  1712. end;
  1713. { Routines with the poclearstack flag set use only a ret.
  1714. also routines with parasize=0 }
  1715. { TODO: figure out if these are still relevant to us (KB) }
  1716. (*
  1717. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1718. begin
  1719. { complex return values are removed from stack in C code PM }
  1720. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1721. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1722. else
  1723. list.concat(taicpu.op_none(A_RTS,S_NO));
  1724. end
  1725. else if (parasize=0) then
  1726. begin
  1727. list.concat(taicpu.op_none(A_RTS,S_NO));
  1728. end
  1729. else
  1730. *)
  1731. end;
  1732. procedure tcg68k.g_save_registers(list:TAsmList);
  1733. var
  1734. dataregs: tcpuregisterset;
  1735. addrregs: tcpuregisterset;
  1736. fpuregs: tcpuregisterset;
  1737. href : treference;
  1738. hreg : tregister;
  1739. hfreg : tregister;
  1740. size : longint;
  1741. fsize : longint;
  1742. r : integer;
  1743. begin
  1744. { The code generated by the section below, particularly the movem.l
  1745. instruction is known to cause an issue when compiled by some GNU
  1746. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1747. when you run into this problem, just call inherited here instead
  1748. to skip the movem.l generation. But better just use working GNU
  1749. AS version instead. (KB) }
  1750. dataregs:=[];
  1751. addrregs:=[];
  1752. fpuregs:=[];
  1753. { calculate temp. size }
  1754. size:=0;
  1755. fsize:=0;
  1756. hreg:=NR_NO;
  1757. hfreg:=NR_NO;
  1758. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1759. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1760. begin
  1761. hreg:=newreg(R_INTREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1762. inc(size,sizeof(aint));
  1763. dataregs:=dataregs + [saved_standard_registers[r]];
  1764. end;
  1765. if uses_registers(R_ADDRESSREGISTER) then
  1766. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1767. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1768. begin
  1769. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1770. inc(size,sizeof(aint));
  1771. addrregs:=addrregs + [saved_address_registers[r]];
  1772. end;
  1773. if uses_registers(R_FPUREGISTER) then
  1774. for r:=low(saved_fpu_registers) to high(saved_fpu_registers) do
  1775. if saved_fpu_registers[r] in rg[R_FPUREGISTER].used_in_proc then
  1776. begin
  1777. hfreg:=newreg(R_FPUREGISTER,saved_fpu_registers[r],R_SUBNONE);
  1778. inc(fsize,fpuregsize);
  1779. fpuregs:=fpuregs + [saved_fpu_registers[r]];
  1780. end;
  1781. { 68k has no MM registers }
  1782. if uses_registers(R_MMREGISTER) then
  1783. internalerror(2014030201);
  1784. if (size+fsize) > 0 then
  1785. begin
  1786. tg.GetTemp(list,size+fsize,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1787. include(current_procinfo.flags,pi_has_saved_regs);
  1788. { Copy registers to temp }
  1789. { NOTE: virtual registers allocated here won't be translated --> no higher-level stuff. }
  1790. href:=current_procinfo.save_regs_ref;
  1791. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire+[cpu_mc68000]) then
  1792. begin
  1793. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1794. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1795. reference_reset_base(href,NR_A0,0,sizeof(pint),[]);
  1796. end;
  1797. if size > 0 then
  1798. if size = sizeof(aint) then
  1799. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href))
  1800. else
  1801. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,[],href));
  1802. if fsize > 0 then
  1803. begin
  1804. { size is always longword aligned, while fsize is not }
  1805. inc(href.offset,size);
  1806. if fsize = fpuregsize then
  1807. list.concat(taicpu.op_reg_ref(A_FMOVE,fpuregopsize,hfreg,href))
  1808. else
  1809. list.concat(taicpu.op_regset_ref(A_FMOVEM,fpuregopsize,[],[],fpuregs,href));
  1810. end;
  1811. end;
  1812. end;
  1813. procedure tcg68k.g_restore_registers(list:TAsmList);
  1814. var
  1815. dataregs: tcpuregisterset;
  1816. addrregs: tcpuregisterset;
  1817. fpuregs : tcpuregisterset;
  1818. href : treference;
  1819. r : integer;
  1820. hreg : tregister;
  1821. hfreg : tregister;
  1822. size : longint;
  1823. fsize : longint;
  1824. begin
  1825. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1826. dataregs:=[];
  1827. addrregs:=[];
  1828. fpuregs:=[];
  1829. if not(pi_has_saved_regs in current_procinfo.flags) then
  1830. exit;
  1831. { Copy registers from temp }
  1832. size:=0;
  1833. fsize:=0;
  1834. hreg:=NR_NO;
  1835. hfreg:=NR_NO;
  1836. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1837. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1838. begin
  1839. inc(size,sizeof(aint));
  1840. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1841. { Allocate register so the optimizer does not remove the load }
  1842. a_reg_alloc(list,hreg);
  1843. dataregs:=dataregs + [saved_standard_registers[r]];
  1844. end;
  1845. if uses_registers(R_ADDRESSREGISTER) then
  1846. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1847. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1848. begin
  1849. inc(size,sizeof(aint));
  1850. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1851. { Allocate register so the optimizer does not remove the load }
  1852. a_reg_alloc(list,hreg);
  1853. addrregs:=addrregs + [saved_address_registers[r]];
  1854. end;
  1855. if uses_registers(R_FPUREGISTER) then
  1856. for r:=low(saved_fpu_registers) to high(saved_fpu_registers) do
  1857. if saved_fpu_registers[r] in rg[R_FPUREGISTER].used_in_proc then
  1858. begin
  1859. inc(fsize,fpuregsize);
  1860. hfreg:=newreg(R_FPUREGISTER,saved_fpu_registers[r],R_SUBNONE);
  1861. { Allocate register so the optimizer does not remove the load }
  1862. a_reg_alloc(list,hfreg);
  1863. fpuregs:=fpuregs + [saved_fpu_registers[r]];
  1864. end;
  1865. { 68k has no MM registers }
  1866. if uses_registers(R_MMREGISTER) then
  1867. internalerror(2014030202);
  1868. { Restore registers from temp }
  1869. href:=current_procinfo.save_regs_ref;
  1870. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire+[cpu_mc68000]) then
  1871. begin
  1872. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1873. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1874. reference_reset_base(href,NR_A0,0,sizeof(pint),[]);
  1875. end;
  1876. if size > 0 then
  1877. if size = sizeof(aint) then
  1878. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,hreg))
  1879. else
  1880. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs,[]));
  1881. if fsize > 0 then
  1882. begin
  1883. { size is always longword aligned, while fsize is not }
  1884. inc(href.offset,size);
  1885. if fsize = fpuregsize then
  1886. list.concat(taicpu.op_ref_reg(A_FMOVE,fpuregopsize,href,hfreg))
  1887. else
  1888. list.concat(taicpu.op_ref_regset(A_FMOVEM,fpuregopsize,href,[],[],fpuregs));
  1889. end;
  1890. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1891. end;
  1892. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  1893. begin
  1894. case _newsize of
  1895. OS_S16, OS_16:
  1896. case _oldsize of
  1897. OS_S8:
  1898. begin { 8 -> 16 bit sign extend }
  1899. if (isaddressregister(reg)) then
  1900. internalerror(2014031201);
  1901. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1902. end;
  1903. OS_8: { 8 -> 16 bit zero extend }
  1904. begin
  1905. if (current_settings.cputype in cpu_coldfire) then
  1906. { ColdFire has no ANDI.W }
  1907. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  1908. else
  1909. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  1910. end;
  1911. end;
  1912. OS_S32, OS_32:
  1913. case _oldsize of
  1914. OS_S8:
  1915. begin { 8 -> 32 bit sign extend }
  1916. if (isaddressregister(reg)) then
  1917. internalerror(2014031202);
  1918. if (current_settings.cputype = cpu_MC68000) then
  1919. begin
  1920. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1921. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1922. end
  1923. else
  1924. begin
  1925. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  1926. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1927. end;
  1928. end;
  1929. OS_8: { 8 -> 32 bit zero extend }
  1930. begin
  1931. if (isaddressregister(reg)) then
  1932. internalerror(2015031501);
  1933. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1934. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1935. end;
  1936. OS_S16: { 16 -> 32 bit sign extend }
  1937. begin
  1938. { address registers are sign-extended from 16->32 bit anyway
  1939. automagically on every W operation by the CPU, so this is a NOP }
  1940. if not isaddressregister(reg) then
  1941. begin
  1942. //list.concat(tai_comment.create(strpnew('sign extend word')));
  1943. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1944. end;
  1945. end;
  1946. OS_16:
  1947. begin
  1948. if (isaddressregister(reg)) then
  1949. internalerror(2015031502);
  1950. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1951. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1952. end;
  1953. end;
  1954. end; { otherwise the size is already correct }
  1955. end;
  1956. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1957. begin
  1958. sign_extend(list, _oldsize, OS_INT, reg);
  1959. end;
  1960. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1961. var
  1962. ai : taicpu;
  1963. begin
  1964. if cond=OC_None then
  1965. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1966. else
  1967. begin
  1968. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1969. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1970. end;
  1971. ai.is_jmp:=true;
  1972. list.concat(ai);
  1973. end;
  1974. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  1975. operations on an address register. if the register is a dataregister anyway, it
  1976. just returns it untouched.}
  1977. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  1978. var
  1979. scratch_reg: TRegister;
  1980. instr: Taicpu;
  1981. begin
  1982. if isaddressregister(reg) then
  1983. begin
  1984. scratch_reg:=getintregister(list,OS_INT);
  1985. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg);
  1986. add_move_instruction(instr);
  1987. list.concat(instr);
  1988. result:=scratch_reg;
  1989. end
  1990. else
  1991. result:=reg;
  1992. end;
  1993. { moves source register to destination register, if the two are not the same. can be used in pair
  1994. with force_to_dataregister() }
  1995. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  1996. var
  1997. instr: Taicpu;
  1998. begin
  1999. if (src <> dest) then
  2000. begin
  2001. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  2002. add_move_instruction(instr);
  2003. list.concat(instr);
  2004. end;
  2005. end;
  2006. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2007. var
  2008. hsym : tsym;
  2009. href : treference;
  2010. paraloc : Pcgparalocation;
  2011. begin
  2012. { calculate the parameter info for the procdef }
  2013. procdef.init_paraloc_info(callerside);
  2014. hsym:=tsym(procdef.parast.Find('self'));
  2015. if not(assigned(hsym) and
  2016. (hsym.typ=paravarsym)) then
  2017. internalerror(2013100702);
  2018. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2019. while paraloc<>nil do
  2020. with paraloc^ do
  2021. begin
  2022. case loc of
  2023. LOC_REGISTER:
  2024. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2025. LOC_REFERENCE:
  2026. begin
  2027. { offset in the wrapper needs to be adjusted for the stored
  2028. return address }
  2029. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint),[]);
  2030. { plain 68k could use SUBI on href directly, but this way it works on Coldfire too
  2031. and it's probably smaller code for the majority of cases (if ioffset small, the
  2032. load will use MOVEQ) (KB) }
  2033. a_load_const_reg(list,OS_ADDR,ioffset,NR_D0);
  2034. list.concat(taicpu.op_reg_ref(A_SUB,S_L,NR_D0,href));
  2035. end
  2036. else
  2037. internalerror(2013100703);
  2038. end;
  2039. paraloc:=next;
  2040. end;
  2041. end;
  2042. procedure tcg68k.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2043. begin
  2044. list.concat(taicpu.op_const_reg(A_SUB,S_L,localsize,NR_STACK_POINTER_REG));
  2045. end;
  2046. procedure tcg68k.check_register_size(size:tcgsize;reg:tregister);
  2047. begin
  2048. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  2049. internalerror(201512131);
  2050. end;
  2051. function tcg68k.optimize_const_mul_to_shift_sub_add(list: TAsmList; maxops: longint; a: tcgint; size: tcgsize; reg: TRegister): boolean;
  2052. var
  2053. i: longint;
  2054. nextpower: tcgint;
  2055. powerbit: longint;
  2056. submask: tcgint;
  2057. lastshift: longint;
  2058. hreg: tregister;
  2059. firstmov: boolean;
  2060. begin
  2061. nextpower:=nextpowerof2(a,powerbit);
  2062. submask:=nextpower-a;
  2063. result:=not ((popcnt(qword(a)) > maxops) and ((popcnt(qword(submask))+1) > maxops));
  2064. if not result then
  2065. exit;
  2066. list.concat(tai_comment.create(strpnew('optimize_const_mul_to_shift_sub_add, multiplier: '+tostr(a))));
  2067. lastshift:=0;
  2068. hreg:=getintregister(list,OS_INT);
  2069. if (popcnt(qword(a)) < (popcnt(qword(submask))+1)) then
  2070. begin
  2071. { doing additions }
  2072. firstmov:=(a and 1) = 0;
  2073. if not firstmov then
  2074. a_load_reg_reg(list,size,OS_INT,reg,hreg);
  2075. for i:=1 to bsrqword(a) do
  2076. if ((a shr i) and 1) = 1 then
  2077. begin
  2078. if firstmov then
  2079. begin
  2080. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,reg);
  2081. a_load_reg_reg(list,OS_INT,OS_INT,reg,hreg);
  2082. firstmov:=false;
  2083. end
  2084. else
  2085. begin
  2086. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,hreg);
  2087. a_op_reg_reg(list,OP_ADD,OS_INT,hreg,reg);
  2088. end;
  2089. lastshift:=i;
  2090. end;
  2091. end
  2092. else
  2093. begin
  2094. { doing subtractions }
  2095. a_load_const_reg(list,OS_INT,0,hreg);
  2096. for i:=0 to bsrqword(submask) do
  2097. if ((submask shr i) and 1) = 1 then
  2098. begin
  2099. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,reg);
  2100. a_op_reg_reg(list,OP_SUB,OS_INT,reg,hreg);
  2101. lastshift:=i;
  2102. end;
  2103. a_op_const_reg(list,OP_SHL,OS_INT,powerbit-lastshift,reg);
  2104. a_op_reg_reg(list,OP_ADD,OS_INT,hreg,reg);
  2105. end;
  2106. result:=true;
  2107. end;
  2108. {****************************************************************************}
  2109. { TCG64F68K }
  2110. {****************************************************************************}
  2111. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  2112. var
  2113. opcode : tasmop;
  2114. xopcode : tasmop;
  2115. instr : taicpu;
  2116. begin
  2117. opcode := topcg2tasmop[op];
  2118. xopcode := topcg2tasmopx[op];
  2119. case op of
  2120. OP_ADD,OP_SUB:
  2121. begin
  2122. { if one of these three registers is an address
  2123. register, we'll really get into problems! }
  2124. if isaddressregister(regdst.reglo) or
  2125. isaddressregister(regdst.reghi) or
  2126. isaddressregister(regsrc.reghi) then
  2127. internalerror(2014030101);
  2128. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  2129. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  2130. end;
  2131. OP_AND,OP_OR:
  2132. begin
  2133. { at least one of the registers must be a data register }
  2134. if (isaddressregister(regdst.reglo) and
  2135. isaddressregister(regsrc.reglo)) or
  2136. (isaddressregister(regsrc.reghi) and
  2137. isaddressregister(regdst.reghi)) then
  2138. internalerror(2014030102);
  2139. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2140. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2141. end;
  2142. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  2143. OP_IDIV,OP_DIV,
  2144. OP_IMUL,OP_MUL:
  2145. internalerror(2002081701);
  2146. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  2147. OP_SAR,OP_SHL,OP_SHR:
  2148. internalerror(2002081702);
  2149. OP_XOR:
  2150. begin
  2151. if isaddressregister(regdst.reglo) or
  2152. isaddressregister(regsrc.reglo) or
  2153. isaddressregister(regsrc.reghi) or
  2154. isaddressregister(regdst.reghi) then
  2155. internalerror(2014030103);
  2156. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2157. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2158. end;
  2159. OP_NEG,OP_NOT:
  2160. begin
  2161. if isaddressregister(regdst.reglo) or
  2162. isaddressregister(regdst.reghi) then
  2163. internalerror(2014030104);
  2164. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  2165. cg.add_move_instruction(instr);
  2166. list.concat(instr);
  2167. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  2168. cg.add_move_instruction(instr);
  2169. list.concat(instr);
  2170. if (op = OP_NOT) then
  2171. xopcode:=opcode;
  2172. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  2173. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  2174. end;
  2175. end; { end case }
  2176. end;
  2177. procedure tcg64f68k.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  2178. var
  2179. href : treference;
  2180. hreg: tregister;
  2181. begin
  2182. case op of
  2183. OP_NEG,OP_NOT:
  2184. begin
  2185. a_load64_ref_reg(list,ref,reg);
  2186. a_op64_reg_reg(list,op,size,reg,reg);
  2187. end;
  2188. OP_AND,OP_OR:
  2189. begin
  2190. href:=ref;
  2191. tcg68k(cg).fixref(list,href,false);
  2192. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reghi));
  2193. inc(href.offset,4);
  2194. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reglo));
  2195. end;
  2196. OP_ADD,OP_SUB:
  2197. begin
  2198. href:=ref;
  2199. tcg68k(cg).fixref(list,href,false);
  2200. hreg:=cg.getintregister(list,OS_32);
  2201. cg.a_load_ref_reg(list,OS_32,OS_32,href,hreg);
  2202. inc(href.offset,4);
  2203. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reglo));
  2204. list.concat(taicpu.op_reg_reg(topcg2tasmopx[op],S_L,hreg,reg.reghi));
  2205. end;
  2206. else
  2207. { XOR does not allow reference for source; ADD/SUB do not allow reference for
  2208. high dword, although low dword can still be handled directly. }
  2209. inherited a_op64_ref_reg(list,op,size,ref,reg);
  2210. end;
  2211. end;
  2212. procedure tcg64f68k.a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const ref : treference);
  2213. var
  2214. href: treference;
  2215. hreg: tregister;
  2216. begin
  2217. case op of
  2218. OP_AND,OP_OR,OP_XOR:
  2219. begin
  2220. href:=ref;
  2221. tcg68k(cg).fixref(list,href,false);
  2222. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reghi,href));
  2223. inc(href.offset,4);
  2224. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reglo,href));
  2225. end;
  2226. OP_ADD,OP_SUB:
  2227. begin
  2228. href:=ref;
  2229. tcg68k(cg).fixref(list,href,false);
  2230. hreg:=cg.getintregister(list,OS_32);
  2231. cg.a_load_ref_reg(list,OS_32,OS_32,href,hreg);
  2232. inc(href.offset,4);
  2233. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reglo,href));
  2234. list.concat(taicpu.op_reg_reg(topcg2tasmopx[op],S_L,reg.reghi,hreg));
  2235. dec(href.offset,4);
  2236. cg.a_load_reg_ref(list,OS_32,OS_32,hreg,href);
  2237. end;
  2238. else
  2239. inherited a_op64_reg_ref(list,op,size,reg,ref);
  2240. end;
  2241. end;
  2242. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  2243. var
  2244. lowvalue : cardinal;
  2245. highvalue : cardinal;
  2246. opcode : tasmop;
  2247. xopcode : tasmop;
  2248. hreg : tregister;
  2249. begin
  2250. { is it optimized out ? }
  2251. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  2252. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  2253. exit; }
  2254. lowvalue := cardinal(value);
  2255. highvalue := value shr 32;
  2256. opcode := topcg2tasmop[op];
  2257. xopcode := topcg2tasmopx[op];
  2258. { the destination registers must be data registers }
  2259. if isaddressregister(regdst.reglo) or
  2260. isaddressregister(regdst.reghi) then
  2261. internalerror(2014030105);
  2262. case op of
  2263. OP_ADD,OP_SUB:
  2264. begin
  2265. hreg:=cg.getintregister(list,OS_INT);
  2266. { cg.a_load_const_reg provides optimized loading to register for special cases }
  2267. cg.a_load_const_reg(list,OS_S32,longint(highvalue),hreg);
  2268. { don't use cg.a_op_const_reg() here, because a possible optimized
  2269. ADDQ/SUBQ wouldn't set the eXtend bit }
  2270. list.concat(taicpu.op_const_reg(opcode,S_L,lowvalue,regdst.reglo));
  2271. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  2272. end;
  2273. OP_AND,OP_OR,OP_XOR:
  2274. begin
  2275. cg.a_op_const_reg(list,op,OS_S32,longint(lowvalue),regdst.reglo);
  2276. cg.a_op_const_reg(list,op,OS_S32,longint(highvalue),regdst.reghi);
  2277. end;
  2278. { this is handled in 1st pass for 32-bit cpus (helper call) }
  2279. OP_IDIV,OP_DIV,
  2280. OP_IMUL,OP_MUL:
  2281. internalerror(2002081701);
  2282. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  2283. OP_SAR,OP_SHL,OP_SHR:
  2284. internalerror(2002081702);
  2285. { these should have been handled already by earlier passes }
  2286. OP_NOT,OP_NEG:
  2287. internalerror(2012110403);
  2288. end; { end case }
  2289. end;
  2290. procedure tcg64f68k.a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);
  2291. var
  2292. tmpref: treference;
  2293. begin
  2294. tmpref:=ref;
  2295. tcg68k(cg).fixref(list,tmpref,false);
  2296. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reghi,tmpref);
  2297. inc(tmpref.offset,4);
  2298. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reglo,tmpref);
  2299. end;
  2300. procedure tcg64f68k.a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);
  2301. var
  2302. tmpref: treference;
  2303. begin
  2304. { do not allow 64bit values to be loaded to address registers }
  2305. if isaddressregister(reg.reglo) or
  2306. isaddressregister(reg.reghi) then
  2307. internalerror(2016050501);
  2308. tmpref:=ref;
  2309. tcg68k(cg).fixref(list,tmpref,false);
  2310. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reghi);
  2311. inc(tmpref.offset,4);
  2312. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reglo);
  2313. end;
  2314. procedure create_codegen;
  2315. begin
  2316. cg := tcg68k.create;
  2317. cg64 :=tcg64f68k.create;
  2318. end;
  2319. end.