cgcpu.pas 96 KB

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