aasmcpu.pas 92 KB

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