aasmcpu.pas 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974
  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. 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_REGF = $00201020; { coproc register }
  74. OT_MEMORY = $00204000; { register number in 'basereg' }
  75. OT_MEM8 = $00204001;
  76. OT_MEM16 = $00204002;
  77. OT_MEM32 = $00204004;
  78. OT_MEM64 = $00204008;
  79. OT_MEM80 = $00204010;
  80. { word/byte load/store }
  81. OT_AM2 = $00010000;
  82. { misc ld/st operations }
  83. OT_AM3 = $00020000;
  84. { multiple ld/st operations }
  85. OT_AM4 = $00040000;
  86. { co proc. ld/st operations }
  87. OT_AM5 = $00080000;
  88. OT_AMMASK = $000f0000;
  89. { IT instruction }
  90. OT_CONDITION = $00100000;
  91. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  92. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  93. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  94. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  95. OT_FPUREG = $01000000; { floating point stack registers }
  96. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  97. { a mask for the following }
  98. OT_MEM_OFFS = $00604000; { special type of EA }
  99. { simple [address] offset }
  100. OT_ONENESS = $00800000; { special type of immediate operand }
  101. { so UNITY == IMMEDIATE | ONENESS }
  102. OT_UNITY = $00802000; { for shift/rotate instructions }
  103. instabentries = {$i armnop.inc}
  104. maxinfolen = 5;
  105. IF_NONE = $00000000;
  106. IF_ARMMASK = $000F0000;
  107. IF_ARM7 = $00070000;
  108. IF_FPMASK = $00F00000;
  109. IF_FPA = $00100000;
  110. { if the instruction can change in a second pass }
  111. IF_PASS2 = longint($80000000);
  112. type
  113. TInsTabCache=array[TasmOp] of longint;
  114. PInsTabCache=^TInsTabCache;
  115. tinsentry = record
  116. opcode : tasmop;
  117. ops : byte;
  118. optypes : array[0..3] of longint;
  119. code : array[0..maxinfolen] of char;
  120. flags : longint;
  121. end;
  122. pinsentry=^tinsentry;
  123. const
  124. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  125. var
  126. InsTabCache : PInsTabCache;
  127. type
  128. taicpu = class(tai_cpu_abstract_sym)
  129. oppostfix : TOpPostfix;
  130. wideformat : boolean;
  131. roundingmode : troundingmode;
  132. procedure loadshifterop(opidx:longint;const so:tshifterop);
  133. procedure loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean=false);
  134. procedure loadconditioncode(opidx:longint;const cond:tasmcond);
  135. procedure loadmodeflags(opidx:longint;const flags:tcpumodeflags);
  136. procedure loadspecialreg(opidx:longint;const areg:tregister; const aflags:tspecialregflags);
  137. constructor op_none(op : tasmop);
  138. constructor op_reg(op : tasmop;_op1 : tregister);
  139. constructor op_ref(op : tasmop;const _op1 : treference);
  140. constructor op_const(op : tasmop;_op1 : longint);
  141. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  142. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  143. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  144. constructor op_regset(op:tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  145. constructor op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  146. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  147. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  148. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  149. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  150. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  151. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  152. { SFM/LFM }
  153. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  154. { ITxxx }
  155. constructor op_cond(op: tasmop; cond: tasmcond);
  156. { CPSxx }
  157. constructor op_modeflags(op: tasmop; flags: tcpumodeflags);
  158. constructor op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
  159. { MSR }
  160. constructor op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  161. { *M*LL }
  162. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  163. { this is for Jmp instructions }
  164. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  165. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  166. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  167. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  168. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  169. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  170. function spilling_get_operation_type(opnr: longint): topertype;override;
  171. { assembler }
  172. public
  173. { the next will reset all instructions that can change in pass 2 }
  174. procedure ResetPass1;override;
  175. procedure ResetPass2;override;
  176. function CheckIfValid:boolean;
  177. function GetString:string;
  178. function Pass1(objdata:TObjData):longint;override;
  179. procedure Pass2(objdata:TObjData);override;
  180. protected
  181. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  182. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  183. procedure ppubuildderefimploper(var o:toper);override;
  184. procedure ppuderefoper(var o:toper);override;
  185. private
  186. { next fields are filled in pass1, so pass2 is faster }
  187. inssize : shortint;
  188. insoffset : longint;
  189. LastInsOffset : longint; { need to be public to be reset }
  190. insentry : PInsEntry;
  191. function InsEnd:longint;
  192. procedure create_ot(objdata:TObjData);
  193. function Matches(p:PInsEntry):longint;
  194. function calcsize(p:PInsEntry):shortint;
  195. procedure gencode(objdata:TObjData);
  196. function NeedAddrPrefix(opidx:byte):boolean;
  197. procedure Swapoperands;
  198. function FindInsentry(objdata:TObjData):boolean;
  199. end;
  200. tai_align = class(tai_align_abstract)
  201. { nothing to add }
  202. end;
  203. tai_thumb_func = class(tai)
  204. constructor create;
  205. end;
  206. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  207. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  208. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  209. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  210. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  211. { inserts pc relative symbols at places where they are reachable
  212. and transforms special instructions to valid instruction encodings }
  213. procedure finalizearmcode(list,listtoinsert : TAsmList);
  214. { inserts .pdata section and dummy function prolog needed for arm-wince exception handling }
  215. procedure InsertPData;
  216. procedure InitAsm;
  217. procedure DoneAsm;
  218. implementation
  219. uses
  220. itcpugas,aoptcpu;
  221. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  222. begin
  223. allocate_oper(opidx+1);
  224. with oper[opidx]^ do
  225. begin
  226. if typ<>top_shifterop then
  227. begin
  228. clearop(opidx);
  229. new(shifterop);
  230. end;
  231. shifterop^:=so;
  232. typ:=top_shifterop;
  233. if assigned(add_reg_instruction_hook) then
  234. add_reg_instruction_hook(self,shifterop^.rs);
  235. end;
  236. end;
  237. procedure taicpu.loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean);
  238. var
  239. i : byte;
  240. begin
  241. allocate_oper(opidx+1);
  242. with oper[opidx]^ do
  243. begin
  244. if typ<>top_regset then
  245. begin
  246. clearop(opidx);
  247. new(regset);
  248. end;
  249. regset^:=s;
  250. regtyp:=regsetregtype;
  251. subreg:=regsetsubregtype;
  252. usermode:=ausermode;
  253. typ:=top_regset;
  254. case regsetregtype of
  255. R_INTREGISTER:
  256. for i:=RS_R0 to RS_R15 do
  257. begin
  258. if assigned(add_reg_instruction_hook) and (i in regset^) then
  259. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,regsetsubregtype));
  260. end;
  261. R_MMREGISTER:
  262. { both RS_S0 and RS_D0 range from 0 to 31 }
  263. for i:=RS_D0 to RS_D31 do
  264. begin
  265. if assigned(add_reg_instruction_hook) and (i in regset^) then
  266. add_reg_instruction_hook(self,newreg(R_MMREGISTER,i,regsetsubregtype));
  267. end;
  268. end;
  269. end;
  270. end;
  271. procedure taicpu.loadconditioncode(opidx:longint;const cond:tasmcond);
  272. begin
  273. allocate_oper(opidx+1);
  274. with oper[opidx]^ do
  275. begin
  276. if typ<>top_conditioncode then
  277. clearop(opidx);
  278. cc:=cond;
  279. typ:=top_conditioncode;
  280. end;
  281. end;
  282. procedure taicpu.loadmodeflags(opidx: longint; const flags: tcpumodeflags);
  283. begin
  284. allocate_oper(opidx+1);
  285. with oper[opidx]^ do
  286. begin
  287. if typ<>top_modeflags then
  288. clearop(opidx);
  289. modeflags:=flags;
  290. typ:=top_modeflags;
  291. end;
  292. end;
  293. procedure taicpu.loadspecialreg(opidx: longint; const areg: tregister; const aflags: tspecialregflags);
  294. begin
  295. allocate_oper(opidx+1);
  296. with oper[opidx]^ do
  297. begin
  298. if typ<>top_specialreg then
  299. clearop(opidx);
  300. specialreg:=areg;
  301. specialflags:=aflags;
  302. typ:=top_specialreg;
  303. end;
  304. end;
  305. {*****************************************************************************
  306. taicpu Constructors
  307. *****************************************************************************}
  308. constructor taicpu.op_none(op : tasmop);
  309. begin
  310. inherited create(op);
  311. end;
  312. { for pld }
  313. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  314. begin
  315. inherited create(op);
  316. ops:=1;
  317. loadref(0,_op1);
  318. end;
  319. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  320. begin
  321. inherited create(op);
  322. ops:=1;
  323. loadreg(0,_op1);
  324. end;
  325. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  326. begin
  327. inherited create(op);
  328. ops:=1;
  329. loadconst(0,aint(_op1));
  330. end;
  331. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  332. begin
  333. inherited create(op);
  334. ops:=2;
  335. loadreg(0,_op1);
  336. loadreg(1,_op2);
  337. end;
  338. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  339. begin
  340. inherited create(op);
  341. ops:=2;
  342. loadreg(0,_op1);
  343. loadconst(1,aint(_op2));
  344. end;
  345. constructor taicpu.op_regset(op: tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  346. begin
  347. inherited create(op);
  348. ops:=1;
  349. loadregset(0,regtype,subreg,_op1);
  350. end;
  351. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  352. begin
  353. inherited create(op);
  354. ops:=2;
  355. loadref(0,_op1);
  356. loadregset(1,regtype,subreg,_op2);
  357. end;
  358. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  359. begin
  360. inherited create(op);
  361. ops:=2;
  362. loadreg(0,_op1);
  363. loadref(1,_op2);
  364. end;
  365. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  366. begin
  367. inherited create(op);
  368. ops:=3;
  369. loadreg(0,_op1);
  370. loadreg(1,_op2);
  371. loadreg(2,_op3);
  372. end;
  373. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  374. begin
  375. inherited create(op);
  376. ops:=4;
  377. loadreg(0,_op1);
  378. loadreg(1,_op2);
  379. loadreg(2,_op3);
  380. loadreg(3,_op4);
  381. end;
  382. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  383. begin
  384. inherited create(op);
  385. ops:=3;
  386. loadreg(0,_op1);
  387. loadreg(1,_op2);
  388. loadconst(2,aint(_op3));
  389. end;
  390. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  391. begin
  392. inherited create(op);
  393. ops:=3;
  394. loadreg(0,_op1);
  395. loadconst(1,_op2);
  396. loadref(2,_op3);
  397. end;
  398. constructor taicpu.op_cond(op: tasmop; cond: tasmcond);
  399. begin
  400. inherited create(op);
  401. ops:=1;
  402. loadconditioncode(0, cond);
  403. end;
  404. constructor taicpu.op_modeflags(op: tasmop; flags: tcpumodeflags);
  405. begin
  406. inherited create(op);
  407. ops := 1;
  408. loadmodeflags(0,flags);
  409. end;
  410. constructor taicpu.op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
  411. begin
  412. inherited create(op);
  413. ops := 2;
  414. loadmodeflags(0,flags);
  415. loadconst(1,a);
  416. end;
  417. constructor taicpu.op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  418. begin
  419. inherited create(op);
  420. ops:=2;
  421. loadspecialreg(0,specialreg,specialregflags);
  422. loadreg(1,_op2);
  423. end;
  424. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  425. begin
  426. inherited create(op);
  427. ops:=3;
  428. loadreg(0,_op1);
  429. loadreg(1,_op2);
  430. loadsymbol(0,_op3,_op3ofs);
  431. end;
  432. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  433. begin
  434. inherited create(op);
  435. ops:=3;
  436. loadreg(0,_op1);
  437. loadreg(1,_op2);
  438. loadref(2,_op3);
  439. end;
  440. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  441. begin
  442. inherited create(op);
  443. ops:=3;
  444. loadreg(0,_op1);
  445. loadreg(1,_op2);
  446. loadshifterop(2,_op3);
  447. end;
  448. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  449. begin
  450. inherited create(op);
  451. ops:=4;
  452. loadreg(0,_op1);
  453. loadreg(1,_op2);
  454. loadreg(2,_op3);
  455. loadshifterop(3,_op4);
  456. end;
  457. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  458. begin
  459. inherited create(op);
  460. condition:=cond;
  461. ops:=1;
  462. loadsymbol(0,_op1,0);
  463. end;
  464. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  465. begin
  466. inherited create(op);
  467. ops:=1;
  468. loadsymbol(0,_op1,0);
  469. end;
  470. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  471. begin
  472. inherited create(op);
  473. ops:=1;
  474. loadsymbol(0,_op1,_op1ofs);
  475. end;
  476. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  477. begin
  478. inherited create(op);
  479. ops:=2;
  480. loadreg(0,_op1);
  481. loadsymbol(1,_op2,_op2ofs);
  482. end;
  483. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  484. begin
  485. inherited create(op);
  486. ops:=2;
  487. loadsymbol(0,_op1,_op1ofs);
  488. loadref(1,_op2);
  489. end;
  490. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  491. begin
  492. { allow the register allocator to remove unnecessary moves }
  493. result:=(
  494. ((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  495. ((opcode=A_MVF) and (regtype = R_FPUREGISTER)) or
  496. ((opcode in [A_FCPYS, A_FCPYD]) and (regtype = R_MMREGISTER))
  497. ) and
  498. (oppostfix in [PF_None,PF_D]) and
  499. (condition=C_None) and
  500. (ops=2) and
  501. (oper[0]^.typ=top_reg) and
  502. (oper[1]^.typ=top_reg) and
  503. (oper[0]^.reg=oper[1]^.reg);
  504. end;
  505. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  506. var
  507. op: tasmop;
  508. begin
  509. case getregtype(r) of
  510. R_INTREGISTER :
  511. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  512. R_FPUREGISTER :
  513. { use lfm because we don't know the current internal format
  514. and avoid exceptions
  515. }
  516. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  517. R_MMREGISTER :
  518. begin
  519. case getsubreg(r) of
  520. R_SUBFD:
  521. op:=A_FLDD;
  522. R_SUBFS:
  523. op:=A_FLDS;
  524. else
  525. internalerror(2009112905);
  526. end;
  527. result:=taicpu.op_reg_ref(op,r,ref);
  528. end;
  529. else
  530. internalerror(200401041);
  531. end;
  532. end;
  533. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  534. var
  535. op: tasmop;
  536. begin
  537. case getregtype(r) of
  538. R_INTREGISTER :
  539. result:=taicpu.op_reg_ref(A_STR,r,ref);
  540. R_FPUREGISTER :
  541. { use sfm because we don't know the current internal format
  542. and avoid exceptions
  543. }
  544. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  545. R_MMREGISTER :
  546. begin
  547. case getsubreg(r) of
  548. R_SUBFD:
  549. op:=A_FSTD;
  550. R_SUBFS:
  551. op:=A_FSTS;
  552. else
  553. internalerror(2009112904);
  554. end;
  555. result:=taicpu.op_reg_ref(op,r,ref);
  556. end;
  557. else
  558. internalerror(200401041);
  559. end;
  560. end;
  561. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  562. begin
  563. case opcode of
  564. A_ADC,A_ADD,A_AND,A_BIC,
  565. A_EOR,A_CLZ,A_RBIT,
  566. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  567. A_LDRSH,A_LDRT,
  568. A_MOV,A_MVN,A_MLA,A_MUL,
  569. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  570. A_SWP,A_SWPB,
  571. A_LDF,A_FLT,A_FIX,
  572. A_ADF,A_DVF,A_FDV,A_FML,
  573. A_RFS,A_RFC,A_RDF,
  574. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  575. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  576. A_LFM,
  577. A_FLDS,A_FLDD,
  578. A_FMRX,A_FMXR,A_FMSTAT,
  579. A_FMSR,A_FMRS,A_FMDRR,
  580. A_FCPYS,A_FCPYD,A_FCVTSD,A_FCVTDS,
  581. A_FABSS,A_FABSD,A_FSQRTS,A_FSQRTD,A_FMULS,A_FMULD,
  582. A_FADDS,A_FADDD,A_FSUBS,A_FSUBD,A_FDIVS,A_FDIVD,
  583. A_FMACS,A_FMACD,A_FMSCS,A_FMSCD,A_FNMACS,A_FNMACD,
  584. A_FNMSCS,A_FNMSCD,A_FNMULS,A_FNMULD,
  585. A_FMDHR,A_FMRDH,A_FMDLR,A_FMRDL,
  586. A_FNEGS,A_FNEGD,
  587. A_FSITOS,A_FSITOD,A_FTOSIS,A_FTOSID,
  588. A_FTOUIS,A_FTOUID,A_FUITOS,A_FUITOD,
  589. A_SXTB16,A_UXTB16,
  590. A_UXTB,A_UXTH,A_SXTB,A_SXTH:
  591. if opnr=0 then
  592. result:=operand_write
  593. else
  594. result:=operand_read;
  595. A_BKPT,A_B,A_BL,A_BLX,A_BX,
  596. A_CMN,A_CMP,A_TEQ,A_TST,
  597. A_CMF,A_CMFE,A_WFS,A_CNF,
  598. A_FCMPS,A_FCMPD,A_FCMPES,A_FCMPED,A_FCMPEZS,A_FCMPEZD,
  599. A_FCMPZS,A_FCMPZD:
  600. result:=operand_read;
  601. A_SMLAL,A_UMLAL:
  602. if opnr in [0,1] then
  603. result:=operand_readwrite
  604. else
  605. result:=operand_read;
  606. A_SMULL,A_UMULL,
  607. A_FMRRD:
  608. if opnr in [0,1] then
  609. result:=operand_write
  610. else
  611. result:=operand_read;
  612. A_STR,A_STRB,A_STRBT,
  613. A_STRH,A_STRT,A_STF,A_SFM,
  614. A_FSTS,A_FSTD:
  615. { important is what happens with the involved registers }
  616. if opnr=0 then
  617. result := operand_read
  618. else
  619. { check for pre/post indexed }
  620. result := operand_read;
  621. //Thumb2
  622. A_LSL, A_LSR, A_ROR, A_ASR, A_SDIV, A_UDIV, A_MOVW, A_MOVT, A_MLS:
  623. if opnr in [0] then
  624. result:=operand_write
  625. else
  626. result:=operand_read;
  627. A_LDREX:
  628. if opnr in [0] then
  629. result:=operand_write
  630. else
  631. result:=operand_read;
  632. A_STREX:
  633. if opnr in [0,1,2] then
  634. result:=operand_write;
  635. else
  636. internalerror(200403151);
  637. end;
  638. end;
  639. procedure BuildInsTabCache;
  640. var
  641. i : longint;
  642. begin
  643. new(instabcache);
  644. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  645. i:=0;
  646. while (i<InsTabEntries) do
  647. begin
  648. if InsTabCache^[InsTab[i].Opcode]=-1 then
  649. InsTabCache^[InsTab[i].Opcode]:=i;
  650. inc(i);
  651. end;
  652. end;
  653. procedure InitAsm;
  654. begin
  655. if not assigned(instabcache) then
  656. BuildInsTabCache;
  657. end;
  658. procedure DoneAsm;
  659. begin
  660. if assigned(instabcache) then
  661. begin
  662. dispose(instabcache);
  663. instabcache:=nil;
  664. end;
  665. end;
  666. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  667. begin
  668. i.oppostfix:=pf;
  669. result:=i;
  670. end;
  671. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  672. begin
  673. i.roundingmode:=rm;
  674. result:=i;
  675. end;
  676. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  677. begin
  678. i.condition:=c;
  679. result:=i;
  680. end;
  681. Function SimpleGetNextInstruction(Current: tai; Var Next: tai): Boolean;
  682. Begin
  683. Current:=tai(Current.Next);
  684. While Assigned(Current) And (Current.typ In SkipInstr) Do
  685. Current:=tai(Current.Next);
  686. Next:=Current;
  687. If Assigned(Next) And Not(Next.typ In SkipInstr) Then
  688. Result:=True
  689. Else
  690. Begin
  691. Next:=Nil;
  692. Result:=False;
  693. End;
  694. End;
  695. (*
  696. function armconstequal(hp1,hp2: tai): boolean;
  697. begin
  698. result:=false;
  699. if hp1.typ<>hp2.typ then
  700. exit;
  701. case hp1.typ of
  702. tai_const:
  703. result:=
  704. (tai_const(hp2).sym=tai_const(hp).sym) and
  705. (tai_const(hp2).value=tai_const(hp).value) and
  706. (tai(hp2.previous).typ=ait_label);
  707. tai_const:
  708. result:=
  709. (tai_const(hp2).sym=tai_const(hp).sym) and
  710. (tai_const(hp2).value=tai_const(hp).value) and
  711. (tai(hp2.previous).typ=ait_label);
  712. end;
  713. end;
  714. *)
  715. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  716. var
  717. curinspos,
  718. penalty,
  719. lastinspos,
  720. { increased for every data element > 4 bytes inserted }
  721. currentsize,
  722. extradataoffset,
  723. limit: longint;
  724. curop : longint;
  725. curtai : tai;
  726. curdatatai,hp,hp2 : tai;
  727. curdata : TAsmList;
  728. l : tasmlabel;
  729. doinsert,
  730. removeref : boolean;
  731. multiplier : byte;
  732. begin
  733. curdata:=TAsmList.create;
  734. lastinspos:=-1;
  735. curinspos:=0;
  736. extradataoffset:=0;
  737. if current_settings.cputype in cpu_thumb then
  738. begin
  739. multiplier:=2;
  740. limit:=504;
  741. end
  742. else
  743. begin
  744. limit:=1016;
  745. multiplier:=1;
  746. end;
  747. curtai:=tai(list.first);
  748. doinsert:=false;
  749. while assigned(curtai) do
  750. begin
  751. { instruction? }
  752. case curtai.typ of
  753. ait_instruction:
  754. begin
  755. { walk through all operand of the instruction }
  756. for curop:=0 to taicpu(curtai).ops-1 do
  757. begin
  758. { reference? }
  759. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  760. begin
  761. { pc relative symbol? }
  762. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  763. if assigned(curdatatai) and
  764. { move only if we're at the first reference of a label }
  765. not(tai_label(curdatatai).moved) then
  766. begin
  767. tai_label(curdatatai).moved:=true;
  768. { check if symbol already used. }
  769. { if yes, reuse the symbol }
  770. hp:=tai(curdatatai.next);
  771. removeref:=false;
  772. if assigned(hp) then
  773. begin
  774. case hp.typ of
  775. ait_const:
  776. begin
  777. if (tai_const(hp).consttype=aitconst_64bit) then
  778. inc(extradataoffset,multiplier);
  779. end;
  780. ait_comp_64bit,
  781. ait_real_64bit:
  782. begin
  783. inc(extradataoffset,multiplier);
  784. end;
  785. ait_real_80bit:
  786. begin
  787. inc(extradataoffset,2*multiplier);
  788. end;
  789. end;
  790. if (hp.typ=ait_const) then
  791. begin
  792. hp2:=tai(curdata.first);
  793. while assigned(hp2) do
  794. begin
  795. { if armconstequal(hp2,hp) then }
  796. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  797. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  798. then
  799. begin
  800. with taicpu(curtai).oper[curop]^.ref^ do
  801. begin
  802. symboldata:=hp2.previous;
  803. symbol:=tai_label(hp2.previous).labsym;
  804. end;
  805. removeref:=true;
  806. break;
  807. end;
  808. hp2:=tai(hp2.next);
  809. end;
  810. end;
  811. end;
  812. { move or remove symbol reference }
  813. repeat
  814. hp:=tai(curdatatai.next);
  815. listtoinsert.remove(curdatatai);
  816. if removeref then
  817. curdatatai.free
  818. else
  819. curdata.concat(curdatatai);
  820. curdatatai:=hp;
  821. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  822. if lastinspos=-1 then
  823. lastinspos:=curinspos;
  824. end;
  825. end;
  826. end;
  827. inc(curinspos,multiplier);
  828. end;
  829. ait_align:
  830. begin
  831. { code is always 4 byte aligned, so we don't have to take care of .align 2 which would
  832. requires also incrementing curinspos by 1 }
  833. inc(curinspos,(tai_align(curtai).aligntype div 4)*multiplier);
  834. end;
  835. ait_const:
  836. begin
  837. inc(curinspos,multiplier);
  838. if (tai_const(curtai).consttype=aitconst_64bit) then
  839. inc(curinspos,multiplier);
  840. end;
  841. ait_real_32bit:
  842. begin
  843. inc(curinspos,multiplier);
  844. end;
  845. ait_comp_64bit,
  846. ait_real_64bit:
  847. begin
  848. inc(curinspos,2*multiplier);
  849. end;
  850. ait_real_80bit:
  851. begin
  852. inc(curinspos,3*multiplier);
  853. end;
  854. end;
  855. { special case for case jump tables }
  856. if SimpleGetNextInstruction(curtai,hp) and
  857. (tai(hp).typ=ait_instruction) and
  858. (taicpu(hp).opcode=A_LDR) and
  859. (taicpu(hp).oper[0]^.typ=top_reg) and
  860. (taicpu(hp).oper[0]^.reg=NR_PC) then
  861. begin
  862. penalty:=1*multiplier;
  863. hp:=tai(hp.next);
  864. { skip register allocations and comments inserted by the optimizer }
  865. while assigned(hp) and (hp.typ in [ait_comment,ait_regalloc]) do
  866. hp:=tai(hp.next);
  867. while assigned(hp) and (hp.typ=ait_const) do
  868. begin
  869. inc(penalty,multiplier);
  870. hp:=tai(hp.next);
  871. end;
  872. end
  873. else
  874. penalty:=0;
  875. { FLD/FST VFP instructions have a limit of +/- 1024, not 4096 }
  876. if SimpleGetNextInstruction(curtai,hp) and
  877. (tai(hp).typ=ait_instruction) and
  878. ((taicpu(hp).opcode=A_FLDS) or
  879. (taicpu(hp).opcode=A_FLDD)) then
  880. limit:=254;
  881. { don't miss an insert }
  882. doinsert:=doinsert or
  883. (not(curdata.empty) and
  884. (curinspos-lastinspos+penalty+extradataoffset>limit));
  885. { split only at real instructions else the test below fails }
  886. if doinsert and (curtai.typ=ait_instruction) and
  887. (
  888. { don't split loads of pc to lr and the following move }
  889. not(
  890. (taicpu(curtai).opcode=A_MOV) and
  891. (taicpu(curtai).oper[0]^.typ=top_reg) and
  892. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  893. (taicpu(curtai).oper[1]^.typ=top_reg) and
  894. (taicpu(curtai).oper[1]^.reg=NR_PC)
  895. )
  896. ) then
  897. begin
  898. lastinspos:=-1;
  899. extradataoffset:=0;
  900. if current_settings.cputype in cpu_thumb then
  901. limit:=508
  902. else
  903. limit:=1016;
  904. doinsert:=false;
  905. hp:=tai(curtai.next);
  906. current_asmdata.getjumplabel(l);
  907. { align thumb in thumb .text section to 4 bytes }
  908. if not(curdata.empty) and (current_settings.cputype in cpu_thumb) then
  909. curdata.Insert(tai_align.Create(4));
  910. curdata.insert(taicpu.op_sym(A_B,l));
  911. curdata.concat(tai_label.create(l));
  912. list.insertlistafter(curtai,curdata);
  913. curtai:=hp;
  914. end
  915. else
  916. curtai:=tai(curtai.next);
  917. end;
  918. { align thumb in thumb .text section to 4 bytes }
  919. if not(curdata.empty) and (current_settings.cputype in cpu_thumb) then
  920. curdata.Insert(tai_align.Create(4));
  921. list.concatlist(curdata);
  922. curdata.free;
  923. end;
  924. procedure ensurethumb2encodings(list: TAsmList);
  925. var
  926. curtai: tai;
  927. op2reg: TRegister;
  928. begin
  929. { Do Thumb-2 16bit -> 32bit transformations }
  930. curtai:=tai(list.first);
  931. while assigned(curtai) do
  932. begin
  933. case curtai.typ of
  934. ait_instruction:
  935. begin
  936. case taicpu(curtai).opcode of
  937. A_ADD:
  938. begin
  939. { Set wide flag for ADD Rd,Rn,Rm where registers are over R7(high register set) }
  940. if taicpu(curtai).ops = 3 then
  941. begin
  942. if taicpu(curtai).oper[2]^.typ in [top_reg,top_shifterop] then
  943. begin
  944. if taicpu(curtai).oper[2]^.typ = top_reg then
  945. op2reg := taicpu(curtai).oper[2]^.reg
  946. else if taicpu(curtai).oper[2]^.shifterop^.rs <> NR_NO then
  947. op2reg := taicpu(curtai).oper[2]^.shifterop^.rs
  948. else
  949. op2reg := NR_NO;
  950. if op2reg <> NR_NO then
  951. begin
  952. if (taicpu(curtai).oper[0]^.reg >= NR_R8) or
  953. (taicpu(curtai).oper[1]^.reg >= NR_R8) or
  954. (op2reg >= NR_R8) then
  955. begin
  956. taicpu(curtai).wideformat:=true;
  957. { Handle special cases where register rules are violated by optimizer/user }
  958. { if d == 13 || (d == 15 && S == ‘0’) || n == 15 || m IN [13,15] then UNPREDICTABLE; }
  959. { Transform ADD.W Rx, Ry, R13 into ADD.W Rx, R13, Ry }
  960. if (op2reg = NR_R13) and (taicpu(curtai).oper[2]^.typ = top_reg) then
  961. begin
  962. taicpu(curtai).oper[2]^.reg := taicpu(curtai).oper[1]^.reg;
  963. taicpu(curtai).oper[1]^.reg := op2reg;
  964. end;
  965. end;
  966. end;
  967. end;
  968. end;
  969. end;
  970. end;
  971. end;
  972. end;
  973. curtai:=tai(curtai.Next);
  974. end;
  975. end;
  976. function getMergedInstruction(FirstOp,LastOp:TAsmOp;InvertLast:boolean) : TAsmOp;
  977. const
  978. opTable: array[A_IT..A_ITTTT] of string =
  979. ('T','TE','TT','TEE','TTE','TET','TTT',
  980. 'TEEE','TTEE','TETE','TTTE',
  981. 'TEET','TTET','TETT','TTTT');
  982. invertedOpTable: array[A_IT..A_ITTTT] of string =
  983. ('E','ET','EE','ETT','EET','ETE','EEE',
  984. 'ETTT','EETT','ETET','EEET',
  985. 'ETTE','EETE','ETEE','EEEE');
  986. var
  987. resStr : string;
  988. i : TAsmOp;
  989. begin
  990. if InvertLast then
  991. resStr := opTable[FirstOp]+invertedOpTable[LastOp]
  992. else
  993. resStr := opTable[FirstOp]+opTable[LastOp];
  994. if length(resStr) > 4 then
  995. internalerror(2012100805);
  996. for i := low(opTable) to high(opTable) do
  997. if opTable[i] = resStr then
  998. exit(i);
  999. internalerror(2012100806);
  1000. end;
  1001. procedure foldITInstructions(list: TAsmList);
  1002. var
  1003. curtai,hp1 : tai;
  1004. levels,i : LongInt;
  1005. begin
  1006. curtai:=tai(list.First);
  1007. while assigned(curtai) do
  1008. begin
  1009. case curtai.typ of
  1010. ait_instruction:
  1011. if IsIT(taicpu(curtai).opcode) then
  1012. begin
  1013. levels := GetITLevels(taicpu(curtai).opcode);
  1014. if levels < 4 then
  1015. begin
  1016. i:=levels;
  1017. hp1:=tai(curtai.Next);
  1018. while assigned(hp1) and
  1019. (i > 0) do
  1020. begin
  1021. if hp1.typ=ait_instruction then
  1022. begin
  1023. dec(i);
  1024. if (i = 0) and
  1025. mustbelast(hp1) then
  1026. begin
  1027. hp1:=nil;
  1028. break;
  1029. end;
  1030. end;
  1031. hp1:=tai(hp1.Next);
  1032. end;
  1033. if assigned(hp1) then
  1034. begin
  1035. // We are pointing at the first instruction after the IT block
  1036. while assigned(hp1) and
  1037. (hp1.typ<>ait_instruction) do
  1038. hp1:=tai(hp1.Next);
  1039. if assigned(hp1) and
  1040. (hp1.typ=ait_instruction) and
  1041. IsIT(taicpu(hp1).opcode) then
  1042. begin
  1043. if (levels+GetITLevels(taicpu(hp1).opcode) <= 4) and
  1044. ((taicpu(curtai).oper[0]^.cc=taicpu(hp1).oper[0]^.cc) or
  1045. (taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc))) then
  1046. begin
  1047. taicpu(curtai).opcode:=getMergedInstruction(taicpu(curtai).opcode,
  1048. taicpu(hp1).opcode,
  1049. taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc));
  1050. list.Remove(hp1);
  1051. hp1.Free;
  1052. end;
  1053. end;
  1054. end;
  1055. end;
  1056. end;
  1057. end;
  1058. curtai:=tai(curtai.Next);
  1059. end;
  1060. end;
  1061. procedure finalizearmcode(list, listtoinsert: TAsmList);
  1062. begin
  1063. { Do Thumb-2 16bit -> 32bit transformations }
  1064. if current_settings.cputype in cpu_thumb2 then
  1065. begin
  1066. ensurethumb2encodings(list);
  1067. foldITInstructions(list);
  1068. end;
  1069. insertpcrelativedata(list, listtoinsert);
  1070. end;
  1071. procedure InsertPData;
  1072. var
  1073. prolog: TAsmList;
  1074. begin
  1075. prolog:=TAsmList.create;
  1076. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
  1077. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  1078. prolog.concat(Tai_const.Create_32bit(0));
  1079. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  1080. { dummy function }
  1081. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  1082. current_asmdata.asmlists[al_start].insertList(prolog);
  1083. prolog.Free;
  1084. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
  1085. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  1086. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  1087. end;
  1088. (*
  1089. Floating point instruction format information, taken from the linux kernel
  1090. ARM Floating Point Instruction Classes
  1091. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1092. |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
  1093. |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)
  1094. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1095. |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
  1096. |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
  1097. |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
  1098. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1099. CPDT data transfer instructions
  1100. LDF, STF, LFM (copro 2), SFM (copro 2)
  1101. CPDO dyadic arithmetic instructions
  1102. ADF, MUF, SUF, RSF, DVF, RDF,
  1103. POW, RPW, RMF, FML, FDV, FRD, POL
  1104. CPDO monadic arithmetic instructions
  1105. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  1106. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  1107. CPRT joint arithmetic/data transfer instructions
  1108. FIX (arithmetic followed by load/store)
  1109. FLT (load/store followed by arithmetic)
  1110. CMF, CNF CMFE, CNFE (comparisons)
  1111. WFS, RFS (write/read floating point status register)
  1112. WFC, RFC (write/read floating point control register)
  1113. cond condition codes
  1114. P pre/post index bit: 0 = postindex, 1 = preindex
  1115. U up/down bit: 0 = stack grows down, 1 = stack grows up
  1116. W write back bit: 1 = update base register (Rn)
  1117. L load/store bit: 0 = store, 1 = load
  1118. Rn base register
  1119. Rd destination/source register
  1120. Fd floating point destination register
  1121. Fn floating point source register
  1122. Fm floating point source register or floating point constant
  1123. uv transfer length (TABLE 1)
  1124. wx register count (TABLE 2)
  1125. abcd arithmetic opcode (TABLES 3 & 4)
  1126. ef destination size (rounding precision) (TABLE 5)
  1127. gh rounding mode (TABLE 6)
  1128. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  1129. i constant bit: 1 = constant (TABLE 6)
  1130. */
  1131. /*
  1132. TABLE 1
  1133. +-------------------------+---+---+---------+---------+
  1134. | Precision | u | v | FPSR.EP | length |
  1135. +-------------------------+---+---+---------+---------+
  1136. | Single | 0 | 0 | x | 1 words |
  1137. | Double | 1 | 1 | x | 2 words |
  1138. | Extended | 1 | 1 | x | 3 words |
  1139. | Packed decimal | 1 | 1 | 0 | 3 words |
  1140. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  1141. +-------------------------+---+---+---------+---------+
  1142. Note: x = don't care
  1143. */
  1144. /*
  1145. TABLE 2
  1146. +---+---+---------------------------------+
  1147. | w | x | Number of registers to transfer |
  1148. +---+---+---------------------------------+
  1149. | 0 | 1 | 1 |
  1150. | 1 | 0 | 2 |
  1151. | 1 | 1 | 3 |
  1152. | 0 | 0 | 4 |
  1153. +---+---+---------------------------------+
  1154. */
  1155. /*
  1156. TABLE 3: Dyadic Floating Point Opcodes
  1157. +---+---+---+---+----------+-----------------------+-----------------------+
  1158. | a | b | c | d | Mnemonic | Description | Operation |
  1159. +---+---+---+---+----------+-----------------------+-----------------------+
  1160. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  1161. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  1162. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  1163. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  1164. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  1165. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  1166. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  1167. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  1168. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  1169. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  1170. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  1171. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  1172. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  1173. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  1174. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  1175. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  1176. +---+---+---+---+----------+-----------------------+-----------------------+
  1177. Note: POW, RPW, POL are deprecated, and are available for backwards
  1178. compatibility only.
  1179. */
  1180. /*
  1181. TABLE 4: Monadic Floating Point Opcodes
  1182. +---+---+---+---+----------+-----------------------+-----------------------+
  1183. | a | b | c | d | Mnemonic | Description | Operation |
  1184. +---+---+---+---+----------+-----------------------+-----------------------+
  1185. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  1186. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  1187. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  1188. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  1189. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  1190. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  1191. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  1192. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  1193. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  1194. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  1195. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  1196. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  1197. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  1198. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  1199. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  1200. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  1201. +---+---+---+---+----------+-----------------------+-----------------------+
  1202. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  1203. available for backwards compatibility only.
  1204. */
  1205. /*
  1206. TABLE 5
  1207. +-------------------------+---+---+
  1208. | Rounding Precision | e | f |
  1209. +-------------------------+---+---+
  1210. | IEEE Single precision | 0 | 0 |
  1211. | IEEE Double precision | 0 | 1 |
  1212. | IEEE Extended precision | 1 | 0 |
  1213. | undefined (trap) | 1 | 1 |
  1214. +-------------------------+---+---+
  1215. */
  1216. /*
  1217. TABLE 5
  1218. +---------------------------------+---+---+
  1219. | Rounding Mode | g | h |
  1220. +---------------------------------+---+---+
  1221. | Round to nearest (default) | 0 | 0 |
  1222. | Round toward plus infinity | 0 | 1 |
  1223. | Round toward negative infinity | 1 | 0 |
  1224. | Round toward zero | 1 | 1 |
  1225. +---------------------------------+---+---+
  1226. *)
  1227. function taicpu.GetString:string;
  1228. var
  1229. i : longint;
  1230. s : string;
  1231. addsize : boolean;
  1232. begin
  1233. s:='['+gas_op2str[opcode];
  1234. for i:=0 to ops-1 do
  1235. begin
  1236. with oper[i]^ do
  1237. begin
  1238. if i=0 then
  1239. s:=s+' '
  1240. else
  1241. s:=s+',';
  1242. { type }
  1243. addsize:=false;
  1244. if (ot and OT_VREG)=OT_VREG then
  1245. s:=s+'vreg'
  1246. else
  1247. if (ot and OT_FPUREG)=OT_FPUREG then
  1248. s:=s+'fpureg'
  1249. else
  1250. if (ot and OT_REGISTER)=OT_REGISTER then
  1251. begin
  1252. s:=s+'reg';
  1253. addsize:=true;
  1254. end
  1255. else
  1256. if (ot and OT_REGLIST)=OT_REGLIST then
  1257. begin
  1258. s:=s+'reglist';
  1259. addsize:=false;
  1260. end
  1261. else
  1262. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1263. begin
  1264. s:=s+'imm';
  1265. addsize:=true;
  1266. end
  1267. else
  1268. if (ot and OT_MEMORY)=OT_MEMORY then
  1269. begin
  1270. s:=s+'mem';
  1271. addsize:=true;
  1272. if (ot and OT_AM2)<>0 then
  1273. s:=s+' am2 ';
  1274. end
  1275. else
  1276. s:=s+'???';
  1277. { size }
  1278. if addsize then
  1279. begin
  1280. if (ot and OT_BITS8)<>0 then
  1281. s:=s+'8'
  1282. else
  1283. if (ot and OT_BITS16)<>0 then
  1284. s:=s+'24'
  1285. else
  1286. if (ot and OT_BITS32)<>0 then
  1287. s:=s+'32'
  1288. else
  1289. if (ot and OT_BITSSHIFTER)<>0 then
  1290. s:=s+'shifter'
  1291. else
  1292. s:=s+'??';
  1293. { signed }
  1294. if (ot and OT_SIGNED)<>0 then
  1295. s:=s+'s';
  1296. end;
  1297. end;
  1298. end;
  1299. GetString:=s+']';
  1300. end;
  1301. procedure taicpu.ResetPass1;
  1302. begin
  1303. { we need to reset everything here, because the choosen insentry
  1304. can be invalid for a new situation where the previously optimized
  1305. insentry is not correct }
  1306. InsEntry:=nil;
  1307. InsSize:=0;
  1308. LastInsOffset:=-1;
  1309. end;
  1310. procedure taicpu.ResetPass2;
  1311. begin
  1312. { we are here in a second pass, check if the instruction can be optimized }
  1313. if assigned(InsEntry) and
  1314. ((InsEntry^.flags and IF_PASS2)<>0) then
  1315. begin
  1316. InsEntry:=nil;
  1317. InsSize:=0;
  1318. end;
  1319. LastInsOffset:=-1;
  1320. end;
  1321. function taicpu.CheckIfValid:boolean;
  1322. begin
  1323. Result:=False; { unimplemented }
  1324. end;
  1325. function taicpu.Pass1(objdata:TObjData):longint;
  1326. var
  1327. ldr2op : array[PF_B..PF_T] of tasmop = (
  1328. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  1329. str2op : array[PF_B..PF_T] of tasmop = (
  1330. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  1331. begin
  1332. Pass1:=0;
  1333. { Save the old offset and set the new offset }
  1334. InsOffset:=ObjData.CurrObjSec.Size;
  1335. { Error? }
  1336. if (Insentry=nil) and (InsSize=-1) then
  1337. exit;
  1338. { set the file postion }
  1339. current_filepos:=fileinfo;
  1340. { tranlate LDR+postfix to complete opcode }
  1341. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  1342. begin
  1343. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  1344. opcode:=ldr2op[oppostfix]
  1345. else
  1346. internalerror(2005091001);
  1347. if opcode=A_None then
  1348. internalerror(2005091004);
  1349. { postfix has been added to opcode }
  1350. oppostfix:=PF_None;
  1351. end
  1352. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  1353. begin
  1354. if (oppostfix in [low(str2op)..high(str2op)]) then
  1355. opcode:=str2op[oppostfix]
  1356. else
  1357. internalerror(2005091002);
  1358. if opcode=A_None then
  1359. internalerror(2005091003);
  1360. { postfix has been added to opcode }
  1361. oppostfix:=PF_None;
  1362. end;
  1363. { Get InsEntry }
  1364. if FindInsEntry(objdata) then
  1365. begin
  1366. InsSize:=4;
  1367. LastInsOffset:=InsOffset;
  1368. Pass1:=InsSize;
  1369. exit;
  1370. end;
  1371. LastInsOffset:=-1;
  1372. end;
  1373. procedure taicpu.Pass2(objdata:TObjData);
  1374. begin
  1375. { error in pass1 ? }
  1376. if insentry=nil then
  1377. exit;
  1378. current_filepos:=fileinfo;
  1379. { Generate the instruction }
  1380. GenCode(objdata);
  1381. end;
  1382. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1383. begin
  1384. end;
  1385. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1386. begin
  1387. end;
  1388. procedure taicpu.ppubuildderefimploper(var o:toper);
  1389. begin
  1390. end;
  1391. procedure taicpu.ppuderefoper(var o:toper);
  1392. begin
  1393. end;
  1394. function taicpu.InsEnd:longint;
  1395. begin
  1396. Result:=0; { unimplemented }
  1397. end;
  1398. procedure taicpu.create_ot(objdata:TObjData);
  1399. var
  1400. i,l,relsize : longint;
  1401. dummy : byte;
  1402. currsym : TObjSymbol;
  1403. begin
  1404. if ops=0 then
  1405. exit;
  1406. { update oper[].ot field }
  1407. for i:=0 to ops-1 do
  1408. with oper[i]^ do
  1409. begin
  1410. case typ of
  1411. top_regset:
  1412. begin
  1413. ot:=OT_REGLIST;
  1414. end;
  1415. top_reg :
  1416. begin
  1417. case getregtype(reg) of
  1418. R_INTREGISTER:
  1419. ot:=OT_REG32 or OT_SHIFTEROP;
  1420. R_FPUREGISTER:
  1421. ot:=OT_FPUREG;
  1422. else
  1423. internalerror(2005090901);
  1424. end;
  1425. end;
  1426. top_ref :
  1427. begin
  1428. if ref^.refaddr=addr_no then
  1429. begin
  1430. { create ot field }
  1431. { we should get the size here dependend on the
  1432. instruction }
  1433. if (ot and OT_SIZE_MASK)=0 then
  1434. ot:=OT_MEMORY or OT_BITS32
  1435. else
  1436. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1437. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1438. ot:=ot or OT_MEM_OFFS;
  1439. { if we need to fix a reference, we do it here }
  1440. { pc relative addressing }
  1441. if (ref^.base=NR_NO) and
  1442. (ref^.index=NR_NO) and
  1443. (ref^.shiftmode=SM_None)
  1444. { at least we should check if the destination symbol
  1445. is in a text section }
  1446. { and
  1447. (ref^.symbol^.owner="text") } then
  1448. ref^.base:=NR_PC;
  1449. { determine possible address modes }
  1450. if (ref^.base<>NR_NO) and
  1451. (
  1452. (
  1453. (ref^.index=NR_NO) and
  1454. (ref^.shiftmode=SM_None) and
  1455. (ref^.offset>=-4097) and
  1456. (ref^.offset<=4097)
  1457. ) or
  1458. (
  1459. (ref^.shiftmode=SM_None) and
  1460. (ref^.offset=0)
  1461. ) or
  1462. (
  1463. (ref^.index<>NR_NO) and
  1464. (ref^.shiftmode<>SM_None) and
  1465. (ref^.shiftimm<=31) and
  1466. (ref^.offset=0)
  1467. )
  1468. ) then
  1469. ot:=ot or OT_AM2;
  1470. if (ref^.index<>NR_NO) and
  1471. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1472. (
  1473. (ref^.base=NR_NO) and
  1474. (ref^.shiftmode=SM_None) and
  1475. (ref^.offset=0)
  1476. ) then
  1477. ot:=ot or OT_AM4;
  1478. end
  1479. else
  1480. begin
  1481. l:=ref^.offset;
  1482. currsym:=ObjData.symbolref(ref^.symbol);
  1483. if assigned(currsym) then
  1484. inc(l,currsym.address);
  1485. relsize:=(InsOffset+2)-l;
  1486. if (relsize<-33554428) or (relsize>33554428) then
  1487. ot:=OT_IMM32
  1488. else
  1489. ot:=OT_IMM24;
  1490. end;
  1491. end;
  1492. top_local :
  1493. begin
  1494. { we should get the size here dependend on the
  1495. instruction }
  1496. if (ot and OT_SIZE_MASK)=0 then
  1497. ot:=OT_MEMORY or OT_BITS32
  1498. else
  1499. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1500. end;
  1501. top_const :
  1502. begin
  1503. ot:=OT_IMMEDIATE;
  1504. if is_shifter_const(val,dummy) then
  1505. ot:=OT_IMMSHIFTER
  1506. else
  1507. ot:=OT_IMM32
  1508. end;
  1509. top_none :
  1510. begin
  1511. { generated when there was an error in the
  1512. assembler reader. It never happends when generating
  1513. assembler }
  1514. end;
  1515. top_shifterop:
  1516. begin
  1517. ot:=OT_SHIFTEROP;
  1518. end;
  1519. else
  1520. internalerror(200402261);
  1521. end;
  1522. end;
  1523. end;
  1524. function taicpu.Matches(p:PInsEntry):longint;
  1525. { * IF_SM stands for Size Match: any operand whose size is not
  1526. * explicitly specified by the template is `really' intended to be
  1527. * the same size as the first size-specified operand.
  1528. * Non-specification is tolerated in the input instruction, but
  1529. * _wrong_ specification is not.
  1530. *
  1531. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1532. * three-operand instructions such as SHLD: it implies that the
  1533. * first two operands must match in size, but that the third is
  1534. * required to be _unspecified_.
  1535. *
  1536. * IF_SB invokes Size Byte: operands with unspecified size in the
  1537. * template are really bytes, and so no non-byte specification in
  1538. * the input instruction will be tolerated. IF_SW similarly invokes
  1539. * Size Word, and IF_SD invokes Size Doubleword.
  1540. *
  1541. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1542. * that any operand with unspecified size in the template is
  1543. * required to have unspecified size in the instruction too...)
  1544. }
  1545. var
  1546. i{,j,asize,oprs} : longint;
  1547. {siz : array[0..3] of longint;}
  1548. begin
  1549. Matches:=100;
  1550. writeln(getstring,'---');
  1551. { Check the opcode and operands }
  1552. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1553. begin
  1554. Matches:=0;
  1555. exit;
  1556. end;
  1557. { Check that no spurious colons or TOs are present }
  1558. for i:=0 to p^.ops-1 do
  1559. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1560. begin
  1561. Matches:=0;
  1562. exit;
  1563. end;
  1564. { Check that the operand flags all match up }
  1565. for i:=0 to p^.ops-1 do
  1566. begin
  1567. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1568. ((p^.optypes[i] and OT_SIZE_MASK) and
  1569. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1570. begin
  1571. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1572. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1573. begin
  1574. Matches:=0;
  1575. exit;
  1576. end
  1577. else
  1578. Matches:=1;
  1579. end;
  1580. end;
  1581. { check postfixes:
  1582. the existance of a certain postfix requires a
  1583. particular code }
  1584. { update condition flags
  1585. or floating point single }
  1586. if (oppostfix=PF_S) and
  1587. not(p^.code[0] in [#$04]) then
  1588. begin
  1589. Matches:=0;
  1590. exit;
  1591. end;
  1592. { floating point size }
  1593. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1594. not(p^.code[0] in []) then
  1595. begin
  1596. Matches:=0;
  1597. exit;
  1598. end;
  1599. { multiple load/store address modes }
  1600. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1601. not(p^.code[0] in [
  1602. // ldr,str,ldrb,strb
  1603. #$17,
  1604. // stm,ldm
  1605. #$26
  1606. ]) then
  1607. begin
  1608. Matches:=0;
  1609. exit;
  1610. end;
  1611. { we shouldn't see any opsize prefixes here }
  1612. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1613. begin
  1614. Matches:=0;
  1615. exit;
  1616. end;
  1617. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1618. begin
  1619. Matches:=0;
  1620. exit;
  1621. end;
  1622. { Check operand sizes }
  1623. { as default an untyped size can get all the sizes, this is different
  1624. from nasm, but else we need to do a lot checking which opcodes want
  1625. size or not with the automatic size generation }
  1626. (*
  1627. asize:=longint($ffffffff);
  1628. if (p^.flags and IF_SB)<>0 then
  1629. asize:=OT_BITS8
  1630. else if (p^.flags and IF_SW)<>0 then
  1631. asize:=OT_BITS16
  1632. else if (p^.flags and IF_SD)<>0 then
  1633. asize:=OT_BITS32;
  1634. if (p^.flags and IF_ARMASK)<>0 then
  1635. begin
  1636. siz[0]:=0;
  1637. siz[1]:=0;
  1638. siz[2]:=0;
  1639. if (p^.flags and IF_AR0)<>0 then
  1640. siz[0]:=asize
  1641. else if (p^.flags and IF_AR1)<>0 then
  1642. siz[1]:=asize
  1643. else if (p^.flags and IF_AR2)<>0 then
  1644. siz[2]:=asize;
  1645. end
  1646. else
  1647. begin
  1648. { we can leave because the size for all operands is forced to be
  1649. the same
  1650. but not if IF_SB IF_SW or IF_SD is set PM }
  1651. if asize=-1 then
  1652. exit;
  1653. siz[0]:=asize;
  1654. siz[1]:=asize;
  1655. siz[2]:=asize;
  1656. end;
  1657. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1658. begin
  1659. if (p^.flags and IF_SM2)<>0 then
  1660. oprs:=2
  1661. else
  1662. oprs:=p^.ops;
  1663. for i:=0 to oprs-1 do
  1664. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1665. begin
  1666. for j:=0 to oprs-1 do
  1667. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1668. break;
  1669. end;
  1670. end
  1671. else
  1672. oprs:=2;
  1673. { Check operand sizes }
  1674. for i:=0 to p^.ops-1 do
  1675. begin
  1676. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1677. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1678. { Immediates can always include smaller size }
  1679. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1680. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1681. Matches:=2;
  1682. end;
  1683. *)
  1684. end;
  1685. function taicpu.calcsize(p:PInsEntry):shortint;
  1686. begin
  1687. result:=4;
  1688. end;
  1689. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1690. begin
  1691. Result:=False; { unimplemented }
  1692. end;
  1693. procedure taicpu.Swapoperands;
  1694. begin
  1695. end;
  1696. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1697. var
  1698. i : longint;
  1699. begin
  1700. result:=false;
  1701. { Things which may only be done once, not when a second pass is done to
  1702. optimize }
  1703. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1704. begin
  1705. { create the .ot fields }
  1706. create_ot(objdata);
  1707. { set the file postion }
  1708. current_filepos:=fileinfo;
  1709. end
  1710. else
  1711. begin
  1712. { we've already an insentry so it's valid }
  1713. result:=true;
  1714. exit;
  1715. end;
  1716. { Lookup opcode in the table }
  1717. InsSize:=-1;
  1718. i:=instabcache^[opcode];
  1719. if i=-1 then
  1720. begin
  1721. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1722. exit;
  1723. end;
  1724. insentry:=@instab[i];
  1725. while (insentry^.opcode=opcode) do
  1726. begin
  1727. if matches(insentry)=100 then
  1728. begin
  1729. result:=true;
  1730. exit;
  1731. end;
  1732. inc(i);
  1733. insentry:=@instab[i];
  1734. end;
  1735. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1736. { No instruction found, set insentry to nil and inssize to -1 }
  1737. insentry:=nil;
  1738. inssize:=-1;
  1739. end;
  1740. procedure taicpu.gencode(objdata:TObjData);
  1741. var
  1742. bytes : dword;
  1743. i_field : byte;
  1744. procedure setshifterop(op : byte);
  1745. begin
  1746. case oper[op]^.typ of
  1747. top_const:
  1748. begin
  1749. i_field:=1;
  1750. bytes:=bytes or dword(oper[op]^.val and $fff);
  1751. end;
  1752. top_reg:
  1753. begin
  1754. i_field:=0;
  1755. bytes:=bytes or (getsupreg(oper[op]^.reg) shl 16);
  1756. { does a real shifter op follow? }
  1757. if (op+1<=op) and (oper[op+1]^.typ=top_shifterop) then
  1758. begin
  1759. end;
  1760. end;
  1761. else
  1762. internalerror(2005091103);
  1763. end;
  1764. end;
  1765. begin
  1766. bytes:=$0;
  1767. { evaluate and set condition code }
  1768. { condition code allowed? }
  1769. { setup rest of the instruction }
  1770. case insentry^.code[0] of
  1771. #$08:
  1772. begin
  1773. { set instruction code }
  1774. bytes:=bytes or (ord(insentry^.code[1]) shl 26);
  1775. bytes:=bytes or (ord(insentry^.code[2]) shl 21);
  1776. { set destination }
  1777. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  1778. { create shifter op }
  1779. setshifterop(1);
  1780. { set i field }
  1781. bytes:=bytes or (i_field shl 25);
  1782. { set s if necessary }
  1783. if oppostfix=PF_S then
  1784. bytes:=bytes or (1 shl 20);
  1785. end;
  1786. #$ff:
  1787. internalerror(2005091101);
  1788. else
  1789. internalerror(2005091102);
  1790. end;
  1791. { we're finished, write code }
  1792. objdata.writebytes(bytes,sizeof(bytes));
  1793. end;
  1794. {$ifdef dummy}
  1795. (*
  1796. static void gencode (long segment, long offset, int bits,
  1797. insn *ins, char *codes, long insn_end)
  1798. {
  1799. int has_S_code; /* S - setflag */
  1800. int has_B_code; /* B - setflag */
  1801. int has_T_code; /* T - setflag */
  1802. int has_W_code; /* ! => W flag */
  1803. int has_F_code; /* ^ => S flag */
  1804. int keep;
  1805. unsigned char c;
  1806. unsigned char bytes[4];
  1807. long data, size;
  1808. static int cc_code[] = /* bit pattern of cc */
  1809. { /* order as enum in */
  1810. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1811. 0x0A, 0x0C, 0x08, 0x0D,
  1812. 0x09, 0x0B, 0x04, 0x01,
  1813. 0x05, 0x07, 0x06,
  1814. };
  1815. #ifdef DEBUG
  1816. static char *CC[] =
  1817. { /* condition code names */
  1818. "AL", "CC", "CS", "EQ",
  1819. "GE", "GT", "HI", "LE",
  1820. "LS", "LT", "MI", "NE",
  1821. "PL", "VC", "VS", "",
  1822. "S"
  1823. };
  1824. has_S_code = (ins->condition & C_SSETFLAG);
  1825. has_B_code = (ins->condition & C_BSETFLAG);
  1826. has_T_code = (ins->condition & C_TSETFLAG);
  1827. has_W_code = (ins->condition & C_EXSETFLAG);
  1828. has_F_code = (ins->condition & C_FSETFLAG);
  1829. ins->condition = (ins->condition & 0x0F);
  1830. if (rt_debug)
  1831. {
  1832. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1833. CC[ins->condition & 0x0F]);
  1834. if (has_S_code)
  1835. printf ("S");
  1836. if (has_B_code)
  1837. printf ("B");
  1838. if (has_T_code)
  1839. printf ("T");
  1840. if (has_W_code)
  1841. printf ("!");
  1842. if (has_F_code)
  1843. printf ("^");
  1844. printf ("\n");
  1845. c = *codes;
  1846. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1847. bytes[0] = 0xB;
  1848. bytes[1] = 0xE;
  1849. bytes[2] = 0xE;
  1850. bytes[3] = 0xF;
  1851. }
  1852. // First condition code in upper nibble
  1853. if (ins->condition < C_NONE)
  1854. {
  1855. c = cc_code[ins->condition] << 4;
  1856. }
  1857. else
  1858. {
  1859. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1860. }
  1861. switch (keep = *codes)
  1862. {
  1863. case 1:
  1864. // B, BL
  1865. ++codes;
  1866. c |= *codes++;
  1867. bytes[0] = c;
  1868. if (ins->oprs[0].segment != segment)
  1869. {
  1870. // fais une relocation
  1871. c = 1;
  1872. data = 0; // Let the linker locate ??
  1873. }
  1874. else
  1875. {
  1876. c = 0;
  1877. data = ins->oprs[0].offset - (offset + 8);
  1878. if (data % 4)
  1879. {
  1880. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1881. }
  1882. }
  1883. if (data >= 0x1000)
  1884. {
  1885. errfunc (ERR_NONFATAL, "too long offset");
  1886. }
  1887. data = data >> 2;
  1888. bytes[1] = (data >> 16) & 0xFF;
  1889. bytes[2] = (data >> 8) & 0xFF;
  1890. bytes[3] = (data ) & 0xFF;
  1891. if (c == 1)
  1892. {
  1893. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1894. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1895. }
  1896. else
  1897. {
  1898. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1899. }
  1900. return;
  1901. case 2:
  1902. // SWI
  1903. ++codes;
  1904. c |= *codes++;
  1905. bytes[0] = c;
  1906. data = ins->oprs[0].offset;
  1907. bytes[1] = (data >> 16) & 0xFF;
  1908. bytes[2] = (data >> 8) & 0xFF;
  1909. bytes[3] = (data) & 0xFF;
  1910. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1911. return;
  1912. case 3:
  1913. // BX
  1914. ++codes;
  1915. c |= *codes++;
  1916. bytes[0] = c;
  1917. bytes[1] = *codes++;
  1918. bytes[2] = *codes++;
  1919. bytes[3] = *codes++;
  1920. c = regval (&ins->oprs[0],1);
  1921. if (c == 15) // PC
  1922. {
  1923. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1924. }
  1925. else if (c > 15)
  1926. {
  1927. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1928. }
  1929. bytes[3] |= (c & 0x0F);
  1930. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1931. return;
  1932. case 4: // AND Rd,Rn,Rm
  1933. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1934. case 6: // AND Rd,Rn,Rm,<shift>imm
  1935. case 7: // AND Rd,Rn,<shift>imm
  1936. ++codes;
  1937. #ifdef DEBUG
  1938. if (rt_debug)
  1939. {
  1940. printf (" decode - '0x%02X'\n", keep);
  1941. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1942. }
  1943. #endif
  1944. bytes[0] = c | *codes;
  1945. ++codes;
  1946. bytes[1] = *codes;
  1947. if (has_S_code)
  1948. bytes[1] |= 0x10;
  1949. c = regval (&ins->oprs[1],1);
  1950. // Rn in low nibble
  1951. bytes[1] |= c;
  1952. // Rd in high nibble
  1953. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1954. if (keep != 7)
  1955. {
  1956. // Rm in low nibble
  1957. bytes[3] = regval (&ins->oprs[2],1);
  1958. }
  1959. // Shifts if any
  1960. if (keep == 5 || keep == 6)
  1961. {
  1962. // Shift in bytes 2 and 3
  1963. if (keep == 5)
  1964. {
  1965. // Rs
  1966. c = regval (&ins->oprs[3],1);
  1967. bytes[2] |= c;
  1968. c = 0x10; // Set bit 4 in byte[3]
  1969. }
  1970. if (keep == 6)
  1971. {
  1972. c = (ins->oprs[3].offset) & 0x1F;
  1973. // #imm
  1974. bytes[2] |= c >> 1;
  1975. if (c & 0x01)
  1976. {
  1977. bytes[3] |= 0x80;
  1978. }
  1979. c = 0; // Clr bit 4 in byte[3]
  1980. }
  1981. // <shift>
  1982. c |= shiftval (&ins->oprs[3]) << 5;
  1983. bytes[3] |= c;
  1984. }
  1985. // reg,reg,imm
  1986. if (keep == 7)
  1987. {
  1988. int shimm;
  1989. shimm = imm_shift (ins->oprs[2].offset);
  1990. if (shimm == -1)
  1991. {
  1992. errfunc (ERR_NONFATAL, "cannot create that constant");
  1993. }
  1994. bytes[3] = shimm & 0xFF;
  1995. bytes[2] |= (shimm & 0xF00) >> 8;
  1996. }
  1997. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1998. return;
  1999. case 8: // MOV Rd,Rm
  2000. case 9: // MOV Rd,Rm,<shift>Rs
  2001. case 0xA: // MOV Rd,Rm,<shift>imm
  2002. case 0xB: // MOV Rd,<shift>imm
  2003. ++codes;
  2004. #ifdef DEBUG
  2005. if (rt_debug)
  2006. {
  2007. printf (" decode - '0x%02X'\n", keep);
  2008. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  2009. }
  2010. #endif
  2011. bytes[0] = c | *codes;
  2012. ++codes;
  2013. bytes[1] = *codes;
  2014. if (has_S_code)
  2015. bytes[1] |= 0x10;
  2016. // Rd in high nibble
  2017. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2018. if (keep != 0x0B)
  2019. {
  2020. // Rm in low nibble
  2021. bytes[3] = regval (&ins->oprs[1],1);
  2022. }
  2023. // Shifts if any
  2024. if (keep == 0x09 || keep == 0x0A)
  2025. {
  2026. // Shift in bytes 2 and 3
  2027. if (keep == 0x09)
  2028. {
  2029. // Rs
  2030. c = regval (&ins->oprs[2],1);
  2031. bytes[2] |= c;
  2032. c = 0x10; // Set bit 4 in byte[3]
  2033. }
  2034. if (keep == 0x0A)
  2035. {
  2036. c = (ins->oprs[2].offset) & 0x1F;
  2037. // #imm
  2038. bytes[2] |= c >> 1;
  2039. if (c & 0x01)
  2040. {
  2041. bytes[3] |= 0x80;
  2042. }
  2043. c = 0; // Clr bit 4 in byte[3]
  2044. }
  2045. // <shift>
  2046. c |= shiftval (&ins->oprs[2]) << 5;
  2047. bytes[3] |= c;
  2048. }
  2049. // reg,imm
  2050. if (keep == 0x0B)
  2051. {
  2052. int shimm;
  2053. shimm = imm_shift (ins->oprs[1].offset);
  2054. if (shimm == -1)
  2055. {
  2056. errfunc (ERR_NONFATAL, "cannot create that constant");
  2057. }
  2058. bytes[3] = shimm & 0xFF;
  2059. bytes[2] |= (shimm & 0xF00) >> 8;
  2060. }
  2061. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2062. return;
  2063. case 0xC: // CMP Rn,Rm
  2064. case 0xD: // CMP Rn,Rm,<shift>Rs
  2065. case 0xE: // CMP Rn,Rm,<shift>imm
  2066. case 0xF: // CMP Rn,<shift>imm
  2067. ++codes;
  2068. bytes[0] = c | *codes++;
  2069. bytes[1] = *codes;
  2070. // Implicit S code
  2071. bytes[1] |= 0x10;
  2072. c = regval (&ins->oprs[0],1);
  2073. // Rn in low nibble
  2074. bytes[1] |= c;
  2075. // No destination
  2076. bytes[2] = 0;
  2077. if (keep != 0x0B)
  2078. {
  2079. // Rm in low nibble
  2080. bytes[3] = regval (&ins->oprs[1],1);
  2081. }
  2082. // Shifts if any
  2083. if (keep == 0x0D || keep == 0x0E)
  2084. {
  2085. // Shift in bytes 2 and 3
  2086. if (keep == 0x0D)
  2087. {
  2088. // Rs
  2089. c = regval (&ins->oprs[2],1);
  2090. bytes[2] |= c;
  2091. c = 0x10; // Set bit 4 in byte[3]
  2092. }
  2093. if (keep == 0x0E)
  2094. {
  2095. c = (ins->oprs[2].offset) & 0x1F;
  2096. // #imm
  2097. bytes[2] |= c >> 1;
  2098. if (c & 0x01)
  2099. {
  2100. bytes[3] |= 0x80;
  2101. }
  2102. c = 0; // Clr bit 4 in byte[3]
  2103. }
  2104. // <shift>
  2105. c |= shiftval (&ins->oprs[2]) << 5;
  2106. bytes[3] |= c;
  2107. }
  2108. // reg,imm
  2109. if (keep == 0x0F)
  2110. {
  2111. int shimm;
  2112. shimm = imm_shift (ins->oprs[1].offset);
  2113. if (shimm == -1)
  2114. {
  2115. errfunc (ERR_NONFATAL, "cannot create that constant");
  2116. }
  2117. bytes[3] = shimm & 0xFF;
  2118. bytes[2] |= (shimm & 0xF00) >> 8;
  2119. }
  2120. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2121. return;
  2122. case 0x10: // MRS Rd,<psr>
  2123. ++codes;
  2124. bytes[0] = c | *codes++;
  2125. bytes[1] = *codes++;
  2126. // Rd
  2127. c = regval (&ins->oprs[0],1);
  2128. bytes[2] = c << 4;
  2129. bytes[3] = 0;
  2130. c = ins->oprs[1].basereg;
  2131. if (c == R_CPSR || c == R_SPSR)
  2132. {
  2133. if (c == R_SPSR)
  2134. {
  2135. bytes[1] |= 0x40;
  2136. }
  2137. }
  2138. else
  2139. {
  2140. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  2141. }
  2142. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2143. return;
  2144. case 0x11: // MSR <psr>,Rm
  2145. case 0x12: // MSR <psrf>,Rm
  2146. case 0x13: // MSR <psrf>,#expression
  2147. ++codes;
  2148. bytes[0] = c | *codes++;
  2149. bytes[1] = *codes++;
  2150. bytes[2] = *codes;
  2151. if (keep == 0x11 || keep == 0x12)
  2152. {
  2153. // Rm
  2154. c = regval (&ins->oprs[1],1);
  2155. bytes[3] = c;
  2156. }
  2157. else
  2158. {
  2159. int shimm;
  2160. shimm = imm_shift (ins->oprs[1].offset);
  2161. if (shimm == -1)
  2162. {
  2163. errfunc (ERR_NONFATAL, "cannot create that constant");
  2164. }
  2165. bytes[3] = shimm & 0xFF;
  2166. bytes[2] |= (shimm & 0xF00) >> 8;
  2167. }
  2168. c = ins->oprs[0].basereg;
  2169. if ( keep == 0x11)
  2170. {
  2171. if ( c == R_CPSR || c == R_SPSR)
  2172. {
  2173. if ( c== R_SPSR)
  2174. {
  2175. bytes[1] |= 0x40;
  2176. }
  2177. }
  2178. else
  2179. {
  2180. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  2181. }
  2182. }
  2183. else
  2184. {
  2185. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  2186. {
  2187. if ( c== R_SPSR_FLG)
  2188. {
  2189. bytes[1] |= 0x40;
  2190. }
  2191. }
  2192. else
  2193. {
  2194. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  2195. }
  2196. }
  2197. break;
  2198. case 0x14: // MUL Rd,Rm,Rs
  2199. case 0x15: // MULA Rd,Rm,Rs,Rn
  2200. ++codes;
  2201. bytes[0] = c | *codes++;
  2202. bytes[1] = *codes++;
  2203. bytes[3] = *codes;
  2204. // Rd
  2205. bytes[1] |= regval (&ins->oprs[0],1);
  2206. if (has_S_code)
  2207. bytes[1] |= 0x10;
  2208. // Rm
  2209. bytes[3] |= regval (&ins->oprs[1],1);
  2210. // Rs
  2211. bytes[2] = regval (&ins->oprs[2],1);
  2212. if (keep == 0x15)
  2213. {
  2214. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  2215. }
  2216. break;
  2217. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  2218. ++codes;
  2219. bytes[0] = c | *codes++;
  2220. bytes[1] = *codes++;
  2221. bytes[3] = *codes;
  2222. // RdHi
  2223. bytes[1] |= regval (&ins->oprs[1],1);
  2224. if (has_S_code)
  2225. bytes[1] |= 0x10;
  2226. // RdLo
  2227. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2228. // Rm
  2229. bytes[3] |= regval (&ins->oprs[2],1);
  2230. // Rs
  2231. bytes[2] |= regval (&ins->oprs[3],1);
  2232. break;
  2233. case 0x17: // LDR Rd, expression
  2234. ++codes;
  2235. bytes[0] = c | *codes++;
  2236. bytes[1] = *codes++;
  2237. // Rd
  2238. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2239. if (has_B_code)
  2240. bytes[1] |= 0x40;
  2241. if (has_T_code)
  2242. {
  2243. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  2244. }
  2245. if (has_W_code)
  2246. {
  2247. errfunc (ERR_NONFATAL, "'!' not allowed");
  2248. }
  2249. // Rn - implicit R15
  2250. bytes[1] |= 0xF;
  2251. if (ins->oprs[1].segment != segment)
  2252. {
  2253. errfunc (ERR_NONFATAL, "label not in same segment");
  2254. }
  2255. data = ins->oprs[1].offset - (offset + 8);
  2256. if (data < 0)
  2257. {
  2258. data = -data;
  2259. }
  2260. else
  2261. {
  2262. bytes[1] |= 0x80;
  2263. }
  2264. if (data >= 0x1000)
  2265. {
  2266. errfunc (ERR_NONFATAL, "too long offset");
  2267. }
  2268. bytes[2] |= ((data & 0xF00) >> 8);
  2269. bytes[3] = data & 0xFF;
  2270. break;
  2271. case 0x18: // LDR Rd, [Rn]
  2272. ++codes;
  2273. bytes[0] = c | *codes++;
  2274. bytes[1] = *codes++;
  2275. // Rd
  2276. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2277. if (has_B_code)
  2278. bytes[1] |= 0x40;
  2279. if (has_T_code)
  2280. {
  2281. bytes[1] |= 0x20; // write-back
  2282. }
  2283. else
  2284. {
  2285. bytes[0] |= 0x01; // implicit pre-index mode
  2286. }
  2287. if (has_W_code)
  2288. {
  2289. bytes[1] |= 0x20; // write-back
  2290. }
  2291. // Rn
  2292. c = regval (&ins->oprs[1],1);
  2293. bytes[1] |= c;
  2294. if (c == 0x15) // R15
  2295. data = -8;
  2296. else
  2297. data = 0;
  2298. if (data < 0)
  2299. {
  2300. data = -data;
  2301. }
  2302. else
  2303. {
  2304. bytes[1] |= 0x80;
  2305. }
  2306. bytes[2] |= ((data & 0xF00) >> 8);
  2307. bytes[3] = data & 0xFF;
  2308. break;
  2309. case 0x19: // LDR Rd, [Rn,#expression]
  2310. case 0x20: // LDR Rd, [Rn,Rm]
  2311. case 0x21: // LDR Rd, [Rn,Rm,shift]
  2312. ++codes;
  2313. bytes[0] = c | *codes++;
  2314. bytes[1] = *codes++;
  2315. // Rd
  2316. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2317. if (has_B_code)
  2318. bytes[1] |= 0x40;
  2319. // Rn
  2320. c = regval (&ins->oprs[1],1);
  2321. bytes[1] |= c;
  2322. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  2323. {
  2324. bytes[0] |= 0x01; // pre-index mode
  2325. if (has_W_code)
  2326. {
  2327. bytes[1] |= 0x20;
  2328. }
  2329. if (has_T_code)
  2330. {
  2331. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  2332. }
  2333. }
  2334. else
  2335. {
  2336. if (has_T_code) // Forced write-back in post-index mode
  2337. {
  2338. bytes[1] |= 0x20;
  2339. }
  2340. if (has_W_code)
  2341. {
  2342. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  2343. }
  2344. }
  2345. if (keep == 0x19)
  2346. {
  2347. data = ins->oprs[2].offset;
  2348. if (data < 0)
  2349. {
  2350. data = -data;
  2351. }
  2352. else
  2353. {
  2354. bytes[1] |= 0x80;
  2355. }
  2356. if (data >= 0x1000)
  2357. {
  2358. errfunc (ERR_NONFATAL, "too long offset");
  2359. }
  2360. bytes[2] |= ((data & 0xF00) >> 8);
  2361. bytes[3] = data & 0xFF;
  2362. }
  2363. else
  2364. {
  2365. if (ins->oprs[2].minus == 0)
  2366. {
  2367. bytes[1] |= 0x80;
  2368. }
  2369. c = regval (&ins->oprs[2],1);
  2370. bytes[3] = c;
  2371. if (keep == 0x21)
  2372. {
  2373. c = ins->oprs[3].offset;
  2374. if (c > 0x1F)
  2375. {
  2376. errfunc (ERR_NONFATAL, "too large shiftvalue");
  2377. c = c & 0x1F;
  2378. }
  2379. bytes[2] |= c >> 1;
  2380. if (c & 0x01)
  2381. {
  2382. bytes[3] |= 0x80;
  2383. }
  2384. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  2385. }
  2386. }
  2387. break;
  2388. case 0x22: // LDRH Rd, expression
  2389. ++codes;
  2390. bytes[0] = c | 0x01; // Implicit pre-index
  2391. bytes[1] = *codes++;
  2392. // Rd
  2393. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2394. // Rn - implicit R15
  2395. bytes[1] |= 0xF;
  2396. if (ins->oprs[1].segment != segment)
  2397. {
  2398. errfunc (ERR_NONFATAL, "label not in same segment");
  2399. }
  2400. data = ins->oprs[1].offset - (offset + 8);
  2401. if (data < 0)
  2402. {
  2403. data = -data;
  2404. }
  2405. else
  2406. {
  2407. bytes[1] |= 0x80;
  2408. }
  2409. if (data >= 0x100)
  2410. {
  2411. errfunc (ERR_NONFATAL, "too long offset");
  2412. }
  2413. bytes[3] = *codes++;
  2414. bytes[2] |= ((data & 0xF0) >> 4);
  2415. bytes[3] |= data & 0xF;
  2416. break;
  2417. case 0x23: // LDRH Rd, Rn
  2418. ++codes;
  2419. bytes[0] = c | 0x01; // Implicit pre-index
  2420. bytes[1] = *codes++;
  2421. // Rd
  2422. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2423. // Rn
  2424. c = regval (&ins->oprs[1],1);
  2425. bytes[1] |= c;
  2426. if (c == 0x15) // R15
  2427. data = -8;
  2428. else
  2429. data = 0;
  2430. if (data < 0)
  2431. {
  2432. data = -data;
  2433. }
  2434. else
  2435. {
  2436. bytes[1] |= 0x80;
  2437. }
  2438. if (data >= 0x100)
  2439. {
  2440. errfunc (ERR_NONFATAL, "too long offset");
  2441. }
  2442. bytes[3] = *codes++;
  2443. bytes[2] |= ((data & 0xF0) >> 4);
  2444. bytes[3] |= data & 0xF;
  2445. break;
  2446. case 0x24: // LDRH Rd, Rn, expression
  2447. case 0x25: // LDRH Rd, Rn, Rm
  2448. ++codes;
  2449. bytes[0] = c;
  2450. bytes[1] = *codes++;
  2451. // Rd
  2452. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2453. // Rn
  2454. c = regval (&ins->oprs[1],1);
  2455. bytes[1] |= c;
  2456. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  2457. {
  2458. bytes[0] |= 0x01; // pre-index mode
  2459. if (has_W_code)
  2460. {
  2461. bytes[1] |= 0x20;
  2462. }
  2463. }
  2464. else
  2465. {
  2466. if (has_W_code)
  2467. {
  2468. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  2469. }
  2470. }
  2471. bytes[3] = *codes++;
  2472. if (keep == 0x24)
  2473. {
  2474. data = ins->oprs[2].offset;
  2475. if (data < 0)
  2476. {
  2477. data = -data;
  2478. }
  2479. else
  2480. {
  2481. bytes[1] |= 0x80;
  2482. }
  2483. if (data >= 0x100)
  2484. {
  2485. errfunc (ERR_NONFATAL, "too long offset");
  2486. }
  2487. bytes[2] |= ((data & 0xF0) >> 4);
  2488. bytes[3] |= data & 0xF;
  2489. }
  2490. else
  2491. {
  2492. if (ins->oprs[2].minus == 0)
  2493. {
  2494. bytes[1] |= 0x80;
  2495. }
  2496. c = regval (&ins->oprs[2],1);
  2497. bytes[3] |= c;
  2498. }
  2499. break;
  2500. case 0x26: // LDM/STM Rn, {reg-list}
  2501. ++codes;
  2502. bytes[0] = c;
  2503. bytes[0] |= ( *codes >> 4) & 0xF;
  2504. bytes[1] = ( *codes << 4) & 0xF0;
  2505. ++codes;
  2506. if (has_W_code)
  2507. {
  2508. bytes[1] |= 0x20;
  2509. }
  2510. if (has_F_code)
  2511. {
  2512. bytes[1] |= 0x40;
  2513. }
  2514. // Rn
  2515. bytes[1] |= regval (&ins->oprs[0],1);
  2516. data = ins->oprs[1].basereg;
  2517. bytes[2] = ((data >> 8) & 0xFF);
  2518. bytes[3] = (data & 0xFF);
  2519. break;
  2520. case 0x27: // SWP Rd, Rm, [Rn]
  2521. ++codes;
  2522. bytes[0] = c;
  2523. bytes[0] |= *codes++;
  2524. bytes[1] = regval (&ins->oprs[2],1);
  2525. if (has_B_code)
  2526. {
  2527. bytes[1] |= 0x40;
  2528. }
  2529. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2530. bytes[3] = *codes++;
  2531. bytes[3] |= regval (&ins->oprs[1],1);
  2532. break;
  2533. default:
  2534. errfunc (ERR_FATAL, "unknown decoding of instruction");
  2535. bytes[0] = c;
  2536. // And a fix nibble
  2537. ++codes;
  2538. bytes[0] |= *codes++;
  2539. if ( *codes == 0x01) // An I bit
  2540. {
  2541. }
  2542. if ( *codes == 0x02) // An I bit
  2543. {
  2544. }
  2545. ++codes;
  2546. }
  2547. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2548. }
  2549. *)
  2550. {$endif dummy}
  2551. constructor tai_thumb_func.create;
  2552. begin
  2553. inherited create;
  2554. typ:=ait_thumb_func;
  2555. end;
  2556. begin
  2557. cai_align:=tai_align;
  2558. end.