cgcpu.pas 103 KB

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