aasmcpu.pas 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Contains the assembler object for the ARM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,globtype,globals,verbose,
  22. aasmbase,aasmtai,aasmdata,aasmsym,
  23. ogbase,
  24. symtype,
  25. cpubase,cpuinfo,cgbase,cgutils;
  26. const
  27. { "mov reg,reg" source operand number }
  28. O_MOV_SOURCE = 1;
  29. { "mov reg,reg" source operand number }
  30. O_MOV_DEST = 0;
  31. { Operand types }
  32. OT_NONE = $00000000;
  33. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  34. OT_BITS16 = $00000002;
  35. OT_BITS32 = $00000004;
  36. OT_BITS64 = $00000008; { FPU only }
  37. OT_BITS80 = $00000010;
  38. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  39. OT_NEAR = $00000040;
  40. OT_SHORT = $00000080;
  41. OT_BITSTINY = $00000100; { fpu constant }
  42. OT_BITSSHIFTER =
  43. $00000200;
  44. OT_SIZE_MASK = $000003FF; { all the size attributes }
  45. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  46. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  47. OT_TO = $00000200; { operand is followed by a colon }
  48. { reverse effect in FADD, FSUB &c }
  49. OT_COLON = $00000400;
  50. OT_SHIFTEROP = $00000800;
  51. OT_REGISTER = $00001000;
  52. OT_IMMEDIATE = $00002000;
  53. OT_REGLIST = $00008000;
  54. OT_IMM8 = $00002001;
  55. OT_IMM24 = $00002002;
  56. OT_IMM32 = $00002004;
  57. OT_IMM64 = $00002008;
  58. OT_IMM80 = $00002010;
  59. OT_IMMTINY = $00002100;
  60. OT_IMMSHIFTER= $00002200;
  61. OT_IMMEDIATE24 = OT_IMM24;
  62. OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
  63. OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
  64. OT_IMMEDIATESHIFTER = OT_IMMSHIFTER;
  65. OT_IMMEDIATEFPU = OT_IMMTINY;
  66. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  67. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  68. OT_REG8 = $00201001;
  69. OT_REG16 = $00201002;
  70. OT_REG32 = $00201004;
  71. OT_REG64 = $00201008;
  72. OT_VREG = $00201010; { vector register }
  73. OT_MEMORY = $00204000; { register number in 'basereg' }
  74. OT_MEM8 = $00204001;
  75. OT_MEM16 = $00204002;
  76. OT_MEM32 = $00204004;
  77. OT_MEM64 = $00204008;
  78. OT_MEM80 = $00204010;
  79. { word/byte load/store }
  80. OT_AM2 = $00010000;
  81. { misc ld/st operations }
  82. OT_AM3 = $00020000;
  83. { multiple ld/st operations }
  84. OT_AM4 = $00040000;
  85. { co proc. ld/st operations }
  86. OT_AM5 = $00080000;
  87. OT_AMMASK = $000f0000;
  88. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  89. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  90. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  91. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  92. OT_FPUREG = $01000000; { floating point stack registers }
  93. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  94. { a mask for the following }
  95. OT_MEM_OFFS = $00604000; { special type of EA }
  96. { simple [address] offset }
  97. OT_ONENESS = $00800000; { special type of immediate operand }
  98. { so UNITY == IMMEDIATE | ONENESS }
  99. OT_UNITY = $00802000; { for shift/rotate instructions }
  100. instabentries = {$i armnop.inc}
  101. maxinfolen = 5;
  102. IF_NONE = $00000000;
  103. IF_ARMMASK = $000F0000;
  104. IF_ARM7 = $00070000;
  105. IF_FPMASK = $00F00000;
  106. IF_FPA = $00100000;
  107. { if the instruction can change in a second pass }
  108. IF_PASS2 = longint($80000000);
  109. type
  110. TInsTabCache=array[TasmOp] of longint;
  111. PInsTabCache=^TInsTabCache;
  112. tinsentry = record
  113. opcode : tasmop;
  114. ops : byte;
  115. optypes : array[0..3] of longint;
  116. code : array[0..maxinfolen] of char;
  117. flags : longint;
  118. end;
  119. pinsentry=^tinsentry;
  120. const
  121. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  122. var
  123. InsTabCache : PInsTabCache;
  124. type
  125. taicpu = class(tai_cpu_abstract_sym)
  126. oppostfix : TOpPostfix;
  127. roundingmode : troundingmode;
  128. procedure loadshifterop(opidx:longint;const so:tshifterop);
  129. procedure loadregset(opidx:longint;const s:tcpuregisterset);
  130. constructor op_none(op : tasmop);
  131. constructor op_reg(op : tasmop;_op1 : tregister);
  132. constructor op_const(op : tasmop;_op1 : longint);
  133. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  134. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  135. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  136. constructor op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  137. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  138. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  139. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  140. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  141. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  142. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  143. { SFM/LFM }
  144. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  145. { *M*LL }
  146. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  147. { this is for Jmp instructions }
  148. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  149. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  150. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  151. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  152. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  153. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  154. function spilling_get_operation_type(opnr: longint): topertype;override;
  155. { assembler }
  156. public
  157. { the next will reset all instructions that can change in pass 2 }
  158. procedure ResetPass1;override;
  159. procedure ResetPass2;override;
  160. function CheckIfValid:boolean;
  161. function GetString:string;
  162. function Pass1(objdata:TObjData):longint;override;
  163. procedure Pass2(objdata:TObjData);override;
  164. protected
  165. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  166. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  167. procedure ppubuildderefimploper(var o:toper);override;
  168. procedure ppuderefoper(var o:toper);override;
  169. private
  170. { next fields are filled in pass1, so pass2 is faster }
  171. inssize : shortint;
  172. insoffset : longint;
  173. LastInsOffset : longint; { need to be public to be reset }
  174. insentry : PInsEntry;
  175. function InsEnd:longint;
  176. procedure create_ot(objdata:TObjData);
  177. function Matches(p:PInsEntry):longint;
  178. function calcsize(p:PInsEntry):shortint;
  179. procedure gencode(objdata:TObjData);
  180. function NeedAddrPrefix(opidx:byte):boolean;
  181. procedure Swapoperands;
  182. function FindInsentry(objdata:TObjData):boolean;
  183. end;
  184. tai_align = class(tai_align_abstract)
  185. { nothing to add }
  186. end;
  187. function spilling_create_load(const ref:treference;r:tregister): tai;
  188. function spilling_create_store(r:tregister; const ref:treference): tai;
  189. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  190. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  191. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  192. { inserts pc relative symbols at places where they are reachable }
  193. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  194. { inserts .pdata section and dummy function prolog needed for arm-wince exception handling }
  195. procedure InsertPData;
  196. procedure InitAsm;
  197. procedure DoneAsm;
  198. implementation
  199. uses
  200. cutils,rgobj,itcpugas;
  201. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  202. begin
  203. allocate_oper(opidx+1);
  204. with oper[opidx]^ do
  205. begin
  206. if typ<>top_shifterop then
  207. begin
  208. clearop(opidx);
  209. new(shifterop);
  210. end;
  211. shifterop^:=so;
  212. typ:=top_shifterop;
  213. if assigned(add_reg_instruction_hook) then
  214. add_reg_instruction_hook(self,shifterop^.rs);
  215. end;
  216. end;
  217. procedure taicpu.loadregset(opidx:longint;const s:tcpuregisterset);
  218. var
  219. i : byte;
  220. begin
  221. allocate_oper(opidx+1);
  222. with oper[opidx]^ do
  223. begin
  224. if typ<>top_regset then
  225. clearop(opidx);
  226. new(regset);
  227. regset^:=s;
  228. typ:=top_regset;
  229. for i:=RS_R0 to RS_R15 do
  230. begin
  231. if assigned(add_reg_instruction_hook) and (i in regset^) then
  232. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  233. end;
  234. end;
  235. end;
  236. {*****************************************************************************
  237. taicpu Constructors
  238. *****************************************************************************}
  239. constructor taicpu.op_none(op : tasmop);
  240. begin
  241. inherited create(op);
  242. end;
  243. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  244. begin
  245. inherited create(op);
  246. ops:=1;
  247. loadreg(0,_op1);
  248. end;
  249. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  250. begin
  251. inherited create(op);
  252. ops:=1;
  253. loadconst(0,aint(_op1));
  254. end;
  255. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  256. begin
  257. inherited create(op);
  258. ops:=2;
  259. loadreg(0,_op1);
  260. loadreg(1,_op2);
  261. end;
  262. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  263. begin
  264. inherited create(op);
  265. ops:=2;
  266. loadreg(0,_op1);
  267. loadconst(1,aint(_op2));
  268. end;
  269. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  270. begin
  271. inherited create(op);
  272. ops:=2;
  273. loadref(0,_op1);
  274. loadregset(1,_op2);
  275. end;
  276. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  277. begin
  278. inherited create(op);
  279. ops:=2;
  280. loadreg(0,_op1);
  281. loadref(1,_op2);
  282. end;
  283. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  284. begin
  285. inherited create(op);
  286. ops:=3;
  287. loadreg(0,_op1);
  288. loadreg(1,_op2);
  289. loadreg(2,_op3);
  290. end;
  291. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  292. begin
  293. inherited create(op);
  294. ops:=4;
  295. loadreg(0,_op1);
  296. loadreg(1,_op2);
  297. loadreg(2,_op3);
  298. loadreg(3,_op4);
  299. end;
  300. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  301. begin
  302. inherited create(op);
  303. ops:=3;
  304. loadreg(0,_op1);
  305. loadreg(1,_op2);
  306. loadconst(2,aint(_op3));
  307. end;
  308. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  309. begin
  310. inherited create(op);
  311. ops:=3;
  312. loadreg(0,_op1);
  313. loadconst(1,_op2);
  314. loadref(2,_op3);
  315. end;
  316. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  317. begin
  318. inherited create(op);
  319. ops:=3;
  320. loadreg(0,_op1);
  321. loadreg(1,_op2);
  322. loadsymbol(0,_op3,_op3ofs);
  323. end;
  324. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  325. begin
  326. inherited create(op);
  327. ops:=3;
  328. loadreg(0,_op1);
  329. loadreg(1,_op2);
  330. loadref(2,_op3);
  331. end;
  332. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  333. begin
  334. inherited create(op);
  335. ops:=3;
  336. loadreg(0,_op1);
  337. loadreg(1,_op2);
  338. loadshifterop(2,_op3);
  339. end;
  340. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  341. begin
  342. inherited create(op);
  343. ops:=4;
  344. loadreg(0,_op1);
  345. loadreg(1,_op2);
  346. loadreg(2,_op3);
  347. loadshifterop(3,_op4);
  348. end;
  349. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  350. begin
  351. inherited create(op);
  352. condition:=cond;
  353. ops:=1;
  354. loadsymbol(0,_op1,0);
  355. end;
  356. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  357. begin
  358. inherited create(op);
  359. ops:=1;
  360. loadsymbol(0,_op1,0);
  361. end;
  362. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  363. begin
  364. inherited create(op);
  365. ops:=1;
  366. loadsymbol(0,_op1,_op1ofs);
  367. end;
  368. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  369. begin
  370. inherited create(op);
  371. ops:=2;
  372. loadreg(0,_op1);
  373. loadsymbol(1,_op2,_op2ofs);
  374. end;
  375. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  376. begin
  377. inherited create(op);
  378. ops:=2;
  379. loadsymbol(0,_op1,_op1ofs);
  380. loadref(1,_op2);
  381. end;
  382. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  383. begin
  384. { allow the register allocator to remove unnecessary moves }
  385. result:=(((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  386. ((opcode=A_MVF) and (regtype = R_FPUREGISTER))
  387. ) and
  388. (condition=C_None) and
  389. (ops=2) and
  390. (oper[0]^.typ=top_reg) and
  391. (oper[1]^.typ=top_reg) and
  392. (oper[0]^.reg=oper[1]^.reg);
  393. end;
  394. function spilling_create_load(const ref:treference;r:tregister): tai;
  395. begin
  396. case getregtype(r) of
  397. R_INTREGISTER :
  398. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  399. R_FPUREGISTER :
  400. { use lfm because we don't know the current internal format
  401. and avoid exceptions
  402. }
  403. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  404. else
  405. internalerror(200401041);
  406. end;
  407. end;
  408. function spilling_create_store(r:tregister; const ref:treference): tai;
  409. begin
  410. case getregtype(r) of
  411. R_INTREGISTER :
  412. result:=taicpu.op_reg_ref(A_STR,r,ref);
  413. R_FPUREGISTER :
  414. { use sfm because we don't know the current internal format
  415. and avoid exceptions
  416. }
  417. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  418. else
  419. internalerror(200401041);
  420. end;
  421. end;
  422. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  423. begin
  424. case opcode of
  425. A_ADC,A_ADD,A_AND,
  426. A_EOR,A_CLZ,
  427. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  428. A_LDRSH,A_LDRT,
  429. A_MOV,A_MVN,A_MLA,A_MUL,
  430. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  431. A_SWP,A_SWPB,
  432. A_LDF,A_FLT,A_FIX,
  433. A_ADF,A_DVF,A_FDV,A_FML,
  434. A_RFS,A_RFC,A_RDF,
  435. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  436. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  437. A_LFM:
  438. if opnr=0 then
  439. result:=operand_write
  440. else
  441. result:=operand_read;
  442. A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  443. A_CMN,A_CMP,A_TEQ,A_TST,
  444. A_CMF,A_CMFE,A_WFS,A_CNF:
  445. result:=operand_read;
  446. A_SMLAL,A_UMLAL:
  447. if opnr in [0,1] then
  448. result:=operand_readwrite
  449. else
  450. result:=operand_read;
  451. A_SMULL,A_UMULL:
  452. if opnr in [0,1] then
  453. result:=operand_write
  454. else
  455. result:=operand_read;
  456. A_STR,A_STRB,A_STRBT,
  457. A_STRH,A_STRT,A_STF,A_SFM:
  458. { important is what happens with the involved registers }
  459. if opnr=0 then
  460. result := operand_read
  461. else
  462. { check for pre/post indexed }
  463. result := operand_read;
  464. else
  465. internalerror(200403151);
  466. end;
  467. end;
  468. procedure BuildInsTabCache;
  469. var
  470. i : longint;
  471. begin
  472. new(instabcache);
  473. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  474. i:=0;
  475. while (i<InsTabEntries) do
  476. begin
  477. if InsTabCache^[InsTab[i].Opcode]=-1 then
  478. InsTabCache^[InsTab[i].Opcode]:=i;
  479. inc(i);
  480. end;
  481. end;
  482. procedure InitAsm;
  483. begin
  484. if not assigned(instabcache) then
  485. BuildInsTabCache;
  486. end;
  487. procedure DoneAsm;
  488. begin
  489. if assigned(instabcache) then
  490. begin
  491. dispose(instabcache);
  492. instabcache:=nil;
  493. end;
  494. end;
  495. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  496. begin
  497. i.oppostfix:=pf;
  498. result:=i;
  499. end;
  500. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  501. begin
  502. i.roundingmode:=rm;
  503. result:=i;
  504. end;
  505. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  506. begin
  507. i.condition:=c;
  508. result:=i;
  509. end;
  510. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  511. var
  512. curpos,
  513. penalty,
  514. lastpos : longint;
  515. curop : longint;
  516. curtai : tai;
  517. curdatatai,hp,hp2 : tai;
  518. curdata : TAsmList;
  519. l : tasmlabel;
  520. removeref : boolean;
  521. begin
  522. curdata:=TAsmList.create;
  523. lastpos:=-1;
  524. curpos:=0;
  525. curtai:=tai(list.first);
  526. while assigned(curtai) do
  527. begin
  528. { instruction? }
  529. if curtai.typ=ait_instruction then
  530. begin
  531. { walk through all operand of the instruction }
  532. for curop:=0 to taicpu(curtai).ops-1 do
  533. begin
  534. { reference? }
  535. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  536. begin
  537. { pc relative symbol? }
  538. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  539. if assigned(curdatatai) and
  540. { move only if we're at the first reference of a label }
  541. (taicpu(curtai).oper[curop]^.ref^.offset=0) then
  542. begin
  543. { check if symbol already used. }
  544. { if yes, reuse the symbol }
  545. hp:=tai(curdatatai.next);
  546. removeref:=false;
  547. if assigned(hp) and (hp.typ=ait_const) then
  548. begin
  549. hp2:=tai(curdata.first);
  550. while assigned(hp2) do
  551. begin
  552. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  553. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  554. then
  555. begin
  556. with taicpu(curtai).oper[curop]^.ref^ do
  557. begin
  558. symboldata:=hp2.previous;
  559. symbol:=tai_label(hp2.previous).labsym;
  560. end;
  561. removeref:=true;
  562. break;
  563. end;
  564. hp2:=tai(hp2.next);
  565. end;
  566. end;
  567. { move or remove symbol reference }
  568. repeat
  569. hp:=tai(curdatatai.next);
  570. listtoinsert.remove(curdatatai);
  571. if removeref then
  572. curdatatai.free
  573. else
  574. curdata.concat(curdatatai);
  575. curdatatai:=hp;
  576. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  577. if lastpos=-1 then
  578. lastpos:=curpos;
  579. end;
  580. end;
  581. end;
  582. inc(curpos);
  583. end
  584. else
  585. if curtai.typ=ait_const then
  586. inc(curpos);
  587. { special case for case jump tables }
  588. if assigned(curtai.next) and
  589. (taicpu(curtai.next).typ=ait_instruction) and
  590. (taicpu(curtai.next).opcode=A_LDR) and
  591. (taicpu(curtai.next).oper[0]^.typ=top_reg) and
  592. (taicpu(curtai.next).oper[0]^.reg=NR_PC) then
  593. begin
  594. penalty:=1;
  595. hp:=tai(curtai.next);
  596. while assigned(hp) and (hp.typ=ait_const) do
  597. begin
  598. inc(penalty);
  599. hp:=tai(hp.next);
  600. end;
  601. end
  602. else
  603. penalty:=0;
  604. { split only at real instructions else the test below fails }
  605. if ((curpos-lastpos+penalty)>1016) and (curtai.typ=ait_instruction) and
  606. (
  607. { don't split loads of pc to lr and the following move }
  608. not(
  609. (taicpu(curtai).opcode=A_MOV) and
  610. (taicpu(curtai).oper[0]^.typ=top_reg) and
  611. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  612. (taicpu(curtai).oper[1]^.typ=top_reg) and
  613. (taicpu(curtai).oper[1]^.reg=NR_PC)
  614. )
  615. ) then
  616. begin
  617. lastpos:=curpos;
  618. hp:=tai(curtai.next);
  619. current_asmdata.getjumplabel(l);
  620. curdata.insert(taicpu.op_sym(A_B,l));
  621. curdata.concat(tai_label.create(l));
  622. list.insertlistafter(curtai,curdata);
  623. curtai:=hp;
  624. end
  625. else
  626. curtai:=tai(curtai.next);
  627. end;
  628. list.concatlist(curdata);
  629. curdata.free;
  630. end;
  631. procedure InsertPData;
  632. var
  633. prolog: TAsmList;
  634. begin
  635. prolog:=TAsmList.create;
  636. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(aint),secorder_begin);
  637. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  638. prolog.concat(Tai_const.Create_32bit(0));
  639. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  640. { dummy function }
  641. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  642. current_asmdata.asmlists[al_start].insertList(prolog);
  643. prolog.Free;
  644. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(aint));
  645. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  646. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  647. end;
  648. (*
  649. Floating point instruction format information, taken from the linux kernel
  650. ARM Floating Point Instruction Classes
  651. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  652. |c o n d|1 1 0 P|U|u|W|L| Rn |v| Fd |0|0|0|1| o f f s e t | CPDT
  653. |c o n d|1 1 0 P|U|w|W|L| Rn |x| Fd |0|0|1|0| o f f s e t | CPDT (copro 2)
  654. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  655. |c o n d|1 1 1 0|a|b|c|d|e| Fn |j| Fd |0|0|0|1|f|g|h|0|i| Fm | CPDO
  656. |c o n d|1 1 1 0|a|b|c|L|e| Fn | Rd |0|0|0|1|f|g|h|1|i| Fm | CPRT
  657. |c o n d|1 1 1 0|a|b|c|1|e| Fn |1|1|1|1|0|0|0|1|f|g|h|1|i| Fm | comparisons
  658. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  659. CPDT data transfer instructions
  660. LDF, STF, LFM (copro 2), SFM (copro 2)
  661. CPDO dyadic arithmetic instructions
  662. ADF, MUF, SUF, RSF, DVF, RDF,
  663. POW, RPW, RMF, FML, FDV, FRD, POL
  664. CPDO monadic arithmetic instructions
  665. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  666. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  667. CPRT joint arithmetic/data transfer instructions
  668. FIX (arithmetic followed by load/store)
  669. FLT (load/store followed by arithmetic)
  670. CMF, CNF CMFE, CNFE (comparisons)
  671. WFS, RFS (write/read floating point status register)
  672. WFC, RFC (write/read floating point control register)
  673. cond condition codes
  674. P pre/post index bit: 0 = postindex, 1 = preindex
  675. U up/down bit: 0 = stack grows down, 1 = stack grows up
  676. W write back bit: 1 = update base register (Rn)
  677. L load/store bit: 0 = store, 1 = load
  678. Rn base register
  679. Rd destination/source register
  680. Fd floating point destination register
  681. Fn floating point source register
  682. Fm floating point source register or floating point constant
  683. uv transfer length (TABLE 1)
  684. wx register count (TABLE 2)
  685. abcd arithmetic opcode (TABLES 3 & 4)
  686. ef destination size (rounding precision) (TABLE 5)
  687. gh rounding mode (TABLE 6)
  688. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  689. i constant bit: 1 = constant (TABLE 6)
  690. */
  691. /*
  692. TABLE 1
  693. +-------------------------+---+---+---------+---------+
  694. | Precision | u | v | FPSR.EP | length |
  695. +-------------------------+---+---+---------+---------+
  696. | Single | 0 | 0 | x | 1 words |
  697. | Double | 1 | 1 | x | 2 words |
  698. | Extended | 1 | 1 | x | 3 words |
  699. | Packed decimal | 1 | 1 | 0 | 3 words |
  700. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  701. +-------------------------+---+---+---------+---------+
  702. Note: x = don't care
  703. */
  704. /*
  705. TABLE 2
  706. +---+---+---------------------------------+
  707. | w | x | Number of registers to transfer |
  708. +---+---+---------------------------------+
  709. | 0 | 1 | 1 |
  710. | 1 | 0 | 2 |
  711. | 1 | 1 | 3 |
  712. | 0 | 0 | 4 |
  713. +---+---+---------------------------------+
  714. */
  715. /*
  716. TABLE 3: Dyadic Floating Point Opcodes
  717. +---+---+---+---+----------+-----------------------+-----------------------+
  718. | a | b | c | d | Mnemonic | Description | Operation |
  719. +---+---+---+---+----------+-----------------------+-----------------------+
  720. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  721. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  722. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  723. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  724. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  725. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  726. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  727. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  728. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  729. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  730. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  731. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  732. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  733. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  734. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  735. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  736. +---+---+---+---+----------+-----------------------+-----------------------+
  737. Note: POW, RPW, POL are deprecated, and are available for backwards
  738. compatibility only.
  739. */
  740. /*
  741. TABLE 4: Monadic Floating Point Opcodes
  742. +---+---+---+---+----------+-----------------------+-----------------------+
  743. | a | b | c | d | Mnemonic | Description | Operation |
  744. +---+---+---+---+----------+-----------------------+-----------------------+
  745. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  746. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  747. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  748. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  749. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  750. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  751. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  752. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  753. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  754. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  755. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  756. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  757. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  758. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  759. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  760. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  761. +---+---+---+---+----------+-----------------------+-----------------------+
  762. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  763. available for backwards compatibility only.
  764. */
  765. /*
  766. TABLE 5
  767. +-------------------------+---+---+
  768. | Rounding Precision | e | f |
  769. +-------------------------+---+---+
  770. | IEEE Single precision | 0 | 0 |
  771. | IEEE Double precision | 0 | 1 |
  772. | IEEE Extended precision | 1 | 0 |
  773. | undefined (trap) | 1 | 1 |
  774. +-------------------------+---+---+
  775. */
  776. /*
  777. TABLE 5
  778. +---------------------------------+---+---+
  779. | Rounding Mode | g | h |
  780. +---------------------------------+---+---+
  781. | Round to nearest (default) | 0 | 0 |
  782. | Round toward plus infinity | 0 | 1 |
  783. | Round toward negative infinity | 1 | 0 |
  784. | Round toward zero | 1 | 1 |
  785. +---------------------------------+---+---+
  786. *)
  787. function taicpu.GetString:string;
  788. var
  789. i : longint;
  790. s : string;
  791. addsize : boolean;
  792. begin
  793. s:='['+gas_op2str[opcode];
  794. for i:=0 to ops-1 do
  795. begin
  796. with oper[i]^ do
  797. begin
  798. if i=0 then
  799. s:=s+' '
  800. else
  801. s:=s+',';
  802. { type }
  803. addsize:=false;
  804. if (ot and OT_VREG)=OT_VREG then
  805. s:=s+'vreg'
  806. else
  807. if (ot and OT_FPUREG)=OT_FPUREG then
  808. s:=s+'fpureg'
  809. else
  810. if (ot and OT_REGISTER)=OT_REGISTER then
  811. begin
  812. s:=s+'reg';
  813. addsize:=true;
  814. end
  815. else
  816. if (ot and OT_REGLIST)=OT_REGLIST then
  817. begin
  818. s:=s+'reglist';
  819. addsize:=false;
  820. end
  821. else
  822. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  823. begin
  824. s:=s+'imm';
  825. addsize:=true;
  826. end
  827. else
  828. if (ot and OT_MEMORY)=OT_MEMORY then
  829. begin
  830. s:=s+'mem';
  831. addsize:=true;
  832. if (ot and OT_AM2)<>0 then
  833. s:=s+' am2 ';
  834. end
  835. else
  836. s:=s+'???';
  837. { size }
  838. if addsize then
  839. begin
  840. if (ot and OT_BITS8)<>0 then
  841. s:=s+'8'
  842. else
  843. if (ot and OT_BITS16)<>0 then
  844. s:=s+'24'
  845. else
  846. if (ot and OT_BITS32)<>0 then
  847. s:=s+'32'
  848. else
  849. if (ot and OT_BITSSHIFTER)<>0 then
  850. s:=s+'shifter'
  851. else
  852. s:=s+'??';
  853. { signed }
  854. if (ot and OT_SIGNED)<>0 then
  855. s:=s+'s';
  856. end;
  857. end;
  858. end;
  859. GetString:=s+']';
  860. end;
  861. procedure taicpu.ResetPass1;
  862. begin
  863. { we need to reset everything here, because the choosen insentry
  864. can be invalid for a new situation where the previously optimized
  865. insentry is not correct }
  866. InsEntry:=nil;
  867. InsSize:=0;
  868. LastInsOffset:=-1;
  869. end;
  870. procedure taicpu.ResetPass2;
  871. begin
  872. { we are here in a second pass, check if the instruction can be optimized }
  873. if assigned(InsEntry) and
  874. ((InsEntry^.flags and IF_PASS2)<>0) then
  875. begin
  876. InsEntry:=nil;
  877. InsSize:=0;
  878. end;
  879. LastInsOffset:=-1;
  880. end;
  881. function taicpu.CheckIfValid:boolean;
  882. begin
  883. end;
  884. function taicpu.Pass1(objdata:TObjData):longint;
  885. var
  886. ldr2op : array[PF_B..PF_T] of tasmop = (
  887. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  888. str2op : array[PF_B..PF_T] of tasmop = (
  889. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  890. begin
  891. Pass1:=0;
  892. { Save the old offset and set the new offset }
  893. InsOffset:=ObjData.CurrObjSec.Size;
  894. { Error? }
  895. if (Insentry=nil) and (InsSize=-1) then
  896. exit;
  897. { set the file postion }
  898. current_filepos:=fileinfo;
  899. { tranlate LDR+postfix to complete opcode }
  900. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  901. begin
  902. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  903. opcode:=ldr2op[oppostfix]
  904. else
  905. internalerror(2005091001);
  906. if opcode=A_None then
  907. internalerror(2005091004);
  908. { postfix has been added to opcode }
  909. oppostfix:=PF_None;
  910. end
  911. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  912. begin
  913. if (oppostfix in [low(str2op)..high(str2op)]) then
  914. opcode:=str2op[oppostfix]
  915. else
  916. internalerror(2005091002);
  917. if opcode=A_None then
  918. internalerror(2005091003);
  919. { postfix has been added to opcode }
  920. oppostfix:=PF_None;
  921. end;
  922. { Get InsEntry }
  923. if FindInsEntry(objdata) then
  924. begin
  925. InsSize:=4;
  926. LastInsOffset:=InsOffset;
  927. Pass1:=InsSize;
  928. exit;
  929. end;
  930. LastInsOffset:=-1;
  931. end;
  932. procedure taicpu.Pass2(objdata:TObjData);
  933. begin
  934. { error in pass1 ? }
  935. if insentry=nil then
  936. exit;
  937. current_filepos:=fileinfo;
  938. { Generate the instruction }
  939. GenCode(objdata);
  940. end;
  941. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  942. begin
  943. end;
  944. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  945. begin
  946. end;
  947. procedure taicpu.ppubuildderefimploper(var o:toper);
  948. begin
  949. end;
  950. procedure taicpu.ppuderefoper(var o:toper);
  951. begin
  952. end;
  953. function taicpu.InsEnd:longint;
  954. begin
  955. end;
  956. procedure taicpu.create_ot(objdata:TObjData);
  957. var
  958. i,l,relsize : longint;
  959. dummy : byte;
  960. currsym : TObjSymbol;
  961. begin
  962. if ops=0 then
  963. exit;
  964. { update oper[].ot field }
  965. for i:=0 to ops-1 do
  966. with oper[i]^ do
  967. begin
  968. case typ of
  969. top_regset:
  970. begin
  971. ot:=OT_REGLIST;
  972. end;
  973. top_reg :
  974. begin
  975. case getregtype(reg) of
  976. R_INTREGISTER:
  977. ot:=OT_REG32 or OT_SHIFTEROP;
  978. R_FPUREGISTER:
  979. ot:=OT_FPUREG;
  980. else
  981. internalerror(2005090901);
  982. end;
  983. end;
  984. top_ref :
  985. begin
  986. if ref^.refaddr=addr_no then
  987. begin
  988. { create ot field }
  989. { we should get the size here dependend on the
  990. instruction }
  991. if (ot and OT_SIZE_MASK)=0 then
  992. ot:=OT_MEMORY or OT_BITS32
  993. else
  994. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  995. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  996. ot:=ot or OT_MEM_OFFS;
  997. { if we need to fix a reference, we do it here }
  998. { pc relative addressing }
  999. if (ref^.base=NR_NO) and
  1000. (ref^.index=NR_NO) and
  1001. (ref^.shiftmode=SM_None)
  1002. { at least we should check if the destination symbol
  1003. is in a text section }
  1004. { and
  1005. (ref^.symbol^.owner="text") } then
  1006. ref^.base:=NR_PC;
  1007. { determine possible address modes }
  1008. if (ref^.base<>NR_NO) and
  1009. (
  1010. (
  1011. (ref^.index=NR_NO) and
  1012. (ref^.shiftmode=SM_None) and
  1013. (ref^.offset>=-4097) and
  1014. (ref^.offset<=4097)
  1015. ) or
  1016. (
  1017. (ref^.shiftmode=SM_None) and
  1018. (ref^.offset=0)
  1019. ) or
  1020. (
  1021. (ref^.index<>NR_NO) and
  1022. (ref^.shiftmode<>SM_None) and
  1023. (ref^.shiftimm<=31) and
  1024. (ref^.offset=0)
  1025. )
  1026. ) then
  1027. ot:=ot or OT_AM2;
  1028. if (ref^.index<>NR_NO) and
  1029. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1030. (
  1031. (ref^.base=NR_NO) and
  1032. (ref^.shiftmode=SM_None) and
  1033. (ref^.offset=0)
  1034. ) then
  1035. ot:=ot or OT_AM4;
  1036. end
  1037. else
  1038. begin
  1039. l:=ref^.offset;
  1040. currsym:=ObjData.symbolref(ref^.symbol);
  1041. if assigned(currsym) then
  1042. inc(l,currsym.address);
  1043. relsize:=(InsOffset+2)-l;
  1044. if (relsize<-33554428) or (relsize>33554428) then
  1045. ot:=OT_IMM32
  1046. else
  1047. ot:=OT_IMM24;
  1048. end;
  1049. end;
  1050. top_local :
  1051. begin
  1052. { we should get the size here dependend on the
  1053. instruction }
  1054. if (ot and OT_SIZE_MASK)=0 then
  1055. ot:=OT_MEMORY or OT_BITS32
  1056. else
  1057. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1058. end;
  1059. top_const :
  1060. begin
  1061. ot:=OT_IMMEDIATE;
  1062. if is_shifter_const(val,dummy) then
  1063. ot:=OT_IMMSHIFTER
  1064. else
  1065. ot:=OT_IMM32
  1066. end;
  1067. top_none :
  1068. begin
  1069. { generated when there was an error in the
  1070. assembler reader. It never happends when generating
  1071. assembler }
  1072. end;
  1073. top_shifterop:
  1074. begin
  1075. ot:=OT_SHIFTEROP;
  1076. end;
  1077. else
  1078. internalerror(200402261);
  1079. end;
  1080. end;
  1081. end;
  1082. function taicpu.Matches(p:PInsEntry):longint;
  1083. { * IF_SM stands for Size Match: any operand whose size is not
  1084. * explicitly specified by the template is `really' intended to be
  1085. * the same size as the first size-specified operand.
  1086. * Non-specification is tolerated in the input instruction, but
  1087. * _wrong_ specification is not.
  1088. *
  1089. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1090. * three-operand instructions such as SHLD: it implies that the
  1091. * first two operands must match in size, but that the third is
  1092. * required to be _unspecified_.
  1093. *
  1094. * IF_SB invokes Size Byte: operands with unspecified size in the
  1095. * template are really bytes, and so no non-byte specification in
  1096. * the input instruction will be tolerated. IF_SW similarly invokes
  1097. * Size Word, and IF_SD invokes Size Doubleword.
  1098. *
  1099. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1100. * that any operand with unspecified size in the template is
  1101. * required to have unspecified size in the instruction too...)
  1102. }
  1103. var
  1104. i,j,asize,oprs : longint;
  1105. siz : array[0..3] of longint;
  1106. begin
  1107. Matches:=100;
  1108. writeln(getstring,'---');
  1109. { Check the opcode and operands }
  1110. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1111. begin
  1112. Matches:=0;
  1113. exit;
  1114. end;
  1115. { Check that no spurious colons or TOs are present }
  1116. for i:=0 to p^.ops-1 do
  1117. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1118. begin
  1119. Matches:=0;
  1120. exit;
  1121. end;
  1122. { Check that the operand flags all match up }
  1123. for i:=0 to p^.ops-1 do
  1124. begin
  1125. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1126. ((p^.optypes[i] and OT_SIZE_MASK) and
  1127. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1128. begin
  1129. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1130. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1131. begin
  1132. Matches:=0;
  1133. exit;
  1134. end
  1135. else
  1136. Matches:=1;
  1137. end;
  1138. end;
  1139. { check postfixes:
  1140. the existance of a certain postfix requires a
  1141. particular code }
  1142. { update condition flags
  1143. or floating point single }
  1144. if (oppostfix=PF_S) and
  1145. not(p^.code[0] in [#$04]) then
  1146. begin
  1147. Matches:=0;
  1148. exit;
  1149. end;
  1150. { floating point size }
  1151. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1152. not(p^.code[0] in []) then
  1153. begin
  1154. Matches:=0;
  1155. exit;
  1156. end;
  1157. { multiple load/store address modes }
  1158. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1159. not(p^.code[0] in [
  1160. // ldr,str,ldrb,strb
  1161. #$17,
  1162. // stm,ldm
  1163. #$26
  1164. ]) then
  1165. begin
  1166. Matches:=0;
  1167. exit;
  1168. end;
  1169. { we shouldn't see any opsize prefixes here }
  1170. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1171. begin
  1172. Matches:=0;
  1173. exit;
  1174. end;
  1175. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1176. begin
  1177. Matches:=0;
  1178. exit;
  1179. end;
  1180. { Check operand sizes }
  1181. { as default an untyped size can get all the sizes, this is different
  1182. from nasm, but else we need to do a lot checking which opcodes want
  1183. size or not with the automatic size generation }
  1184. asize:=longint($ffffffff);
  1185. (*
  1186. if (p^.flags and IF_SB)<>0 then
  1187. asize:=OT_BITS8
  1188. else if (p^.flags and IF_SW)<>0 then
  1189. asize:=OT_BITS16
  1190. else if (p^.flags and IF_SD)<>0 then
  1191. asize:=OT_BITS32;
  1192. if (p^.flags and IF_ARMASK)<>0 then
  1193. begin
  1194. siz[0]:=0;
  1195. siz[1]:=0;
  1196. siz[2]:=0;
  1197. if (p^.flags and IF_AR0)<>0 then
  1198. siz[0]:=asize
  1199. else if (p^.flags and IF_AR1)<>0 then
  1200. siz[1]:=asize
  1201. else if (p^.flags and IF_AR2)<>0 then
  1202. siz[2]:=asize;
  1203. end
  1204. else
  1205. begin
  1206. { we can leave because the size for all operands is forced to be
  1207. the same
  1208. but not if IF_SB IF_SW or IF_SD is set PM }
  1209. if asize=-1 then
  1210. exit;
  1211. siz[0]:=asize;
  1212. siz[1]:=asize;
  1213. siz[2]:=asize;
  1214. end;
  1215. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1216. begin
  1217. if (p^.flags and IF_SM2)<>0 then
  1218. oprs:=2
  1219. else
  1220. oprs:=p^.ops;
  1221. for i:=0 to oprs-1 do
  1222. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1223. begin
  1224. for j:=0 to oprs-1 do
  1225. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1226. break;
  1227. end;
  1228. end
  1229. else
  1230. oprs:=2;
  1231. { Check operand sizes }
  1232. for i:=0 to p^.ops-1 do
  1233. begin
  1234. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1235. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1236. { Immediates can always include smaller size }
  1237. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1238. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1239. Matches:=2;
  1240. end;
  1241. *)
  1242. end;
  1243. function taicpu.calcsize(p:PInsEntry):shortint;
  1244. begin
  1245. result:=4;
  1246. end;
  1247. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1248. begin
  1249. end;
  1250. procedure taicpu.Swapoperands;
  1251. begin
  1252. end;
  1253. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1254. var
  1255. i : longint;
  1256. begin
  1257. result:=false;
  1258. { Things which may only be done once, not when a second pass is done to
  1259. optimize }
  1260. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1261. begin
  1262. { create the .ot fields }
  1263. create_ot(objdata);
  1264. { set the file postion }
  1265. current_filepos:=fileinfo;
  1266. end
  1267. else
  1268. begin
  1269. { we've already an insentry so it's valid }
  1270. result:=true;
  1271. exit;
  1272. end;
  1273. { Lookup opcode in the table }
  1274. InsSize:=-1;
  1275. i:=instabcache^[opcode];
  1276. if i=-1 then
  1277. begin
  1278. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1279. exit;
  1280. end;
  1281. insentry:=@instab[i];
  1282. while (insentry^.opcode=opcode) do
  1283. begin
  1284. if matches(insentry)=100 then
  1285. begin
  1286. result:=true;
  1287. exit;
  1288. end;
  1289. inc(i);
  1290. insentry:=@instab[i];
  1291. end;
  1292. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1293. { No instruction found, set insentry to nil and inssize to -1 }
  1294. insentry:=nil;
  1295. inssize:=-1;
  1296. end;
  1297. procedure taicpu.gencode(objdata:TObjData);
  1298. var
  1299. bytes : dword;
  1300. i_field : byte;
  1301. procedure setshifterop(op : byte);
  1302. begin
  1303. case oper[op]^.typ of
  1304. top_const:
  1305. begin
  1306. i_field:=1;
  1307. bytes:=bytes or (oper[op]^.val and $fff);
  1308. end;
  1309. top_reg:
  1310. begin
  1311. i_field:=0;
  1312. bytes:=bytes or (getsupreg(oper[op]^.reg) shl 16);
  1313. { does a real shifter op follow? }
  1314. if (op+1<=op) and (oper[op+1]^.typ=top_shifterop) then
  1315. begin
  1316. end;
  1317. end;
  1318. else
  1319. internalerror(2005091103);
  1320. end;
  1321. end;
  1322. begin
  1323. bytes:=$0;
  1324. { evaluate and set condition code }
  1325. { condition code allowed? }
  1326. { setup rest of the instruction }
  1327. case insentry^.code[0] of
  1328. #$08:
  1329. begin
  1330. { set instruction code }
  1331. bytes:=bytes or (ord(insentry^.code[1]) shl 26);
  1332. bytes:=bytes or (ord(insentry^.code[2]) shl 21);
  1333. { set destination }
  1334. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  1335. { create shifter op }
  1336. setshifterop(1);
  1337. { set i field }
  1338. bytes:=bytes or (i_field shl 25);
  1339. { set s if necessary }
  1340. if oppostfix=PF_S then
  1341. bytes:=bytes or (1 shl 20);
  1342. end;
  1343. #$ff:
  1344. internalerror(2005091101);
  1345. else
  1346. internalerror(2005091102);
  1347. end;
  1348. { we're finished, write code }
  1349. objdata.writebytes(bytes,sizeof(bytes));
  1350. end;
  1351. end.
  1352. {$ifdef dummy}
  1353. (*
  1354. static void gencode (long segment, long offset, int bits,
  1355. insn *ins, char *codes, long insn_end)
  1356. {
  1357. int has_S_code; /* S - setflag */
  1358. int has_B_code; /* B - setflag */
  1359. int has_T_code; /* T - setflag */
  1360. int has_W_code; /* ! => W flag */
  1361. int has_F_code; /* ^ => S flag */
  1362. int keep;
  1363. unsigned char c;
  1364. unsigned char bytes[4];
  1365. long data, size;
  1366. static int cc_code[] = /* bit pattern of cc */
  1367. { /* order as enum in */
  1368. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1369. 0x0A, 0x0C, 0x08, 0x0D,
  1370. 0x09, 0x0B, 0x04, 0x01,
  1371. 0x05, 0x07, 0x06,
  1372. };
  1373. (*
  1374. #ifdef DEBUG
  1375. static char *CC[] =
  1376. { /* condition code names */
  1377. "AL", "CC", "CS", "EQ",
  1378. "GE", "GT", "HI", "LE",
  1379. "LS", "LT", "MI", "NE",
  1380. "PL", "VC", "VS", "",
  1381. "S"
  1382. };
  1383. *)
  1384. has_S_code = (ins->condition & C_SSETFLAG);
  1385. has_B_code = (ins->condition & C_BSETFLAG);
  1386. has_T_code = (ins->condition & C_TSETFLAG);
  1387. has_W_code = (ins->condition & C_EXSETFLAG);
  1388. has_F_code = (ins->condition & C_FSETFLAG);
  1389. ins->condition = (ins->condition & 0x0F);
  1390. (*
  1391. if (rt_debug)
  1392. {
  1393. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1394. CC[ins->condition & 0x0F]);
  1395. if (has_S_code)
  1396. printf ("S");
  1397. if (has_B_code)
  1398. printf ("B");
  1399. if (has_T_code)
  1400. printf ("T");
  1401. if (has_W_code)
  1402. printf ("!");
  1403. if (has_F_code)
  1404. printf ("^");
  1405. printf ("\n");
  1406. c = *codes;
  1407. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1408. bytes[0] = 0xB;
  1409. bytes[1] = 0xE;
  1410. bytes[2] = 0xE;
  1411. bytes[3] = 0xF;
  1412. }
  1413. *)
  1414. // First condition code in upper nibble
  1415. if (ins->condition < C_NONE)
  1416. {
  1417. c = cc_code[ins->condition] << 4;
  1418. }
  1419. else
  1420. {
  1421. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1422. }
  1423. switch (keep = *codes)
  1424. {
  1425. case 1:
  1426. // B, BL
  1427. ++codes;
  1428. c |= *codes++;
  1429. bytes[0] = c;
  1430. if (ins->oprs[0].segment != segment)
  1431. {
  1432. // fais une relocation
  1433. c = 1;
  1434. data = 0; // Let the linker locate ??
  1435. }
  1436. else
  1437. {
  1438. c = 0;
  1439. data = ins->oprs[0].offset - (offset + 8);
  1440. if (data % 4)
  1441. {
  1442. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1443. }
  1444. }
  1445. if (data >= 0x1000)
  1446. {
  1447. errfunc (ERR_NONFATAL, "too long offset");
  1448. }
  1449. data = data >> 2;
  1450. bytes[1] = (data >> 16) & 0xFF;
  1451. bytes[2] = (data >> 8) & 0xFF;
  1452. bytes[3] = (data ) & 0xFF;
  1453. if (c == 1)
  1454. {
  1455. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1456. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1457. }
  1458. else
  1459. {
  1460. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1461. }
  1462. return;
  1463. case 2:
  1464. // SWI
  1465. ++codes;
  1466. c |= *codes++;
  1467. bytes[0] = c;
  1468. data = ins->oprs[0].offset;
  1469. bytes[1] = (data >> 16) & 0xFF;
  1470. bytes[2] = (data >> 8) & 0xFF;
  1471. bytes[3] = (data) & 0xFF;
  1472. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1473. return;
  1474. case 3:
  1475. // BX
  1476. ++codes;
  1477. c |= *codes++;
  1478. bytes[0] = c;
  1479. bytes[1] = *codes++;
  1480. bytes[2] = *codes++;
  1481. bytes[3] = *codes++;
  1482. c = regval (&ins->oprs[0],1);
  1483. if (c == 15) // PC
  1484. {
  1485. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1486. }
  1487. else if (c > 15)
  1488. {
  1489. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1490. }
  1491. bytes[3] |= (c & 0x0F);
  1492. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1493. return;
  1494. case 4: // AND Rd,Rn,Rm
  1495. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1496. case 6: // AND Rd,Rn,Rm,<shift>imm
  1497. case 7: // AND Rd,Rn,<shift>imm
  1498. ++codes;
  1499. #ifdef DEBUG
  1500. if (rt_debug)
  1501. {
  1502. printf (" decode - '0x%02X'\n", keep);
  1503. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1504. }
  1505. #endif
  1506. bytes[0] = c | *codes;
  1507. ++codes;
  1508. bytes[1] = *codes;
  1509. if (has_S_code)
  1510. bytes[1] |= 0x10;
  1511. c = regval (&ins->oprs[1],1);
  1512. // Rn in low nibble
  1513. bytes[1] |= c;
  1514. // Rd in high nibble
  1515. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1516. if (keep != 7)
  1517. {
  1518. // Rm in low nibble
  1519. bytes[3] = regval (&ins->oprs[2],1);
  1520. }
  1521. // Shifts if any
  1522. if (keep == 5 || keep == 6)
  1523. {
  1524. // Shift in bytes 2 and 3
  1525. if (keep == 5)
  1526. {
  1527. // Rs
  1528. c = regval (&ins->oprs[3],1);
  1529. bytes[2] |= c;
  1530. c = 0x10; // Set bit 4 in byte[3]
  1531. }
  1532. if (keep == 6)
  1533. {
  1534. c = (ins->oprs[3].offset) & 0x1F;
  1535. // #imm
  1536. bytes[2] |= c >> 1;
  1537. if (c & 0x01)
  1538. {
  1539. bytes[3] |= 0x80;
  1540. }
  1541. c = 0; // Clr bit 4 in byte[3]
  1542. }
  1543. // <shift>
  1544. c |= shiftval (&ins->oprs[3]) << 5;
  1545. bytes[3] |= c;
  1546. }
  1547. // reg,reg,imm
  1548. if (keep == 7)
  1549. {
  1550. int shimm;
  1551. shimm = imm_shift (ins->oprs[2].offset);
  1552. if (shimm == -1)
  1553. {
  1554. errfunc (ERR_NONFATAL, "cannot create that constant");
  1555. }
  1556. bytes[3] = shimm & 0xFF;
  1557. bytes[2] |= (shimm & 0xF00) >> 8;
  1558. }
  1559. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1560. return;
  1561. case 8: // MOV Rd,Rm
  1562. case 9: // MOV Rd,Rm,<shift>Rs
  1563. case 0xA: // MOV Rd,Rm,<shift>imm
  1564. case 0xB: // MOV Rd,<shift>imm
  1565. ++codes;
  1566. #ifdef DEBUG
  1567. if (rt_debug)
  1568. {
  1569. printf (" decode - '0x%02X'\n", keep);
  1570. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1571. }
  1572. #endif
  1573. bytes[0] = c | *codes;
  1574. ++codes;
  1575. bytes[1] = *codes;
  1576. if (has_S_code)
  1577. bytes[1] |= 0x10;
  1578. // Rd in high nibble
  1579. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1580. if (keep != 0x0B)
  1581. {
  1582. // Rm in low nibble
  1583. bytes[3] = regval (&ins->oprs[1],1);
  1584. }
  1585. // Shifts if any
  1586. if (keep == 0x09 || keep == 0x0A)
  1587. {
  1588. // Shift in bytes 2 and 3
  1589. if (keep == 0x09)
  1590. {
  1591. // Rs
  1592. c = regval (&ins->oprs[2],1);
  1593. bytes[2] |= c;
  1594. c = 0x10; // Set bit 4 in byte[3]
  1595. }
  1596. if (keep == 0x0A)
  1597. {
  1598. c = (ins->oprs[2].offset) & 0x1F;
  1599. // #imm
  1600. bytes[2] |= c >> 1;
  1601. if (c & 0x01)
  1602. {
  1603. bytes[3] |= 0x80;
  1604. }
  1605. c = 0; // Clr bit 4 in byte[3]
  1606. }
  1607. // <shift>
  1608. c |= shiftval (&ins->oprs[2]) << 5;
  1609. bytes[3] |= c;
  1610. }
  1611. // reg,imm
  1612. if (keep == 0x0B)
  1613. {
  1614. int shimm;
  1615. shimm = imm_shift (ins->oprs[1].offset);
  1616. if (shimm == -1)
  1617. {
  1618. errfunc (ERR_NONFATAL, "cannot create that constant");
  1619. }
  1620. bytes[3] = shimm & 0xFF;
  1621. bytes[2] |= (shimm & 0xF00) >> 8;
  1622. }
  1623. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1624. return;
  1625. case 0xC: // CMP Rn,Rm
  1626. case 0xD: // CMP Rn,Rm,<shift>Rs
  1627. case 0xE: // CMP Rn,Rm,<shift>imm
  1628. case 0xF: // CMP Rn,<shift>imm
  1629. ++codes;
  1630. bytes[0] = c | *codes++;
  1631. bytes[1] = *codes;
  1632. // Implicit S code
  1633. bytes[1] |= 0x10;
  1634. c = regval (&ins->oprs[0],1);
  1635. // Rn in low nibble
  1636. bytes[1] |= c;
  1637. // No destination
  1638. bytes[2] = 0;
  1639. if (keep != 0x0B)
  1640. {
  1641. // Rm in low nibble
  1642. bytes[3] = regval (&ins->oprs[1],1);
  1643. }
  1644. // Shifts if any
  1645. if (keep == 0x0D || keep == 0x0E)
  1646. {
  1647. // Shift in bytes 2 and 3
  1648. if (keep == 0x0D)
  1649. {
  1650. // Rs
  1651. c = regval (&ins->oprs[2],1);
  1652. bytes[2] |= c;
  1653. c = 0x10; // Set bit 4 in byte[3]
  1654. }
  1655. if (keep == 0x0E)
  1656. {
  1657. c = (ins->oprs[2].offset) & 0x1F;
  1658. // #imm
  1659. bytes[2] |= c >> 1;
  1660. if (c & 0x01)
  1661. {
  1662. bytes[3] |= 0x80;
  1663. }
  1664. c = 0; // Clr bit 4 in byte[3]
  1665. }
  1666. // <shift>
  1667. c |= shiftval (&ins->oprs[2]) << 5;
  1668. bytes[3] |= c;
  1669. }
  1670. // reg,imm
  1671. if (keep == 0x0F)
  1672. {
  1673. int shimm;
  1674. shimm = imm_shift (ins->oprs[1].offset);
  1675. if (shimm == -1)
  1676. {
  1677. errfunc (ERR_NONFATAL, "cannot create that constant");
  1678. }
  1679. bytes[3] = shimm & 0xFF;
  1680. bytes[2] |= (shimm & 0xF00) >> 8;
  1681. }
  1682. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1683. return;
  1684. case 0x10: // MRS Rd,<psr>
  1685. ++codes;
  1686. bytes[0] = c | *codes++;
  1687. bytes[1] = *codes++;
  1688. // Rd
  1689. c = regval (&ins->oprs[0],1);
  1690. bytes[2] = c << 4;
  1691. bytes[3] = 0;
  1692. c = ins->oprs[1].basereg;
  1693. if (c == R_CPSR || c == R_SPSR)
  1694. {
  1695. if (c == R_SPSR)
  1696. {
  1697. bytes[1] |= 0x40;
  1698. }
  1699. }
  1700. else
  1701. {
  1702. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1703. }
  1704. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1705. return;
  1706. case 0x11: // MSR <psr>,Rm
  1707. case 0x12: // MSR <psrf>,Rm
  1708. case 0x13: // MSR <psrf>,#expression
  1709. ++codes;
  1710. bytes[0] = c | *codes++;
  1711. bytes[1] = *codes++;
  1712. bytes[2] = *codes;
  1713. if (keep == 0x11 || keep == 0x12)
  1714. {
  1715. // Rm
  1716. c = regval (&ins->oprs[1],1);
  1717. bytes[3] = c;
  1718. }
  1719. else
  1720. {
  1721. int shimm;
  1722. shimm = imm_shift (ins->oprs[1].offset);
  1723. if (shimm == -1)
  1724. {
  1725. errfunc (ERR_NONFATAL, "cannot create that constant");
  1726. }
  1727. bytes[3] = shimm & 0xFF;
  1728. bytes[2] |= (shimm & 0xF00) >> 8;
  1729. }
  1730. c = ins->oprs[0].basereg;
  1731. if ( keep == 0x11)
  1732. {
  1733. if ( c == R_CPSR || c == R_SPSR)
  1734. {
  1735. if ( c== R_SPSR)
  1736. {
  1737. bytes[1] |= 0x40;
  1738. }
  1739. }
  1740. else
  1741. {
  1742. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1743. }
  1744. }
  1745. else
  1746. {
  1747. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  1748. {
  1749. if ( c== R_SPSR_FLG)
  1750. {
  1751. bytes[1] |= 0x40;
  1752. }
  1753. }
  1754. else
  1755. {
  1756. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  1757. }
  1758. }
  1759. break;
  1760. case 0x14: // MUL Rd,Rm,Rs
  1761. case 0x15: // MULA Rd,Rm,Rs,Rn
  1762. ++codes;
  1763. bytes[0] = c | *codes++;
  1764. bytes[1] = *codes++;
  1765. bytes[3] = *codes;
  1766. // Rd
  1767. bytes[1] |= regval (&ins->oprs[0],1);
  1768. if (has_S_code)
  1769. bytes[1] |= 0x10;
  1770. // Rm
  1771. bytes[3] |= regval (&ins->oprs[1],1);
  1772. // Rs
  1773. bytes[2] = regval (&ins->oprs[2],1);
  1774. if (keep == 0x15)
  1775. {
  1776. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  1777. }
  1778. break;
  1779. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  1780. ++codes;
  1781. bytes[0] = c | *codes++;
  1782. bytes[1] = *codes++;
  1783. bytes[3] = *codes;
  1784. // RdHi
  1785. bytes[1] |= regval (&ins->oprs[1],1);
  1786. if (has_S_code)
  1787. bytes[1] |= 0x10;
  1788. // RdLo
  1789. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1790. // Rm
  1791. bytes[3] |= regval (&ins->oprs[2],1);
  1792. // Rs
  1793. bytes[2] |= regval (&ins->oprs[3],1);
  1794. break;
  1795. case 0x17: // LDR Rd, expression
  1796. ++codes;
  1797. bytes[0] = c | *codes++;
  1798. bytes[1] = *codes++;
  1799. // Rd
  1800. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1801. if (has_B_code)
  1802. bytes[1] |= 0x40;
  1803. if (has_T_code)
  1804. {
  1805. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1806. }
  1807. if (has_W_code)
  1808. {
  1809. errfunc (ERR_NONFATAL, "'!' not allowed");
  1810. }
  1811. // Rn - implicit R15
  1812. bytes[1] |= 0xF;
  1813. if (ins->oprs[1].segment != segment)
  1814. {
  1815. errfunc (ERR_NONFATAL, "label not in same segment");
  1816. }
  1817. data = ins->oprs[1].offset - (offset + 8);
  1818. if (data < 0)
  1819. {
  1820. data = -data;
  1821. }
  1822. else
  1823. {
  1824. bytes[1] |= 0x80;
  1825. }
  1826. if (data >= 0x1000)
  1827. {
  1828. errfunc (ERR_NONFATAL, "too long offset");
  1829. }
  1830. bytes[2] |= ((data & 0xF00) >> 8);
  1831. bytes[3] = data & 0xFF;
  1832. break;
  1833. case 0x18: // LDR Rd, [Rn]
  1834. ++codes;
  1835. bytes[0] = c | *codes++;
  1836. bytes[1] = *codes++;
  1837. // Rd
  1838. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1839. if (has_B_code)
  1840. bytes[1] |= 0x40;
  1841. if (has_T_code)
  1842. {
  1843. bytes[1] |= 0x20; // write-back
  1844. }
  1845. else
  1846. {
  1847. bytes[0] |= 0x01; // implicit pre-index mode
  1848. }
  1849. if (has_W_code)
  1850. {
  1851. bytes[1] |= 0x20; // write-back
  1852. }
  1853. // Rn
  1854. c = regval (&ins->oprs[1],1);
  1855. bytes[1] |= c;
  1856. if (c == 0x15) // R15
  1857. data = -8;
  1858. else
  1859. data = 0;
  1860. if (data < 0)
  1861. {
  1862. data = -data;
  1863. }
  1864. else
  1865. {
  1866. bytes[1] |= 0x80;
  1867. }
  1868. bytes[2] |= ((data & 0xF00) >> 8);
  1869. bytes[3] = data & 0xFF;
  1870. break;
  1871. case 0x19: // LDR Rd, [Rn,#expression]
  1872. case 0x20: // LDR Rd, [Rn,Rm]
  1873. case 0x21: // LDR Rd, [Rn,Rm,shift]
  1874. ++codes;
  1875. bytes[0] = c | *codes++;
  1876. bytes[1] = *codes++;
  1877. // Rd
  1878. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1879. if (has_B_code)
  1880. bytes[1] |= 0x40;
  1881. // Rn
  1882. c = regval (&ins->oprs[1],1);
  1883. bytes[1] |= c;
  1884. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1885. {
  1886. bytes[0] |= 0x01; // pre-index mode
  1887. if (has_W_code)
  1888. {
  1889. bytes[1] |= 0x20;
  1890. }
  1891. if (has_T_code)
  1892. {
  1893. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1894. }
  1895. }
  1896. else
  1897. {
  1898. if (has_T_code) // Forced write-back in post-index mode
  1899. {
  1900. bytes[1] |= 0x20;
  1901. }
  1902. if (has_W_code)
  1903. {
  1904. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1905. }
  1906. }
  1907. if (keep == 0x19)
  1908. {
  1909. data = ins->oprs[2].offset;
  1910. if (data < 0)
  1911. {
  1912. data = -data;
  1913. }
  1914. else
  1915. {
  1916. bytes[1] |= 0x80;
  1917. }
  1918. if (data >= 0x1000)
  1919. {
  1920. errfunc (ERR_NONFATAL, "too long offset");
  1921. }
  1922. bytes[2] |= ((data & 0xF00) >> 8);
  1923. bytes[3] = data & 0xFF;
  1924. }
  1925. else
  1926. {
  1927. if (ins->oprs[2].minus == 0)
  1928. {
  1929. bytes[1] |= 0x80;
  1930. }
  1931. c = regval (&ins->oprs[2],1);
  1932. bytes[3] = c;
  1933. if (keep == 0x21)
  1934. {
  1935. c = ins->oprs[3].offset;
  1936. if (c > 0x1F)
  1937. {
  1938. errfunc (ERR_NONFATAL, "too large shiftvalue");
  1939. c = c & 0x1F;
  1940. }
  1941. bytes[2] |= c >> 1;
  1942. if (c & 0x01)
  1943. {
  1944. bytes[3] |= 0x80;
  1945. }
  1946. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  1947. }
  1948. }
  1949. break;
  1950. case 0x22: // LDRH Rd, expression
  1951. ++codes;
  1952. bytes[0] = c | 0x01; // Implicit pre-index
  1953. bytes[1] = *codes++;
  1954. // Rd
  1955. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1956. // Rn - implicit R15
  1957. bytes[1] |= 0xF;
  1958. if (ins->oprs[1].segment != segment)
  1959. {
  1960. errfunc (ERR_NONFATAL, "label not in same segment");
  1961. }
  1962. data = ins->oprs[1].offset - (offset + 8);
  1963. if (data < 0)
  1964. {
  1965. data = -data;
  1966. }
  1967. else
  1968. {
  1969. bytes[1] |= 0x80;
  1970. }
  1971. if (data >= 0x100)
  1972. {
  1973. errfunc (ERR_NONFATAL, "too long offset");
  1974. }
  1975. bytes[3] = *codes++;
  1976. bytes[2] |= ((data & 0xF0) >> 4);
  1977. bytes[3] |= data & 0xF;
  1978. break;
  1979. case 0x23: // LDRH Rd, Rn
  1980. ++codes;
  1981. bytes[0] = c | 0x01; // Implicit pre-index
  1982. bytes[1] = *codes++;
  1983. // Rd
  1984. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1985. // Rn
  1986. c = regval (&ins->oprs[1],1);
  1987. bytes[1] |= c;
  1988. if (c == 0x15) // R15
  1989. data = -8;
  1990. else
  1991. data = 0;
  1992. if (data < 0)
  1993. {
  1994. data = -data;
  1995. }
  1996. else
  1997. {
  1998. bytes[1] |= 0x80;
  1999. }
  2000. if (data >= 0x100)
  2001. {
  2002. errfunc (ERR_NONFATAL, "too long offset");
  2003. }
  2004. bytes[3] = *codes++;
  2005. bytes[2] |= ((data & 0xF0) >> 4);
  2006. bytes[3] |= data & 0xF;
  2007. break;
  2008. case 0x24: // LDRH Rd, Rn, expression
  2009. case 0x25: // LDRH Rd, Rn, Rm
  2010. ++codes;
  2011. bytes[0] = c;
  2012. bytes[1] = *codes++;
  2013. // Rd
  2014. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2015. // Rn
  2016. c = regval (&ins->oprs[1],1);
  2017. bytes[1] |= c;
  2018. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  2019. {
  2020. bytes[0] |= 0x01; // pre-index mode
  2021. if (has_W_code)
  2022. {
  2023. bytes[1] |= 0x20;
  2024. }
  2025. }
  2026. else
  2027. {
  2028. if (has_W_code)
  2029. {
  2030. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  2031. }
  2032. }
  2033. bytes[3] = *codes++;
  2034. if (keep == 0x24)
  2035. {
  2036. data = ins->oprs[2].offset;
  2037. if (data < 0)
  2038. {
  2039. data = -data;
  2040. }
  2041. else
  2042. {
  2043. bytes[1] |= 0x80;
  2044. }
  2045. if (data >= 0x100)
  2046. {
  2047. errfunc (ERR_NONFATAL, "too long offset");
  2048. }
  2049. bytes[2] |= ((data & 0xF0) >> 4);
  2050. bytes[3] |= data & 0xF;
  2051. }
  2052. else
  2053. {
  2054. if (ins->oprs[2].minus == 0)
  2055. {
  2056. bytes[1] |= 0x80;
  2057. }
  2058. c = regval (&ins->oprs[2],1);
  2059. bytes[3] |= c;
  2060. }
  2061. break;
  2062. case 0x26: // LDM/STM Rn, {reg-list}
  2063. ++codes;
  2064. bytes[0] = c;
  2065. bytes[0] |= ( *codes >> 4) & 0xF;
  2066. bytes[1] = ( *codes << 4) & 0xF0;
  2067. ++codes;
  2068. if (has_W_code)
  2069. {
  2070. bytes[1] |= 0x20;
  2071. }
  2072. if (has_F_code)
  2073. {
  2074. bytes[1] |= 0x40;
  2075. }
  2076. // Rn
  2077. bytes[1] |= regval (&ins->oprs[0],1);
  2078. data = ins->oprs[1].basereg;
  2079. bytes[2] = ((data >> 8) & 0xFF);
  2080. bytes[3] = (data & 0xFF);
  2081. break;
  2082. case 0x27: // SWP Rd, Rm, [Rn]
  2083. ++codes;
  2084. bytes[0] = c;
  2085. bytes[0] |= *codes++;
  2086. bytes[1] = regval (&ins->oprs[2],1);
  2087. if (has_B_code)
  2088. {
  2089. bytes[1] |= 0x40;
  2090. }
  2091. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2092. bytes[3] = *codes++;
  2093. bytes[3] |= regval (&ins->oprs[1],1);
  2094. break;
  2095. default:
  2096. errfunc (ERR_FATAL, "unknown decoding of instruction");
  2097. bytes[0] = c;
  2098. // And a fix nibble
  2099. ++codes;
  2100. bytes[0] |= *codes++;
  2101. if ( *codes == 0x01) // An I bit
  2102. {
  2103. }
  2104. if ( *codes == 0x02) // An I bit
  2105. {
  2106. }
  2107. ++codes;
  2108. }
  2109. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2110. }
  2111. *)
  2112. {$endif dummy
  2113. }