cgcpu.pas 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193
  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. {DEFINE DEBUG_CHARLIE}
  18. {$IFNDEF DEBUG_CHARLIE}
  19. {$WARNINGS OFF}
  20. {$ENDIF}
  21. unit cgcpu;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cgbase,cgobj,globtype,
  26. aasmbase,aasmtai,aasmdata,aasmcpu,
  27. cpubase,cpuinfo,
  28. parabase,cpupara,
  29. node,symconst,symtype,symdef,
  30. cgutils,cg64f32;
  31. type
  32. tcg68k = class(tcg)
  33. procedure init_register_allocators;override;
  34. procedure done_register_allocators;override;
  35. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  36. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  37. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  38. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  39. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  40. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  41. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);override;
  42. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  43. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  44. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  45. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  46. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);override;
  47. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  48. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  49. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  50. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  51. procedure a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);override;
  52. procedure a_loadmm_reg_reg(list: TAsmList;fromsize,tosize : tcgsize; reg1, reg2: tregister;shuffle : pmmshuffle); override;
  53. procedure a_loadmm_ref_reg(list: TAsmList;fromsize,tosize : tcgsize; const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  54. procedure a_loadmm_reg_ref(list: TAsmList;fromsize,tosize : tcgsize; reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  55. procedure a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const locpara : TCGPara;shuffle : pmmshuffle); override;
  56. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister); override;
  57. // procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  58. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); override;
  59. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  60. l : tasmlabel);override;
  61. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  62. procedure a_jmp_name(list : TAsmList;const s : string); override;
  63. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  64. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  65. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  66. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  67. { generates overflow checking code for a node }
  68. procedure g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef); override;
  69. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  70. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  71. // procedure g_restore_frame_pointer(list : TAsmList);override;
  72. // procedure g_return_from_proc(list : TAsmList;parasize : tcgint);override;
  73. procedure g_restore_registers(list:TAsmList);override;
  74. procedure g_save_registers(list:TAsmList);override;
  75. // procedure g_save_all_registers(list : TAsmList);override;
  76. // procedure g_restore_all_registers(list : TAsmList;const funcretparaloc:TCGPara);override;
  77. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  78. protected
  79. function fixref(list: TAsmList; var ref: treference): boolean;
  80. procedure call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  81. procedure call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  82. private
  83. { # Sign or zero extend the register to a full 32-bit value.
  84. The new value is left in the same register.
  85. }
  86. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  87. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  88. end;
  89. tcg64f68k = class(tcg64f32)
  90. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG; size: tcgsize; regsrc,regdst : tregister64);override;
  91. procedure a_op64_const_reg(list : TAsmList;op:TOpCG; size: tcgsize; value : int64;regdst : tregister64);override;
  92. end;
  93. { This function returns true if the reference+offset is valid.
  94. Otherwise extra code must be generated to solve the reference.
  95. On the m68k, this verifies that the reference is valid
  96. (e.g : if index register is used, then the max displacement
  97. is 256 bytes, if only base is used, then max displacement
  98. is 32K
  99. }
  100. function isvalidrefoffset(const ref: treference): boolean;
  101. procedure create_codegen;
  102. implementation
  103. uses
  104. globals,verbose,systems,cutils,
  105. symsym,symtable,defutil,paramgr,procinfo,
  106. rgobj,tgobj,rgcpu,fmodule;
  107. const
  108. { opcode table lookup }
  109. topcg2tasmop: Array[topcg] of tasmop =
  110. (
  111. A_NONE,
  112. A_MOVE,
  113. A_ADD,
  114. A_AND,
  115. A_DIVU,
  116. A_DIVS,
  117. A_MULS,
  118. A_MULU,
  119. A_NEG,
  120. A_NOT,
  121. A_OR,
  122. A_ASR,
  123. A_LSL,
  124. A_LSR,
  125. A_SUB,
  126. A_EOR,
  127. A_NONE,
  128. A_NONE
  129. );
  130. TOpCmp2AsmCond: Array[topcmp] of TAsmCond =
  131. (
  132. C_NONE,
  133. C_EQ,
  134. C_GT,
  135. C_LT,
  136. C_GE,
  137. C_LE,
  138. C_NE,
  139. C_LS,
  140. C_CS,
  141. C_CC,
  142. C_HI
  143. );
  144. function isvalidrefoffset(const ref: treference): boolean;
  145. begin
  146. isvalidrefoffset := true;
  147. if ref.index <> NR_NO then
  148. begin
  149. if ref.base <> NR_NO then
  150. internalerror(2002081401);
  151. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  152. isvalidrefoffset := false
  153. end
  154. else
  155. begin
  156. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  157. isvalidrefoffset := false;
  158. end;
  159. end;
  160. {****************************************************************************}
  161. { TCG68K }
  162. {****************************************************************************}
  163. function use_push(const cgpara:tcgpara):boolean;
  164. begin
  165. result:=(not paramanager.use_fixed_stack) and
  166. assigned(cgpara.location) and
  167. (cgpara.location^.loc=LOC_REFERENCE) and
  168. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  169. end;
  170. procedure tcg68k.init_register_allocators;
  171. begin
  172. inherited init_register_allocators;
  173. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  174. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7],
  175. first_int_imreg,[]);
  176. rg[R_ADDRESSREGISTER]:=trgcpu.create(R_ADDRESSREGISTER,R_SUBWHOLE,
  177. [RS_A0,RS_A1,RS_A2,RS_A3,RS_A4,RS_A5,RS_A6],
  178. first_addr_imreg,[]);
  179. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  180. [RS_FP0,RS_FP1,RS_FP2,RS_FP3,RS_FP4,RS_FP5,RS_FP6,RS_FP7],
  181. first_fpu_imreg,[]);
  182. end;
  183. procedure tcg68k.done_register_allocators;
  184. begin
  185. rg[R_INTREGISTER].free;
  186. rg[R_FPUREGISTER].free;
  187. rg[R_ADDRESSREGISTER].free;
  188. inherited done_register_allocators;
  189. end;
  190. procedure tcg68k.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  191. var
  192. pushsize : tcgsize;
  193. ref : treference;
  194. begin
  195. {$ifdef DEBUG_CHARLIE}
  196. // writeln('a_load_reg');_cgpara
  197. {$endif DEBUG_CHARLIE}
  198. { it's probably necessary to port this from x86 later, or provide an m68k solution (KB) }
  199. { TODO: FIX ME! check_register_size()}
  200. // check_register_size(size,r);
  201. if use_push(cgpara) then
  202. begin
  203. cgpara.check_simple_location;
  204. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  205. pushsize:=cgpara.location^.size
  206. else
  207. pushsize:=int_cgsize(cgpara.alignment);
  208. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  209. ref.direction := dir_dec;
  210. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize),ref));
  211. end
  212. else
  213. inherited a_load_reg_cgpara(list,size,r,cgpara);
  214. end;
  215. procedure tcg68k.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  216. var
  217. pushsize : tcgsize;
  218. ref : treference;
  219. begin
  220. {$ifdef DEBUG_CHARLIE}
  221. // writeln('a_load_const');_cgpara
  222. {$endif DEBUG_CHARLIE}
  223. if use_push(cgpara) then
  224. begin
  225. cgpara.check_simple_location;
  226. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  227. pushsize:=cgpara.location^.size
  228. else
  229. pushsize:=int_cgsize(cgpara.alignment);
  230. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  231. ref.direction := dir_dec;
  232. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[pushsize],a,ref));
  233. end
  234. else
  235. inherited a_load_const_cgpara(list,size,a,cgpara);
  236. end;
  237. procedure tcg68k.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  238. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  239. var
  240. pushsize : tcgsize;
  241. tmpreg : tregister;
  242. href : treference;
  243. ref : treference;
  244. begin
  245. if not assigned(paraloc) then
  246. exit;
  247. { TODO: FIX ME!!! this also triggers location bug }
  248. {if (paraloc^.loc<>LOC_REFERENCE) or
  249. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  250. (tcgsize2size[paraloc^.size]>sizeof(tcgint)) then
  251. internalerror(200501162);}
  252. { Pushes are needed in reverse order, add the size of the
  253. current location to the offset where to load from. This
  254. prevents wrong calculations for the last location when
  255. the size is not a power of 2 }
  256. if assigned(paraloc^.next) then
  257. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  258. { Push the data starting at ofs }
  259. href:=r;
  260. inc(href.offset,ofs);
  261. fixref(list,href);
  262. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  263. pushsize:=paraloc^.size
  264. else
  265. pushsize:=int_cgsize(cgpara.alignment);
  266. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, tcgsize2size[pushsize]);
  267. ref.direction := dir_dec;
  268. if tcgsize2size[paraloc^.size]<cgpara.alignment then
  269. begin
  270. tmpreg:=getintregister(list,pushsize);
  271. a_load_ref_reg(list,paraloc^.size,pushsize,href,tmpreg);
  272. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],tmpreg,ref));
  273. end
  274. else
  275. list.concat(taicpu.op_ref_ref(A_MOVE,tcgsize2opsize[pushsize],href,ref));
  276. end;
  277. var
  278. len : tcgint;
  279. href : treference;
  280. begin
  281. {$ifdef DEBUG_CHARLIE}
  282. // writeln('a_load_ref');_cgpara
  283. {$endif DEBUG_CHARLIE}
  284. { cgpara.size=OS_NO requires a copy on the stack }
  285. if use_push(cgpara) then
  286. begin
  287. { Record copy? }
  288. if (cgpara.size in [OS_NO,OS_F64]) or (size=OS_NO) then
  289. begin
  290. cgpara.check_simple_location;
  291. len:=align(cgpara.intsize,cgpara.alignment);
  292. g_stackpointer_alloc(list,len);
  293. reference_reset_base(href,NR_STACK_POINTER_REG,0,cgpara.alignment);
  294. g_concatcopy(list,r,href,len);
  295. end
  296. else
  297. begin
  298. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  299. internalerror(200501161);
  300. { We need to push the data in reverse order,
  301. therefor we use a recursive algorithm }
  302. pushdata(cgpara.location,0);
  303. end
  304. end
  305. else
  306. inherited a_load_ref_cgpara(list,size,r,cgpara);
  307. end;
  308. procedure tcg68k.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  309. var
  310. tmpreg : tregister;
  311. opsize : topsize;
  312. begin
  313. {$ifdef DEBUG_CHARLIE}
  314. // writeln('a_loadaddr_ref');_cgpara
  315. {$endif DEBUG_CHARLIE}
  316. with r do
  317. begin
  318. { i suppose this is not required for m68k (KB) }
  319. // if (segment<>NR_NO) then
  320. // cgmessage(cg_e_cant_use_far_pointer_there);
  321. if not use_push(cgpara) then
  322. begin
  323. cgpara.check_simple_location;
  324. opsize:=tcgsize2opsize[OS_ADDR];
  325. if (segment=NR_NO) and (base=NR_NO) and (index=NR_NO) then
  326. begin
  327. if assigned(symbol) then
  328. // list.concat(Taicpu.Op_sym_ofs(A_PUSH,opsize,symbol,offset))
  329. else;
  330. // list.concat(Taicpu.Op_const(A_PUSH,opsize,offset));
  331. end
  332. else if (segment=NR_NO) and (base=NR_NO) and (index<>NR_NO) and
  333. (offset=0) and (scalefactor=0) and (symbol=nil) then
  334. // list.concat(Taicpu.Op_reg(A_PUSH,opsize,index))
  335. else if (segment=NR_NO) and (base<>NR_NO) and (index=NR_NO) and
  336. (offset=0) and (symbol=nil) then
  337. // list.concat(Taicpu.Op_reg(A_PUSH,opsize,base))
  338. else
  339. begin
  340. tmpreg:=getaddressregister(list);
  341. a_loadaddr_ref_reg(list,r,tmpreg);
  342. // list.concat(taicpu.op_reg(A_PUSH,opsize,tmpreg));
  343. end;
  344. end
  345. else
  346. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  347. end;
  348. end;
  349. function tcg68k.fixref(list: TAsmList; var ref: treference): boolean;
  350. var
  351. hreg,idxreg : tregister;
  352. href : treference;
  353. instr : taicpu;
  354. begin
  355. result:=false;
  356. { The MC68020+ has extended
  357. addressing capabilities with a 32-bit
  358. displacement.
  359. }
  360. { first ensure that base is an address register }
  361. if (not assigned (ref.symbol) and (current_settings.cputype<>cpu_MC68000)) and
  362. (ref.base<>NR_NO) and not isaddressregister(ref.base) then
  363. begin
  364. hreg:=getaddressregister(list);
  365. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  366. add_move_instruction(instr);
  367. list.concat(instr);
  368. fixref:=true;
  369. ref.base:=hreg;
  370. end;
  371. if (current_settings.cputype=cpu_MC68020) then
  372. exit;
  373. { ToDo: check which constraints of Coldfire also apply to MC68000 }
  374. case current_settings.cputype of
  375. cpu_MC68000:
  376. begin
  377. if (ref.base<>NR_NO) then
  378. begin
  379. if (ref.index<>NR_NO) and assigned(ref.symbol) then
  380. begin
  381. hreg:=getaddressregister(list);
  382. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  383. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.index,hreg));
  384. ref.index:=NR_NO;
  385. ref.base:=hreg;
  386. end;
  387. { base + reg }
  388. if ref.index <> NR_NO then
  389. begin
  390. { base + reg + offset }
  391. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  392. begin
  393. hreg:=getaddressregister(list);
  394. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  395. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  396. fixref:=true;
  397. ref.offset:=0;
  398. ref.base:=hreg;
  399. exit;
  400. end;
  401. end
  402. else
  403. { base + offset }
  404. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  405. begin
  406. hreg:=getaddressregister(list);
  407. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  408. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  409. fixref:=true;
  410. ref.offset:=0;
  411. ref.base:=hreg;
  412. exit;
  413. end;
  414. if assigned(ref.symbol) then
  415. begin
  416. hreg:=getaddressregister(list);
  417. idxreg:=ref.base;
  418. ref.base:=NR_NO;
  419. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  420. reference_reset_base(ref,hreg,0,ref.alignment);
  421. fixref:=true;
  422. ref.index:=idxreg;
  423. end
  424. else if not isaddressregister(ref.base) then
  425. begin
  426. hreg:=getaddressregister(list);
  427. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  428. add_move_instruction(instr);
  429. list.concat(instr);
  430. fixref:=true;
  431. ref.base:=hreg;
  432. end;
  433. end
  434. else
  435. { Note: symbol -> ref would be supported as long as ref does not
  436. contain a offset or index... (maybe something for the
  437. optimizer) }
  438. if Assigned(ref.symbol) and (ref.index<>NR_NO) then
  439. begin
  440. hreg:=cg.getaddressregister(list);
  441. idxreg:=ref.index;
  442. ref.index:=NR_NO;
  443. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  444. reference_reset_base(ref,hreg,0,ref.alignment);
  445. ref.index:=idxreg;
  446. fixref:=true;
  447. end;
  448. end;
  449. cpu_isa_a,
  450. cpu_isa_a_p,
  451. cpu_isa_b,
  452. cpu_isa_c:
  453. begin
  454. if (ref.base<>NR_NO) then
  455. begin
  456. if assigned(ref.symbol) then
  457. begin
  458. hreg:=cg.getaddressregister(list);
  459. reference_reset_symbol(href,ref.symbol,ref.offset,ref.alignment);
  460. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  461. if ref.index<>NR_NO then
  462. begin
  463. idxreg:=getaddressregister(list);
  464. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,idxreg));
  465. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.index,idxreg));
  466. ref.index:=idxreg;
  467. end
  468. else
  469. ref.index:=ref.base;
  470. ref.base:=hreg;
  471. ref.offset:=0;
  472. ref.symbol:=nil;
  473. end;
  474. { once the above is verified to work the below code can be
  475. removed }
  476. {if assigned(ref.symbol) and (ref.index=NR_NO) then
  477. begin
  478. hreg:=cg.getaddressregister(list);
  479. reference_reset_symbol(href,ref.symbol,0,ref.alignment);
  480. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  481. ref.index:=ref.base;
  482. ref.base:=hreg;
  483. ref.symbol:=nil;
  484. end;
  485. if (ref.index<>NR_NO) and assigned(ref.symbol) then
  486. begin
  487. hreg:=getaddressregister(list);
  488. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  489. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.index,hreg));
  490. ref.base:=hreg;
  491. ref.index:=NR_NO;
  492. end;}
  493. {if (ref.index <> NR_NO) and assigned(ref.symbol) then
  494. internalerror(2002081403);}
  495. { base + reg }
  496. if ref.index <> NR_NO then
  497. begin
  498. { base + reg + offset }
  499. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  500. begin
  501. hreg:=getaddressregister(list);
  502. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  503. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  504. fixref:=true;
  505. ref.base:=hreg;
  506. ref.offset:=0;
  507. exit;
  508. end;
  509. end
  510. else
  511. { base + offset }
  512. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  513. begin
  514. hreg:=getaddressregister(list);
  515. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg));
  516. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  517. fixref:=true;
  518. ref.offset:=0;
  519. ref.base:=hreg;
  520. exit;
  521. end;
  522. end
  523. else
  524. { Note: symbol -> ref would be supported as long as ref does not
  525. contain a offset or index... (maybe something for the
  526. optimizer) }
  527. if Assigned(ref.symbol) {and (ref.index<>NR_NO)} then
  528. begin
  529. hreg:=cg.getaddressregister(list);
  530. idxreg:=ref.index;
  531. ref.index:=NR_NO;
  532. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  533. reference_reset_base(ref,hreg,0,ref.alignment);
  534. ref.index:=idxreg;
  535. fixref:=true;
  536. end;
  537. end;
  538. end;
  539. end;
  540. procedure tcg68k.call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  541. var
  542. paraloc1,paraloc2,paraloc3 : tcgpara;
  543. pd : tprocdef;
  544. begin
  545. pd:=search_system_proc(name);
  546. paraloc1.init;
  547. paraloc2.init;
  548. paraloc3.init;
  549. paramanager.getintparaloc(pd,1,paraloc1);
  550. paramanager.getintparaloc(pd,2,paraloc2);
  551. paramanager.getintparaloc(pd,3,paraloc3);
  552. a_load_const_cgpara(list,OS_8,0,paraloc3);
  553. a_load_const_cgpara(list,size,a,paraloc2);
  554. a_load_reg_cgpara(list,OS_32,reg,paraloc1);
  555. paramanager.freecgpara(list,paraloc3);
  556. paramanager.freecgpara(list,paraloc2);
  557. paramanager.freecgpara(list,paraloc1);
  558. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  559. a_call_name(list,name,false);
  560. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  561. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  562. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg);
  563. paraloc3.done;
  564. paraloc2.done;
  565. paraloc1.done;
  566. end;
  567. procedure tcg68k.call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  568. var
  569. paraloc1,paraloc2,paraloc3 : tcgpara;
  570. pd : tprocdef;
  571. begin
  572. pd:=search_system_proc(name);
  573. paraloc1.init;
  574. paraloc2.init;
  575. paraloc3.init;
  576. paramanager.getintparaloc(pd,1,paraloc1);
  577. paramanager.getintparaloc(pd,2,paraloc2);
  578. paramanager.getintparaloc(pd,3,paraloc3);
  579. a_load_const_cgpara(list,OS_8,0,paraloc3);
  580. a_load_reg_cgpara(list,OS_32,reg1,paraloc2);
  581. a_load_reg_cgpara(list,OS_32,reg2,paraloc1);
  582. paramanager.freecgpara(list,paraloc3);
  583. paramanager.freecgpara(list,paraloc2);
  584. paramanager.freecgpara(list,paraloc1);
  585. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  586. a_call_name(list,name,false);
  587. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  588. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  589. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg2);
  590. paraloc3.done;
  591. paraloc2.done;
  592. paraloc1.done;
  593. end;
  594. procedure tcg68k.a_call_name(list : TAsmList;const s : string; weak: boolean);
  595. var
  596. sym: tasmsymbol;
  597. begin
  598. if not(weak) then
  599. sym:=current_asmdata.RefAsmSymbol(s)
  600. else
  601. sym:=current_asmdata.WeakRefAsmSymbol(s);
  602. list.concat(taicpu.op_sym(A_JSR,S_NO,current_asmdata.RefAsmSymbol(s)));
  603. end;
  604. procedure tcg68k.a_call_reg(list : TAsmList;reg: tregister);
  605. var
  606. tmpref : treference;
  607. tmpreg : tregister;
  608. instr : taicpu;
  609. begin
  610. {$ifdef DEBUG_CHARLIE}
  611. list.concat(tai_comment.create(strpnew('a_call_reg')));
  612. {$endif}
  613. if isaddressregister(reg) then
  614. begin
  615. { if we have an address register, we can jump to the address directly }
  616. reference_reset_base(tmpref,reg,0,4);
  617. end
  618. else
  619. begin
  620. { if we have a data register, we need to move it to an address register first }
  621. tmpreg:=getaddressregister(list);
  622. reference_reset_base(tmpref,tmpreg,0,4);
  623. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,tmpreg);
  624. add_move_instruction(instr);
  625. list.concat(instr);
  626. end;
  627. list.concat(taicpu.op_ref(A_JSR,S_NO,tmpref));
  628. end;
  629. procedure tcg68k.a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);
  630. begin
  631. {$ifdef DEBUG_CHARLIE}
  632. // writeln('a_load_const_reg');
  633. {$endif DEBUG_CHARLIE}
  634. if isaddressregister(register) then
  635. begin
  636. list.concat(taicpu.op_const_reg(A_MOVE,S_L,longint(a),register))
  637. end
  638. else
  639. if a = 0 then
  640. list.concat(taicpu.op_reg(A_CLR,S_L,register))
  641. else
  642. begin
  643. if (longint(a) >= low(shortint)) and (longint(a) <= high(shortint)) then
  644. list.concat(taicpu.op_const_reg(A_MOVEQ,S_L,longint(a),register))
  645. else
  646. begin
  647. { clear the register first, for unsigned and positive values, so
  648. we don't need to zero extend after }
  649. if (size in [OS_16,OS_8]) or
  650. ((size in [OS_S16,OS_S8]) and (a > 0)) then
  651. list.concat(taicpu.op_reg(A_CLR,S_L,register));
  652. list.concat(taicpu.op_const_reg(A_MOVE,tcgsize2opsize[size],longint(a),register));
  653. { only sign extend if we need to, zero extension is not necessary because the CLR.L above }
  654. if (size in [OS_S16,OS_S8]) and (a < 0) then
  655. sign_extend(list,size,register);
  656. end;
  657. end;
  658. end;
  659. procedure tcg68k.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  660. var
  661. hreg : tregister;
  662. href : treference;
  663. begin
  664. {$ifdef DEBUG_CHARLIE}
  665. list.concat(tai_comment.create(strpnew('a_load_const_ref')));
  666. {$endif DEBUG_CHARLIE}
  667. href:=ref;
  668. fixref(list,href);
  669. { for coldfire we need to go through a temporary register if we have a
  670. offset, index or symbol given }
  671. if (current_settings.cputype in cpu_coldfire) and
  672. (
  673. (href.offset<>0) or
  674. { TODO : check whether we really need this second condition }
  675. (href.index<>NR_NO) or
  676. assigned(href.symbol)
  677. ) then
  678. begin
  679. hreg:=getintregister(list,tosize);
  680. list.concat(taicpu.op_const_reg(A_MOVE,tcgsize2opsize[tosize],longint(a),hreg));
  681. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  682. end
  683. else
  684. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[tosize],longint(a),href));
  685. end;
  686. procedure tcg68k.a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  687. var
  688. href : treference;
  689. size : tcgsize;
  690. begin
  691. href := ref;
  692. fixref(list,href);
  693. {$ifdef DEBUG_CHARLIE}
  694. list.concat(tai_comment.create(strpnew('a_load_reg_ref')));
  695. {$endif DEBUG_CHARLIE}
  696. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  697. size:=fromsize
  698. else
  699. size:=tosize;
  700. { move to destination reference }
  701. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[size],register,href));
  702. end;
  703. procedure tcg68k.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  704. var
  705. aref: treference;
  706. bref: treference;
  707. tmpref : treference;
  708. dofix : boolean;
  709. hreg: TRegister;
  710. begin
  711. aref := sref;
  712. bref := dref;
  713. fixref(list,aref);
  714. fixref(list,bref);
  715. {$ifdef DEBUG_CHARLIE}
  716. // writeln('a_load_ref_ref');
  717. {$endif DEBUG_CHARLIE}
  718. if fromsize<>tosize then
  719. begin
  720. { if we need to change the size then always use a temporary
  721. register }
  722. hreg:=getintregister(list,fromsize);
  723. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],aref,hreg));
  724. sign_extend(list,fromsize,hreg);
  725. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,bref));
  726. exit;
  727. end;
  728. { Coldfire dislikes certain move combinations }
  729. if current_settings.cputype in cpu_coldfire then
  730. begin
  731. { TODO : move.b/w only allowed in newer coldfires... (ISA_B+) }
  732. dofix:=false;
  733. if { (d16,Ax) and (d8,Ax,Xi) }
  734. (
  735. (aref.base<>NR_NO) and
  736. (
  737. (aref.index<>NR_NO) or
  738. (aref.offset<>0)
  739. )
  740. ) or
  741. { (xxx) }
  742. assigned(aref.symbol) then
  743. begin
  744. if aref.index<>NR_NO then
  745. begin
  746. dofix:={ (d16,Ax) and (d8,Ax,Xi) }
  747. (
  748. (bref.base<>NR_NO) and
  749. (
  750. (bref.index<>NR_NO) or
  751. (bref.offset<>0)
  752. )
  753. ) or
  754. { (xxx) }
  755. assigned(bref.symbol);
  756. end
  757. else
  758. { offset <> 0, but no index }
  759. begin
  760. dofix:={ (d8,Ax,Xi) }
  761. (
  762. (bref.base<>NR_NO) and
  763. (bref.index<>NR_NO)
  764. ) or
  765. { (xxx) }
  766. assigned(bref.symbol);
  767. end;
  768. end;
  769. if dofix then
  770. begin
  771. hreg:=getaddressregister(list);
  772. reference_reset_base(tmpref,hreg,0,0);
  773. list.concat(taicpu.op_ref_reg(A_LEA,S_L,aref,hreg));
  774. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],tmpref,bref));
  775. exit;
  776. end;
  777. end;
  778. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],aref,bref));
  779. end;
  780. procedure tcg68k.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  781. var
  782. instr : taicpu;
  783. begin
  784. { move to destination register }
  785. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg1,reg2);
  786. add_move_instruction(instr);
  787. list.concat(instr);
  788. { zero/sign extend register to 32-bit }
  789. sign_extend(list, fromsize, reg2);
  790. end;
  791. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  792. var
  793. href : treference;
  794. size : tcgsize;
  795. begin
  796. href:=ref;
  797. fixref(list,href);
  798. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  799. size:=fromsize
  800. else
  801. size:=tosize;
  802. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[size],href,register));
  803. { extend the value in the register }
  804. sign_extend(list, fromsize, register);
  805. end;
  806. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  807. var
  808. href : treference;
  809. // p: pointer;
  810. begin
  811. { TODO: FIX ME!!! take a look on this mess again...}
  812. // if getregtype(r)=R_ADDRESSREGISTER then
  813. // begin
  814. // writeln('address reg?!?');
  815. // p:=nil; dword(p^):=0; {DEBUG CODE... :D )
  816. // internalerror(2002072901);
  817. // end;
  818. href:=ref;
  819. fixref(list, href);
  820. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  821. end;
  822. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  823. var
  824. instr : taicpu;
  825. begin
  826. { in emulation mode, only 32-bit single is supported }
  827. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  828. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg1,reg2)
  829. else
  830. instr:=taicpu.op_reg_reg(A_FMOVE,tcgsize2opsize[tosize],reg1,reg2);
  831. add_move_instruction(instr);
  832. list.concat(instr);
  833. end;
  834. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  835. var
  836. opsize : topsize;
  837. href : treference;
  838. tmpreg : tregister;
  839. begin
  840. opsize := tcgsize2opsize[fromsize];
  841. { extended is not supported, since it is not available on Coldfire }
  842. if opsize = S_FX then
  843. internalerror(20020729);
  844. href := ref;
  845. fixref(list,href);
  846. { in emulation mode, only 32-bit single is supported }
  847. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  848. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,reg))
  849. else
  850. begin
  851. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  852. if (tosize < fromsize) then
  853. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  854. end;
  855. end;
  856. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  857. var
  858. opsize : topsize;
  859. begin
  860. opsize := tcgsize2opsize[tosize];
  861. { extended is not supported, since it is not available on Coldfire }
  862. if opsize = S_FX then
  863. internalerror(20020729);
  864. { in emulation mode, only 32-bit single is supported }
  865. if (cs_fp_emulation in current_settings.moduleswitches) or (current_settings.fputype=fpu_soft) then
  866. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,reg, ref))
  867. else
  868. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg, ref));
  869. end;
  870. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  871. begin
  872. case cgpara.location^.loc of
  873. LOC_REFERENCE,LOC_CREFERENCE:
  874. begin
  875. case size of
  876. OS_F64:
  877. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  878. OS_F32:
  879. a_load_ref_cgpara(list,size,ref,cgpara);
  880. else
  881. internalerror(2013021201);
  882. end;
  883. end;
  884. else
  885. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  886. end;
  887. end;
  888. procedure tcg68k.a_loadmm_reg_reg(list: TAsmList;fromsize,tosize : tcgsize; reg1, reg2: tregister;shuffle : pmmshuffle);
  889. begin
  890. internalerror(20020729);
  891. end;
  892. procedure tcg68k.a_loadmm_ref_reg(list: TAsmList;fromsize,tosize : tcgsize; const ref: treference; reg: tregister;shuffle : pmmshuffle);
  893. begin
  894. internalerror(20020729);
  895. end;
  896. procedure tcg68k.a_loadmm_reg_ref(list: TAsmList;fromsize,tosize : tcgsize; reg: tregister; const ref: treference;shuffle : pmmshuffle);
  897. begin
  898. internalerror(20020729);
  899. end;
  900. procedure tcg68k.a_loadmm_reg_cgpara(list: TAsmList; size: tcgsize; reg: tregister;const locpara : TCGPara;shuffle : pmmshuffle);
  901. begin
  902. internalerror(20020729);
  903. end;
  904. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  905. var
  906. scratch_reg : tregister;
  907. scratch_reg2: tregister;
  908. opcode : tasmop;
  909. r,r2 : Tregister;
  910. instr : taicpu;
  911. paraloc1,paraloc2,paraloc3 : tcgpara;
  912. begin
  913. optimize_op_const(op, a);
  914. opcode := topcg2tasmop[op];
  915. case op of
  916. OP_NONE :
  917. begin
  918. { Opcode is optimized away }
  919. end;
  920. OP_MOVE :
  921. begin
  922. { Optimized, replaced with a simple load }
  923. a_load_const_reg(list,size,a,reg);
  924. end;
  925. OP_ADD :
  926. begin
  927. if (a >= 1) and (a <= 8) then
  928. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,a, reg))
  929. else
  930. begin
  931. { all others, including coldfire }
  932. list.concat(taicpu.op_const_reg(A_ADD,S_L,a, reg));
  933. end;
  934. end;
  935. OP_AND,
  936. OP_OR:
  937. begin
  938. if isaddressregister(reg) then
  939. begin
  940. { use scratch register (there is a anda/ora though...) }
  941. scratch_reg:=getintregister(list,OS_INT);
  942. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg);
  943. add_move_instruction(instr);
  944. list.concat(instr);
  945. list.concat(taicpu.op_const_reg(opcode,S_L,longint(a),scratch_reg));
  946. instr:=taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg);
  947. add_move_instruction(instr);
  948. list.concat(instr);
  949. end
  950. else
  951. list.concat(taicpu.op_const_reg(topcg2tasmop[op],S_L,longint(a), reg));
  952. end;
  953. OP_DIV :
  954. begin
  955. internalerror(20020816);
  956. end;
  957. OP_IDIV :
  958. begin
  959. internalerror(20020816);
  960. end;
  961. OP_IMUL :
  962. begin
  963. if current_settings.cputype<>cpu_MC68020 then
  964. call_rtl_mul_const_reg(list,size,a,reg,'fpc_mul_longint')
  965. else
  966. begin
  967. if (isaddressregister(reg)) then
  968. begin
  969. scratch_reg := getintregister(list,OS_INT);
  970. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg);
  971. add_move_instruction(instr);
  972. list.concat(instr);
  973. list.concat(taicpu.op_const_reg(A_MULS,S_L,a,scratch_reg));
  974. instr:=taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg);
  975. add_move_instruction(instr);
  976. list.concat(instr);
  977. end
  978. else
  979. list.concat(taicpu.op_const_reg(A_MULS,S_L,a,reg));
  980. end;
  981. end;
  982. OP_MUL :
  983. begin
  984. if current_settings.cputype<>cpu_MC68020 then
  985. call_rtl_mul_const_reg(list,size,a,reg,'fpc_mul_dword')
  986. else
  987. begin
  988. if (isaddressregister(reg)) then
  989. begin
  990. scratch_reg := getintregister(list,OS_INT);
  991. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg);
  992. add_move_instruction(instr);
  993. list.concat(instr);
  994. list.concat(taicpu.op_const_reg(A_MULU,S_L,a,scratch_reg));
  995. instr:=taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg);
  996. add_move_instruction(instr);
  997. list.concat(instr);
  998. end
  999. else
  1000. list.concat(taicpu.op_const_reg(A_MULU,S_L,a,reg));
  1001. end;
  1002. end;
  1003. OP_SAR,
  1004. OP_SHL,
  1005. OP_SHR :
  1006. begin
  1007. if (a >= 1) and (a <= 8) then
  1008. begin
  1009. { not allowed to shift an address register }
  1010. if (isaddressregister(reg)) then
  1011. begin
  1012. scratch_reg := getintregister(list,OS_INT);
  1013. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg);
  1014. add_move_instruction(instr);
  1015. list.concat(instr);
  1016. list.concat(taicpu.op_const_reg(opcode,S_L,a, scratch_reg));
  1017. instr:=taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg,reg);
  1018. add_move_instruction(instr);
  1019. list.concat(instr);
  1020. end
  1021. else
  1022. list.concat(taicpu.op_const_reg(opcode,S_L,a, reg));
  1023. end
  1024. else
  1025. begin
  1026. { we must load the data into a register ... :() }
  1027. scratch_reg := cg.getintregister(list,OS_INT);
  1028. list.concat(taicpu.op_const_reg(A_MOVE,S_L,a, scratch_reg));
  1029. { again... since shifting with address register is not allowed }
  1030. if (isaddressregister(reg)) then
  1031. begin
  1032. scratch_reg2 := cg.getintregister(list,OS_INT);
  1033. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg, scratch_reg2);
  1034. add_move_instruction(instr);
  1035. list.concat(instr);
  1036. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg, scratch_reg2));
  1037. instr:=taicpu.op_reg_reg(A_MOVE,S_L,scratch_reg2,reg);
  1038. add_move_instruction(instr);
  1039. list.concat(instr);
  1040. end
  1041. else
  1042. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg, reg));
  1043. end;
  1044. end;
  1045. OP_SUB :
  1046. begin
  1047. if (a >= 1) and (a <= 8) then
  1048. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,a,reg))
  1049. else
  1050. begin
  1051. { all others, including coldfire }
  1052. list.concat(taicpu.op_const_reg(A_SUB,S_L,a, reg));
  1053. end;
  1054. end;
  1055. OP_XOR :
  1056. begin
  1057. list.concat(taicpu.op_const_reg(A_EORI,S_L,a, reg));
  1058. end;
  1059. else
  1060. internalerror(20020729);
  1061. end;
  1062. end;
  1063. {
  1064. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1065. var
  1066. opcode: tasmop;
  1067. begin
  1068. writeln('a_op_const_ref');
  1069. optimize_op_const(op, a);
  1070. opcode := topcg2tasmop[op];
  1071. case op of
  1072. OP_NONE :
  1073. begin
  1074. { opcode was optimized away }
  1075. end;
  1076. OP_MOVE :
  1077. begin
  1078. { Optimized, replaced with a simple load }
  1079. a_load_const_ref(list,size,a,ref);
  1080. end;
  1081. else
  1082. begin
  1083. internalerror(2007010101);
  1084. end;
  1085. end;
  1086. end;
  1087. }
  1088. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister);
  1089. var
  1090. hreg1,hreg2,r,r2: tregister;
  1091. instr : taicpu;
  1092. paraloc1,paraloc2,paraloc3 : tcgpara;
  1093. begin
  1094. case op of
  1095. OP_ADD :
  1096. begin
  1097. if current_settings.cputype in cpu_ColdFire then
  1098. begin
  1099. { operation only allowed only a longword }
  1100. sign_extend(list, size, reg1);
  1101. sign_extend(list, size, reg2);
  1102. list.concat(taicpu.op_reg_reg(A_ADD,S_L,reg1, reg2));
  1103. end
  1104. else
  1105. begin
  1106. list.concat(taicpu.op_reg_reg(A_ADD,TCGSize2OpSize[size],reg1, reg2));
  1107. end;
  1108. end;
  1109. OP_AND,OP_OR,
  1110. OP_SAR,OP_SHL,
  1111. OP_SHR,OP_SUB,OP_XOR :
  1112. begin
  1113. { load to data registers }
  1114. if (isaddressregister(reg1)) then
  1115. begin
  1116. hreg1 := getintregister(list,OS_INT);
  1117. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1);
  1118. add_move_instruction(instr);
  1119. list.concat(instr);
  1120. end
  1121. else
  1122. hreg1 := reg1;
  1123. if (isaddressregister(reg2)) then
  1124. begin
  1125. hreg2:= getintregister(list,OS_INT);
  1126. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2);
  1127. add_move_instruction(instr);
  1128. list.concat(instr);
  1129. end
  1130. else
  1131. hreg2 := reg2;
  1132. if current_settings.cputype in cpu_ColdFire then
  1133. begin
  1134. { operation only allowed only a longword }
  1135. {!***************************************
  1136. in the case of shifts, the value to
  1137. shift by, should already be valid, so
  1138. no need to sign extend the value
  1139. !
  1140. }
  1141. if op in [OP_AND,OP_OR,OP_SUB,OP_XOR] then
  1142. sign_extend(list, size, hreg1);
  1143. sign_extend(list, size, hreg2);
  1144. instr:=taicpu.op_reg_reg(topcg2tasmop[op],S_L,hreg1, hreg2);
  1145. add_move_instruction(instr);
  1146. list.concat(instr);
  1147. end
  1148. else
  1149. begin
  1150. list.concat(taicpu.op_reg_reg(topcg2tasmop[op],TCGSize2OpSize[size],hreg1, hreg2));
  1151. end;
  1152. { move back result into destination register }
  1153. if reg2 <> hreg2 then
  1154. begin
  1155. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2);
  1156. add_move_instruction(instr);
  1157. list.concat(instr);
  1158. end;
  1159. end;
  1160. OP_DIV :
  1161. begin
  1162. internalerror(20020816);
  1163. end;
  1164. OP_IDIV :
  1165. begin
  1166. internalerror(20020816);
  1167. end;
  1168. OP_IMUL :
  1169. begin
  1170. sign_extend(list, size,reg1);
  1171. sign_extend(list, size,reg2);
  1172. if current_settings.cputype<>cpu_MC68020 then
  1173. call_rtl_mul_reg_reg(list,reg1,reg2,'fpc_mul_longint')
  1174. else
  1175. begin
  1176. // writeln('doing 68020');
  1177. if (isaddressregister(reg1)) then
  1178. hreg1 := getintregister(list,OS_INT)
  1179. else
  1180. hreg1 := reg1;
  1181. if (isaddressregister(reg2)) then
  1182. hreg2:= getintregister(list,OS_INT)
  1183. else
  1184. hreg2 := reg2;
  1185. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1);
  1186. add_move_instruction(instr);
  1187. list.concat(instr);
  1188. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2);
  1189. add_move_instruction(instr);
  1190. list.concat(instr);
  1191. list.concat(taicpu.op_reg_reg(A_MULS,S_L,reg1,reg2));
  1192. { move back result into destination register }
  1193. if reg2 <> hreg2 then
  1194. begin
  1195. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2);
  1196. add_move_instruction(instr);
  1197. list.concat(instr);
  1198. end;
  1199. end;
  1200. end;
  1201. OP_MUL :
  1202. begin
  1203. sign_extend(list, size,reg1);
  1204. sign_extend(list, size,reg2);
  1205. if current_settings.cputype <> cpu_MC68020 then
  1206. call_rtl_mul_reg_reg(list,reg1,reg2,'fpc_mul_dword')
  1207. else
  1208. begin
  1209. if (isaddressregister(reg1)) then
  1210. begin
  1211. hreg1 := cg.getintregister(list,OS_INT);
  1212. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg1,hreg1);
  1213. add_move_instruction(instr);
  1214. list.concat(instr);
  1215. end
  1216. else
  1217. hreg1 := reg1;
  1218. if (isaddressregister(reg2)) then
  1219. begin
  1220. hreg2:= cg.getintregister(list,OS_INT);
  1221. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2);
  1222. add_move_instruction(instr);
  1223. list.concat(instr);
  1224. end
  1225. else
  1226. hreg2 := reg2;
  1227. list.concat(taicpu.op_reg_reg(A_MULU,S_L,reg1,reg2));
  1228. { move back result into destination register }
  1229. if reg2<>hreg2 then
  1230. begin
  1231. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2);
  1232. add_move_instruction(instr);
  1233. list.concat(instr);
  1234. end;
  1235. end;
  1236. end;
  1237. OP_NEG,
  1238. OP_NOT :
  1239. Begin
  1240. { if there are two operands, move the register,
  1241. since the operation will only be done on the result
  1242. register.
  1243. }
  1244. if reg1 <> NR_NO then
  1245. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,reg1,reg2);
  1246. if (isaddressregister(reg2)) then
  1247. begin
  1248. hreg2 := getintregister(list,OS_INT);
  1249. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg2,hreg2);
  1250. add_move_instruction(instr);
  1251. list.concat(instr);
  1252. end
  1253. else
  1254. hreg2 := reg2;
  1255. { coldfire only supports long version }
  1256. if current_settings.cputype in cpu_ColdFire then
  1257. begin
  1258. sign_extend(list, size,hreg2);
  1259. list.concat(taicpu.op_reg(topcg2tasmop[op],S_L,hreg2));
  1260. end
  1261. else
  1262. begin
  1263. list.concat(taicpu.op_reg(topcg2tasmop[op],TCGSize2OpSize[size],hreg2));
  1264. end;
  1265. if reg2 <> hreg2 then
  1266. begin
  1267. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg2,reg2);
  1268. add_move_instruction(instr);
  1269. list.concat(instr);
  1270. end;
  1271. end;
  1272. else
  1273. internalerror(20020729);
  1274. end;
  1275. end;
  1276. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1277. l : tasmlabel);
  1278. var
  1279. hregister : tregister;
  1280. instr : taicpu;
  1281. begin
  1282. if a = 0 then
  1283. begin
  1284. if (current_settings.cputype = cpu_MC68000) and isaddressregister(reg) then
  1285. begin
  1286. {
  1287. 68000 does not seem to like address register for TST instruction
  1288. }
  1289. { always move to a data register }
  1290. hregister := getintregister(list,OS_INT);
  1291. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,hregister);
  1292. add_move_instruction(instr);
  1293. list.concat(instr);
  1294. { sign/zero extend the register }
  1295. sign_extend(list, size,hregister);
  1296. reg:=hregister;
  1297. end;
  1298. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg));
  1299. end
  1300. else
  1301. begin
  1302. if (current_settings.cputype in cpu_ColdFire) then
  1303. begin
  1304. {
  1305. only longword comparison is supported,
  1306. and only on data registers.
  1307. }
  1308. hregister := getintregister(list,OS_INT);
  1309. { always move to a data register }
  1310. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,hregister);
  1311. add_move_instruction(instr);
  1312. list.concat(instr);
  1313. { sign/zero extend the register }
  1314. sign_extend(list, size,hregister);
  1315. list.concat(taicpu.op_const_reg(A_CMPI,S_L,a,hregister));
  1316. end
  1317. else
  1318. begin
  1319. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1320. end;
  1321. end;
  1322. { emit the actual jump to the label }
  1323. a_jmp_cond(list,cmp_op,l);
  1324. end;
  1325. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1326. begin
  1327. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1328. { emit the actual jump to the label }
  1329. a_jmp_cond(list,cmp_op,l);
  1330. end;
  1331. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1332. var
  1333. ai: taicpu;
  1334. begin
  1335. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s));
  1336. ai.is_jmp := true;
  1337. list.concat(ai);
  1338. end;
  1339. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1340. var
  1341. ai: taicpu;
  1342. begin
  1343. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1344. ai.is_jmp := true;
  1345. list.concat(ai);
  1346. end;
  1347. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1348. var
  1349. ai : taicpu;
  1350. begin
  1351. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  1352. ai.SetCondition(flags_to_cond(f));
  1353. ai.is_jmp := true;
  1354. list.concat(ai);
  1355. end;
  1356. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1357. var
  1358. ai : taicpu;
  1359. hreg : tregister;
  1360. instr : taicpu;
  1361. begin
  1362. { move to a Dx register? }
  1363. if (isaddressregister(reg)) then
  1364. hreg:=getintregister(list,OS_INT)
  1365. else
  1366. hreg:=reg;
  1367. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  1368. ai.SetCondition(flags_to_cond(f));
  1369. list.concat(ai);
  1370. { Scc stores a complete byte of 1s, but the compiler expects only one
  1371. bit set, so ensure this is the case }
  1372. list.concat(taicpu.op_const_reg(A_AND,S_L,1,hreg));
  1373. if hreg<>reg then
  1374. begin
  1375. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg);
  1376. add_move_instruction(instr);
  1377. list.concat(instr);
  1378. end;
  1379. end;
  1380. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1381. var
  1382. helpsize : longint;
  1383. i : byte;
  1384. reg8,reg32 : tregister;
  1385. swap : boolean;
  1386. hregister : tregister;
  1387. iregister : tregister;
  1388. jregister : tregister;
  1389. hp1 : treference;
  1390. hp2 : treference;
  1391. hl : tasmlabel;
  1392. hl2: tasmlabel;
  1393. popaddress : boolean;
  1394. srcref,dstref : treference;
  1395. alignsize : tcgsize;
  1396. orglen : tcgint;
  1397. begin
  1398. popaddress := false;
  1399. // writeln('concatcopy:',len);
  1400. { this should never occur }
  1401. if len > 65535 then
  1402. internalerror(0);
  1403. hregister := getintregister(list,OS_INT);
  1404. // if delsource then
  1405. // reference_release(list,source);
  1406. orglen:=len;
  1407. { from 12 bytes movs is being used }
  1408. if {(not loadref) and} ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=12))) then
  1409. begin
  1410. srcref := source;
  1411. dstref := dest;
  1412. helpsize:=len div 4;
  1413. { move a dword x times }
  1414. for i:=1 to helpsize do
  1415. begin
  1416. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  1417. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  1418. inc(srcref.offset,4);
  1419. inc(dstref.offset,4);
  1420. dec(len,4);
  1421. end;
  1422. { move a word }
  1423. if len>1 then
  1424. begin
  1425. if (orglen<sizeof(aint)) and
  1426. (source.base=NR_FRAME_POINTER_REG) and
  1427. (source.offset>0) then
  1428. { copy of param to local location }
  1429. alignsize:=OS_INT
  1430. else
  1431. alignsize:=OS_16;
  1432. a_load_ref_reg(list,alignsize,alignsize,srcref,hregister);
  1433. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  1434. inc(srcref.offset,2);
  1435. inc(dstref.offset,2);
  1436. dec(len,2);
  1437. end;
  1438. { move a single byte }
  1439. if len>0 then
  1440. begin
  1441. if (orglen<sizeof(aint)) and
  1442. (source.base=NR_FRAME_POINTER_REG) and
  1443. (source.offset>0) then
  1444. { copy of param to local location }
  1445. alignsize:=OS_INT
  1446. else
  1447. alignsize:=OS_8;
  1448. a_load_ref_reg(list,alignsize,alignsize,srcref,hregister);
  1449. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  1450. end
  1451. end
  1452. else
  1453. begin
  1454. iregister:=getaddressregister(list);
  1455. jregister:=getaddressregister(list);
  1456. { reference for move (An)+,(An)+ }
  1457. reference_reset(hp1,source.alignment);
  1458. hp1.base := iregister; { source register }
  1459. hp1.direction := dir_inc;
  1460. reference_reset(hp2,dest.alignment);
  1461. hp2.base := jregister;
  1462. hp2.direction := dir_inc;
  1463. { iregister = source }
  1464. { jregister = destination }
  1465. { if loadref then
  1466. cg.a_load_ref_reg(list,OS_INT,OS_INT,source,iregister)
  1467. else}
  1468. a_loadaddr_ref_reg(list,source,iregister);
  1469. a_loadaddr_ref_reg(list,dest,jregister);
  1470. { double word move only on 68020+ machines }
  1471. { because of possible alignment problems }
  1472. { use fast loop mode }
  1473. if (current_settings.cputype=cpu_MC68020) then
  1474. begin
  1475. helpsize := len - len mod 4;
  1476. len := len mod 4;
  1477. list.concat(taicpu.op_const_reg(A_MOVE,S_L,helpsize div 4,hregister));
  1478. current_asmdata.getjumplabel(hl2);
  1479. a_jmp_always(list,hl2);
  1480. current_asmdata.getjumplabel(hl);
  1481. a_label(list,hl);
  1482. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  1483. a_label(list,hl2);
  1484. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1485. if len > 1 then
  1486. begin
  1487. dec(len,2);
  1488. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  1489. end;
  1490. if len = 1 then
  1491. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1492. end
  1493. else
  1494. begin
  1495. { Fast 68010 loop mode with no possible alignment problems }
  1496. helpsize := len;
  1497. list.concat(taicpu.op_const_reg(A_MOVE,S_L,helpsize,hregister));
  1498. current_asmdata.getjumplabel(hl2);
  1499. a_jmp_always(list,hl2);
  1500. current_asmdata.getjumplabel(hl);
  1501. a_label(list,hl);
  1502. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1503. a_label(list,hl2);
  1504. if current_settings.cputype in cpu_coldfire then
  1505. begin
  1506. { Coldfire does not support DBRA }
  1507. list.concat(taicpu.op_const_reg(A_SUB,S_L,1,hregister));
  1508. list.concat(taicpu.op_sym(A_BPL,S_L,hl));
  1509. end
  1510. else
  1511. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1512. end;
  1513. { restore the registers that we have just used olny if they are used! }
  1514. if jregister = NR_A1 then
  1515. hp2.base := NR_NO;
  1516. if iregister = NR_A0 then
  1517. hp1.base := NR_NO;
  1518. // reference_release(list,hp1);
  1519. // reference_release(list,hp2);
  1520. end;
  1521. // if delsource then
  1522. // tg.ungetiftemp(list,source);
  1523. end;
  1524. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1525. begin
  1526. end;
  1527. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1528. var
  1529. r,rsp: TRegister;
  1530. ref : TReference;
  1531. begin
  1532. {$ifdef DEBUG_CHARLIE}
  1533. // writeln('proc entry, localsize:',localsize);
  1534. {$endif DEBUG_CHARLIE}
  1535. if not nostackframe then
  1536. begin
  1537. if localsize<>0 then
  1538. begin
  1539. { size can't be negative }
  1540. if (localsize < 0) then
  1541. internalerror(2006122601);
  1542. { Not to complicate the code generator too much, and since some }
  1543. { of the systems only support this format, the localsize cannot }
  1544. { exceed 32K in size. }
  1545. if (localsize > high(smallint)) then
  1546. CGMessage(cg_e_localsize_too_big);
  1547. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1548. end
  1549. else
  1550. begin
  1551. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1552. (*
  1553. { FIXME! - Carl's original code uses this method. However,
  1554. according to the 68060 users manual, a LINK is faster than
  1555. two moves. So, use a link in #0 case too, for now. I'm not
  1556. really sure tho', that LINK supports #0 disposition, but i
  1557. see no reason why it shouldn't support it. (KB) }
  1558. { when localsize = 0, use two moves, instead of link }
  1559. r:=NR_FRAME_POINTER_REG;
  1560. rsp:=NR_STACK_POINTER_REG;
  1561. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1562. ref.direction:=dir_dec;
  1563. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,r,ref));
  1564. instr:=taicpu.op_reg_reg(A_MOVE,S_L,rsp,r);
  1565. add_move_instruction(instr); mwould also be needed
  1566. list.concat(instr);
  1567. *)
  1568. end;
  1569. end;
  1570. end;
  1571. { procedure tcg68k.g_restore_frame_pointer(list : TAsmList);
  1572. var
  1573. r:Tregister;
  1574. begin
  1575. r:=NR_FRAME_POINTER_REG;
  1576. list.concat(taicpu.op_reg(A_UNLK,S_NO,r));
  1577. end;
  1578. }
  1579. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1580. var
  1581. r,hregister : TRegister;
  1582. localsize: tcgint;
  1583. spr : TRegister;
  1584. fpr : TRegister;
  1585. ref : TReference;
  1586. begin
  1587. if not nostackframe then
  1588. begin
  1589. localsize := current_procinfo.calc_stackframe_size;
  1590. {$ifdef DEBUG_CHARLIE}
  1591. // writeln('proc exit with stackframe, size:',localsize,' parasize:',parasize);
  1592. {$endif DEBUG_CHARLIE}
  1593. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1594. parasize := parasize - target_info.first_parm_offset; { i'm still not 100% confident that this is
  1595. correct here, but at least it looks less
  1596. hacky, and makes some sense (KB) }
  1597. if (parasize<>0) then
  1598. begin
  1599. { only 68020+ supports RTD, so this needs another code path
  1600. for 68000 and Coldfire (KB) }
  1601. { TODO: 68020+ only code generation, without fallback}
  1602. if current_settings.cputype=cpu_mc68020 then
  1603. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1604. else
  1605. begin
  1606. { We must pull the PC Counter from the stack, before }
  1607. { restoring the stack pointer, otherwise the PC would }
  1608. { point to nowhere! }
  1609. { save the PC counter (pop it from the stack) }
  1610. //hregister:=cg.getaddressregister(list);
  1611. hregister:=NR_A3;
  1612. cg.a_reg_alloc(list,hregister);
  1613. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1614. ref.direction:=dir_inc;
  1615. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1616. { can we do a quick addition ... }
  1617. r:=NR_SP;
  1618. if (parasize > 0) and (parasize < 9) then
  1619. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1620. else { nope ... }
  1621. list.concat(taicpu.op_const_reg(A_ADD,S_L,parasize,r));
  1622. { restore the PC counter (push it on the stack) }
  1623. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1624. ref.direction:=dir_dec;
  1625. cg.a_reg_alloc(list,hregister);
  1626. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hregister,ref));
  1627. list.concat(taicpu.op_none(A_RTS,S_NO));
  1628. end;
  1629. end
  1630. else
  1631. list.concat(taicpu.op_none(A_RTS,S_NO));
  1632. end
  1633. else
  1634. begin
  1635. {$ifdef DEBUG_CHARLIE}
  1636. // writeln('proc exit, no stackframe');
  1637. {$endif DEBUG_CHARLIE}
  1638. list.concat(taicpu.op_none(A_RTS,S_NO));
  1639. end;
  1640. // writeln('g_proc_exit');
  1641. { Routines with the poclearstack flag set use only a ret.
  1642. also routines with parasize=0 }
  1643. (*
  1644. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1645. begin
  1646. { complex return values are removed from stack in C code PM }
  1647. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1648. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1649. else
  1650. list.concat(taicpu.op_none(A_RTS,S_NO));
  1651. end
  1652. else if (parasize=0) then
  1653. begin
  1654. list.concat(taicpu.op_none(A_RTS,S_NO));
  1655. end
  1656. else
  1657. begin
  1658. { return with immediate size possible here
  1659. signed!
  1660. RTD is not supported on the coldfire }
  1661. if (current_settings.cputype=cpu_MC68020) and (parasize<$7FFF) then
  1662. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1663. { manually restore the stack }
  1664. else
  1665. begin
  1666. { We must pull the PC Counter from the stack, before }
  1667. { restoring the stack pointer, otherwise the PC would }
  1668. { point to nowhere! }
  1669. { save the PC counter (pop it from the stack) }
  1670. hregister:=NR_A3;
  1671. cg.a_reg_alloc(list,hregister);
  1672. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1673. ref.direction:=dir_inc;
  1674. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1675. { can we do a quick addition ... }
  1676. r:=NR_SP;
  1677. if (parasize > 0) and (parasize < 9) then
  1678. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1679. else { nope ... }
  1680. list.concat(taicpu.op_const_reg(A_ADD,S_L,parasize,r));
  1681. { restore the PC counter (push it on the stack) }
  1682. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1683. ref.direction:=dir_dec;
  1684. cg.a_reg_alloc(list,hregister);
  1685. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hregister,ref));
  1686. list.concat(taicpu.op_none(A_RTS,S_NO));
  1687. end;
  1688. end;
  1689. *)
  1690. end;
  1691. procedure Tcg68k.g_save_registers(list:TAsmList);
  1692. var
  1693. tosave : tcpuregisterset;
  1694. ref : treference;
  1695. begin
  1696. {!!!!!
  1697. tosave:=std_saved_registers;
  1698. { only save the registers which are not used and must be saved }
  1699. tosave:=tosave*(rg[R_INTREGISTER].used_in_proc+rg[R_ADDRESSREGISTER].used_in_proc);
  1700. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1701. ref.direction:=dir_dec;
  1702. if tosave<>[] then
  1703. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,tosave,ref));
  1704. }
  1705. end;
  1706. procedure Tcg68k.g_restore_registers(list:TAsmList);
  1707. var
  1708. torestore : tcpuregisterset;
  1709. r:Tregister;
  1710. ref : treference;
  1711. begin
  1712. {!!!!!!!!
  1713. torestore:=std_saved_registers;
  1714. { should be intersected with used regs, no ? }
  1715. torestore:=torestore*(rg[R_INTREGISTER].used_in_proc+rg[R_ADDRESSREGISTER].used_in_proc);
  1716. reference_reset_base(ref,NR_STACK_POINTER_REG,0);
  1717. ref.direction:=dir_inc;
  1718. if torestore<>[] then
  1719. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,ref,torestore));
  1720. }
  1721. end;
  1722. {
  1723. procedure tcg68k.g_save_all_registers(list : TAsmList);
  1724. begin
  1725. end;
  1726. procedure tcg68k.g_restore_all_registers(list : TAsmList;const funcretparaloc:TCGPara);
  1727. begin
  1728. end;
  1729. }
  1730. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1731. begin
  1732. case _oldsize of
  1733. { sign extend }
  1734. OS_S8:
  1735. begin
  1736. if (isaddressregister(reg)) then
  1737. internalerror(20020729);
  1738. if (current_settings.cputype = cpu_MC68000) then
  1739. begin
  1740. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1741. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1742. end
  1743. else
  1744. begin
  1745. // list.concat(tai_comment.create(strpnew('sign extend byte')));
  1746. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1747. end;
  1748. end;
  1749. OS_S16:
  1750. begin
  1751. if (isaddressregister(reg)) then
  1752. internalerror(20020729);
  1753. // list.concat(tai_comment.create(strpnew('sign extend word')));
  1754. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1755. end;
  1756. { zero extend }
  1757. OS_8:
  1758. begin
  1759. // list.concat(tai_comment.create(strpnew('zero extend byte')));
  1760. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1761. end;
  1762. OS_16:
  1763. begin
  1764. // list.concat(tai_comment.create(strpnew('zero extend word')));
  1765. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1766. end;
  1767. end; { otherwise the size is already correct }
  1768. end;
  1769. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1770. var
  1771. ai : taicpu;
  1772. begin
  1773. if cond=OC_None then
  1774. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1775. else
  1776. begin
  1777. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1778. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1779. end;
  1780. ai.is_jmp:=true;
  1781. list.concat(ai);
  1782. end;
  1783. procedure tcg68k.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1784. {
  1785. procedure loadvmttor11;
  1786. var
  1787. href : treference;
  1788. begin
  1789. reference_reset_base(href,NR_R3,0);
  1790. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R11);
  1791. end;
  1792. procedure op_onr11methodaddr;
  1793. var
  1794. href : treference;
  1795. begin
  1796. if (procdef.extnumber=$ffff) then
  1797. Internalerror(200006139);
  1798. { call/jmp vmtoffs(%eax) ; method offs }
  1799. reference_reset_base(href,NR_R11,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber));
  1800. if not((longint(href.offset) >= low(smallint)) and
  1801. (longint(href.offset) <= high(smallint))) then
  1802. begin
  1803. list.concat(taicpu.op_reg_reg_const(A_ADDIS,NR_R11,NR_R11,
  1804. smallint((href.offset shr 16)+ord(smallint(href.offset and $ffff) < 0))));
  1805. href.offset := smallint(href.offset and $ffff);
  1806. end;
  1807. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R11,href));
  1808. list.concat(taicpu.op_reg(A_MTCTR,NR_R11));
  1809. list.concat(taicpu.op_none(A_BCTR));
  1810. end;
  1811. }
  1812. var
  1813. make_global : boolean;
  1814. begin
  1815. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1816. Internalerror(200006137);
  1817. if not assigned(procdef.struct) or
  1818. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1819. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1820. Internalerror(200006138);
  1821. if procdef.owner.symtabletype<>ObjectSymtable then
  1822. Internalerror(200109191);
  1823. make_global:=false;
  1824. if (not current_module.is_unit) or
  1825. create_smartlink or
  1826. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1827. make_global:=true;
  1828. if make_global then
  1829. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1830. else
  1831. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1832. { set param1 interface to self }
  1833. // g_adjust_self_value(list,procdef,ioffset);
  1834. { case 4 }
  1835. if (po_virtualmethod in procdef.procoptions) and
  1836. not is_objectpascal_helper(procdef.struct) then
  1837. begin
  1838. // loadvmttor11;
  1839. // op_onr11methodaddr;
  1840. end
  1841. { case 0 }
  1842. else
  1843. // list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1844. List.concat(Tai_symbol_end.Createname(labelname));
  1845. end;
  1846. {****************************************************************************}
  1847. { TCG64F68K }
  1848. {****************************************************************************}
  1849. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  1850. var
  1851. hreg1, hreg2 : tregister;
  1852. opcode : tasmop;
  1853. instr : taicpu;
  1854. begin
  1855. // writeln('a_op64_reg_reg');
  1856. opcode := topcg2tasmop[op];
  1857. case op of
  1858. OP_ADD :
  1859. begin
  1860. { if one of these three registers is an address
  1861. register, we'll really get into problems!
  1862. }
  1863. if isaddressregister(regdst.reglo) or
  1864. isaddressregister(regdst.reghi) or
  1865. isaddressregister(regsrc.reghi) then
  1866. internalerror(20020817);
  1867. list.concat(taicpu.op_reg_reg(A_ADD,S_L,regsrc.reglo,regdst.reglo));
  1868. list.concat(taicpu.op_reg_reg(A_ADDX,S_L,regsrc.reghi,regdst.reghi));
  1869. end;
  1870. OP_AND,OP_OR :
  1871. begin
  1872. { at least one of the registers must be a data register }
  1873. if (isaddressregister(regdst.reglo) and
  1874. isaddressregister(regsrc.reglo)) or
  1875. (isaddressregister(regsrc.reghi) and
  1876. isaddressregister(regdst.reghi))
  1877. then
  1878. internalerror(20020817);
  1879. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  1880. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  1881. end;
  1882. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  1883. OP_IDIV,OP_DIV,
  1884. OP_IMUL,OP_MUL: internalerror(2002081701);
  1885. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  1886. OP_SAR,OP_SHL,OP_SHR: internalerror(2002081702);
  1887. OP_SUB:
  1888. begin
  1889. { if one of these three registers is an address
  1890. register, we'll really get into problems!
  1891. }
  1892. if isaddressregister(regdst.reglo) or
  1893. isaddressregister(regdst.reghi) or
  1894. isaddressregister(regsrc.reghi) then
  1895. internalerror(20020817);
  1896. list.concat(taicpu.op_reg_reg(A_SUB,S_L,regsrc.reglo,regdst.reglo));
  1897. list.concat(taicpu.op_reg_reg(A_SUBX,S_L,regsrc.reghi,regdst.reghi));
  1898. end;
  1899. OP_XOR:
  1900. begin
  1901. if isaddressregister(regdst.reglo) or
  1902. isaddressregister(regsrc.reglo) or
  1903. isaddressregister(regsrc.reghi) or
  1904. isaddressregister(regdst.reghi) then
  1905. internalerror(20020817);
  1906. list.concat(taicpu.op_reg_reg(A_EOR,S_L,regsrc.reglo,regdst.reglo));
  1907. list.concat(taicpu.op_reg_reg(A_EOR,S_L,regsrc.reghi,regdst.reghi));
  1908. end;
  1909. OP_NEG:
  1910. begin
  1911. if isaddressregister(regdst.reglo) or
  1912. isaddressregister(regdst.reghi) then
  1913. internalerror(2012110402);
  1914. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  1915. cg.add_move_instruction(instr);
  1916. list.concat(instr);
  1917. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  1918. cg.add_move_instruction(instr);
  1919. list.concat(instr);
  1920. list.concat(taicpu.op_reg(A_NEG,S_L,regdst.reglo));
  1921. list.concat(taicpu.op_reg(A_NEGX,S_L,regdst.reghi));
  1922. end;
  1923. OP_NOT:
  1924. begin
  1925. if isaddressregister(regdst.reglo) or
  1926. isaddressregister(regdst.reghi) then
  1927. internalerror(2012110401);
  1928. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  1929. cg.add_move_instruction(instr);
  1930. list.concat(instr);
  1931. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  1932. cg.add_move_instruction(instr);
  1933. list.concat(instr);
  1934. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reglo));
  1935. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  1936. end;
  1937. end; { end case }
  1938. end;
  1939. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  1940. var
  1941. lowvalue : cardinal;
  1942. highvalue : cardinal;
  1943. hreg : tregister;
  1944. begin
  1945. // writeln('a_op64_const_reg');
  1946. { is it optimized out ? }
  1947. // if cg.optimize64_op_const_reg(list,op,value,reg) then
  1948. // exit;
  1949. lowvalue := cardinal(value);
  1950. highvalue:= value shr 32;
  1951. { the destination registers must be data registers }
  1952. if isaddressregister(regdst.reglo) or
  1953. isaddressregister(regdst.reghi) then
  1954. internalerror(20020817);
  1955. case op of
  1956. OP_ADD :
  1957. begin
  1958. hreg:=cg.getintregister(list,OS_INT);
  1959. list.concat(taicpu.op_const_reg(A_MOVE,S_L,highvalue,hreg));
  1960. list.concat(taicpu.op_const_reg(A_ADD,S_L,lowvalue,regdst.reglo));
  1961. list.concat(taicpu.op_reg_reg(A_ADDX,S_L,hreg,regdst.reghi));
  1962. end;
  1963. OP_AND :
  1964. begin
  1965. list.concat(taicpu.op_const_reg(A_AND,S_L,lowvalue,regdst.reglo));
  1966. list.concat(taicpu.op_const_reg(A_AND,S_L,highvalue,regdst.reghi));
  1967. end;
  1968. OP_OR :
  1969. begin
  1970. list.concat(taicpu.op_const_reg(A_OR,S_L,lowvalue,regdst.reglo));
  1971. list.concat(taicpu.op_const_reg(A_OR,S_L,highvalue,regdst.reghi));
  1972. end;
  1973. { this is handled in 1st pass for 32-bit cpus (helper call) }
  1974. OP_IDIV,OP_DIV,
  1975. OP_IMUL,OP_MUL: internalerror(2002081701);
  1976. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  1977. OP_SAR,OP_SHL,OP_SHR: internalerror(2002081702);
  1978. OP_SUB:
  1979. begin
  1980. hreg:=cg.getintregister(list,OS_INT);
  1981. list.concat(taicpu.op_const_reg(A_MOVE,S_L,highvalue,hreg));
  1982. list.concat(taicpu.op_const_reg(A_SUB,S_L,lowvalue,regdst.reglo));
  1983. list.concat(taicpu.op_reg_reg(A_SUBX,S_L,hreg,regdst.reghi));
  1984. end;
  1985. OP_XOR:
  1986. begin
  1987. list.concat(taicpu.op_const_reg(A_EOR,S_L,lowvalue,regdst.reglo));
  1988. list.concat(taicpu.op_const_reg(A_EOR,S_L,highvalue,regdst.reghi));
  1989. end;
  1990. { these should have been handled already by earlier passes }
  1991. OP_NOT, OP_NEG:
  1992. internalerror(2012110403);
  1993. end; { end case }
  1994. end;
  1995. procedure create_codegen;
  1996. begin
  1997. cg := tcg68k.create;
  1998. cg64 :=tcg64f68k.create;
  1999. end;
  2000. end.