aasmcpu.pas 94 KB

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