aasmcpu.pas 85 KB

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