cgcpu.pas 97 KB

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