cgcpu.pas 82 KB

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