ra386int.pas 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629
  1. {
  2. $Id$
  3. Copyright (c) 1997-98 by Carl Eric Codere
  4. Does the parsing process for the intel styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit Ra386int;
  19. {**********************************************************************}
  20. { WARNING }
  21. {**********************************************************************}
  22. { Any modification in the order or removal of terms in the tables }
  23. { in i386.pas and intasmi3.pas will BREAK the code in this unit, }
  24. { unless the appropriate changes are made to this unit. Addition }
  25. { of terms though, will not change the code herein. }
  26. {**********************************************************************}
  27. {--------------------------------------------------------------------}
  28. { LEFT TO DO: }
  29. {--------------------------------------------------------------------}
  30. { o Add support for floating point opcodes. }
  31. { o Handle module overrides also... such as crt.white or }
  32. { crt.delay and local typed constants. }
  33. { o Handle label references }
  34. { o Add support for TP styled segment overrides, when the opcode }
  35. { table will be completed. }
  36. { o Add imul,shld and shrd support with references and CL }
  37. { i386.pas requires to be updated to do this. }
  38. { o Bugfix of ao_imm8s for IMUL. (Currently the 3 operand imul will }
  39. { be considered as invalid because I use ao_imm8 and the table }
  40. { uses ao_imm8s). }
  41. {--------------------------------------------------------------------}
  42. Interface
  43. {$ifdef TP}
  44. {$R-} { needed for the in [] }
  45. {$endif}
  46. uses
  47. globtype,tree,i386;
  48. function assemble: ptree;
  49. const
  50. { this variable is TRUE if the lookup tables have already been setup }
  51. { for fast access. On the first call to assemble the tables are setup }
  52. { and stay set up. }
  53. _asmsorted: boolean = FALSE;
  54. firstreg = R_EAX;
  55. lastreg = R_ST7;
  56. type
  57. tiasmops = array[firstop..lastop] of string[7];
  58. piasmops = ^tiasmops;
  59. var
  60. { sorted tables of opcodes }
  61. iasmops: piasmops;
  62. { uppercased tables of registers }
  63. iasmregs: array[firstreg..lastreg] of string[6];
  64. Implementation
  65. Uses
  66. strings,cobjects,systems,verbose,globals,
  67. files,aasm,types,scanner,hcodegen,
  68. rautils;
  69. type
  70. tinteltoken = (
  71. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_HEXNUM,AS_OCTALNUM,
  72. AS_BINNUM,AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  73. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,AS_INTNUM,
  74. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  75. {------------------ Assembler directives --------------------}
  76. AS_DB,AS_DW,AS_DD,AS_END,
  77. {------------------ Assembler Operators --------------------}
  78. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_NEAR,AS_FAR,
  79. AS_HIGH,AS_LOW,AS_OFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  80. AS_AND,AS_OR,AS_XOR);
  81. tasmkeyword = string[6];
  82. const
  83. { These tokens should be modified accordingly to the modifications }
  84. { in the different enumerations. }
  85. firstdirective = AS_DB;
  86. lastdirective = AS_END;
  87. firstoperator = AS_BYTE;
  88. lastoperator = AS_XOR;
  89. firstsreg = R_CS;
  90. lastsreg = R_SS;
  91. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  92. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  93. _count_asmprefixes = 5;
  94. _count_asmspecialops = 25;
  95. _count_asmoverrides = 3;
  96. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  97. ('DB','DW','DD','END');
  98. { problems with shl,shr,not,and,or and xor, they are }
  99. { context sensitive. }
  100. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  101. 'BYTE','WORD','DWORD','QWORD','TBYTE','NEAR','FAR','HIGH',
  102. 'LOW','OFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  103. 'OR','XOR');
  104. {------------------ Missing opcodes from std list ----------------}
  105. _asmprefixes: array[0.._count_asmprefixes] of tasmkeyword = (
  106. 'REPNE','REPE','REP','REPZ','REPNZ','LOCK');
  107. _asmoverrides: array[0.._count_asmoverrides] of tasmkeyword =
  108. ('SEGCS','SEGDS','SEGES','SEGSS');
  109. _overridetokens: array[0.._count_asmoverrides] of tregister =
  110. (R_CS,R_DS,R_ES,R_SS);
  111. _prefixtokens: array[0.._count_asmprefixes] of tasmop = (
  112. A_REPNE,A_REPE,A_REP,A_REPE,A_REPNE,A_LOCK);
  113. _specialops: array[0.._count_asmspecialops] of tasmkeyword = (
  114. 'CMPSB','CMPSW','CMPSD','INSB','INSW','INSD','OUTSB','OUTSW','OUTSD',
  115. 'SCASB','SCASW','SCASD','STOSB','STOSW','STOSD','MOVSB','MOVSW','MOVSD',
  116. 'LODSB','LODSW','LODSD','LOCK','SEGCS','SEGDS','SEGES','SEGSS');
  117. _specialopstokens: array[0.._count_asmspecialops] of tasmop = (
  118. A_CMPS,A_CMPS,A_CMPS,A_INS,A_INS,A_INS,A_OUTS,A_OUTS,A_OUTS,
  119. A_SCAS,A_SCAS,A_SCAS,A_STOS,A_STOS,A_STOS,A_MOVS,A_MOVS,A_MOVS,
  120. A_LODS,A_LODS,A_LODS,A_LOCK,A_NONE,A_NONE,A_NONE,A_NONE);
  121. {------------------------------------------------------------------}
  122. const
  123. newline = #10;
  124. firsttoken : boolean = TRUE;
  125. operandnum : byte = 0;
  126. var
  127. { context for SHL,SHR,AND,NOT,OR,XOR operators }
  128. { if set to true GetToken will return these }
  129. { as operators, otherwise will return these as }
  130. { opcodes. }
  131. inexpression: boolean;
  132. p : paasmoutput;
  133. actasmtoken: tinteltoken;
  134. actasmpattern: string;
  135. c: char;
  136. Instr: TInstruction;
  137. labellist: TAsmLabelList;
  138. old_exit : pointer;
  139. Procedure SetupTables;
  140. { creates uppercased symbol tables for speed access }
  141. var
  142. i: tasmop;
  143. j: tregister;
  144. Begin
  145. Message(assem_d_creating_lookup_tables);
  146. { opcodes }
  147. new(iasmops);
  148. for i:=firstop to lastop do
  149. iasmops^[i] := upper(int_op2str[i]);
  150. { opcodes }
  151. for j:=firstreg to lastreg do
  152. iasmregs[j] := upper(int_reg2str[j]);
  153. end;
  154. {---------------------------------------------------------------------}
  155. { Routines for the tokenizing }
  156. {---------------------------------------------------------------------}
  157. function is_asmopcode(const s: string):Boolean;
  158. {*********************************************************************}
  159. { FUNCTION is_asmopcode(s: string):Boolean }
  160. { Description: Determines if the s string is a valid opcode }
  161. { if so returns TRUE otherwise returns FALSE. }
  162. {*********************************************************************}
  163. var
  164. i: tasmop;
  165. j: byte;
  166. Begin
  167. is_asmopcode := FALSE;
  168. for i:=firstop to lastop do
  169. begin
  170. if s = iasmops^[i] then
  171. begin
  172. is_asmopcode:=TRUE;
  173. exit;
  174. end;
  175. end;
  176. { not found yet, search for extended opcodes }
  177. for j:=0 to _count_asmspecialops do
  178. Begin
  179. if s = _specialops[j] then
  180. Begin
  181. is_asmopcode:=TRUE;
  182. exit;
  183. end;
  184. end;
  185. end;
  186. Procedure is_asmdirective(const s: string; var token: tinteltoken);
  187. {*********************************************************************}
  188. { FUNCTION is_asmdirective(s: string; var token: tinteltoken):Boolean }
  189. { Description: Determines if the s string is a valid directive }
  190. { (an operator can occur in operand fields, while a directive cannot) }
  191. { if so returns the directive token, otherwise does not change token.}
  192. {*********************************************************************}
  193. var
  194. i:byte;
  195. Begin
  196. for i:=0 to _count_asmdirectives do
  197. begin
  198. if s=_asmdirectives[i] then
  199. begin
  200. token := tinteltoken(longint(firstdirective)+i);
  201. exit;
  202. end;
  203. end;
  204. end;
  205. Procedure is_asmoperator(const s: string; var token: tinteltoken);
  206. {*********************************************************************}
  207. { FUNCTION is_asmoperator(s: string; var token: tinteltoken): Boolean}
  208. { Description: Determines if the s string is a valid operator }
  209. { (an operator can occur in operand fields, while a directive cannot) }
  210. { if so returns the operator token, otherwise does not change token. }
  211. {*********************************************************************}
  212. var
  213. i:longint;
  214. Begin
  215. for i:=0 to _count_asmoperators do
  216. begin
  217. if s=_asmoperators[i] then
  218. begin
  219. token := tinteltoken(longint(firstoperator)+i);
  220. exit;
  221. end;
  222. end;
  223. end;
  224. Procedure is_register(const s: string; var token: tinteltoken);
  225. {*********************************************************************}
  226. { PROCEDURE is_register(s: string; var token: tinteltoken); }
  227. { Description: Determines if the s string is a valid register, if }
  228. { so return token equal to A_REGISTER, otherwise does not change token}
  229. {*********************************************************************}
  230. Var
  231. i: tregister;
  232. Begin
  233. for i:=firstreg to lastreg do
  234. begin
  235. if s=iasmregs[i] then
  236. begin
  237. token := AS_REGISTER;
  238. exit;
  239. end;
  240. end;
  241. end;
  242. Function GetToken: tinteltoken;
  243. {*********************************************************************}
  244. { FUNCTION GetToken: tinteltoken; }
  245. { Description: This routine returns intel assembler tokens and }
  246. { does some minor syntax error checking. }
  247. {*********************************************************************}
  248. var
  249. j: integer;
  250. token: tinteltoken;
  251. forcelabel: boolean;
  252. errorflag : boolean;
  253. begin
  254. errorflag := FALSE;
  255. forcelabel := FALSE;
  256. actasmpattern :='';
  257. {* INIT TOKEN TO NOTHING *}
  258. token := AS_NONE;
  259. { while space and tab , continue scan... }
  260. while (c in [' ',#9]) do
  261. c := current_scanner^.asmgetchar;
  262. { Possiblities for first token in a statement: }
  263. { Local Label, Label, Directive, Prefix or Opcode.... }
  264. if firsttoken and not (c in [newline,#13,'{',';']) then
  265. begin
  266. current_scanner^.gettokenpos;
  267. firsttoken := FALSE;
  268. if c = '@' then
  269. begin
  270. token := AS_LLABEL; { this is a local label }
  271. { Let us point to the next character }
  272. c := current_scanner^.asmgetchar;
  273. end;
  274. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  275. begin
  276. { if there is an at_sign, then this must absolutely be a label }
  277. if c = '@' then forcelabel:=TRUE;
  278. actasmpattern := actasmpattern + c;
  279. c := current_scanner^.asmgetchar;
  280. end;
  281. uppervar(actasmpattern);
  282. if c = ':' then
  283. begin
  284. case token of
  285. AS_NONE: token := AS_LABEL;
  286. AS_LLABEL: ; { do nothing }
  287. end; { end case }
  288. { let us point to the next character }
  289. c := current_scanner^.asmgetchar;
  290. gettoken := token;
  291. exit;
  292. end;
  293. { Are we trying to create an identifier with }
  294. { an at-sign...? }
  295. if forcelabel then
  296. Message(assem_e_none_label_contain_at);
  297. If is_asmopcode(actasmpattern) then
  298. Begin
  299. gettoken := AS_OPCODE;
  300. { check if we are in an expression }
  301. { then continue with asm directives }
  302. if not inexpression then
  303. exit;
  304. end;
  305. is_asmdirective(actasmpattern, token);
  306. if (token <> AS_NONE) then
  307. Begin
  308. gettoken := token;
  309. exit
  310. end
  311. else
  312. begin
  313. gettoken := AS_NONE;
  314. Message1(assem_e_invalid_operand,actasmpattern);
  315. end;
  316. end
  317. else { else firsttoken }
  318. { Here we must handle all possible cases }
  319. begin
  320. case c of
  321. '@': { possiblities : - local label reference , such as in jmp @local1 }
  322. { - @Result, @Code or @Data special variables. }
  323. begin
  324. actasmpattern := c;
  325. c:= current_scanner^.asmgetchar;
  326. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  327. begin
  328. actasmpattern := actasmpattern + c;
  329. c := current_scanner^.asmgetchar;
  330. end;
  331. uppervar(actasmpattern);
  332. gettoken := AS_ID;
  333. exit;
  334. end;
  335. { identifier, register, opcode, prefix or directive }
  336. 'A'..'Z','a'..'z','_': begin
  337. actasmpattern := c;
  338. c:= current_scanner^.asmgetchar;
  339. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  340. begin
  341. actasmpattern := actasmpattern + c;
  342. c := current_scanner^.asmgetchar;
  343. end;
  344. uppervar(actasmpattern);
  345. If is_asmopcode(actasmpattern) then
  346. Begin
  347. gettoken := AS_OPCODE;
  348. { if we are not in a constant }
  349. { expression than this is an }
  350. { opcode. }
  351. if not inexpression then
  352. exit;
  353. end;
  354. is_register(actasmpattern, token);
  355. is_asmoperator(actasmpattern,token);
  356. is_asmdirective(actasmpattern,token);
  357. { if found }
  358. if (token <> AS_NONE) then
  359. begin
  360. gettoken := token;
  361. exit;
  362. end
  363. { this is surely an identifier }
  364. else
  365. token := AS_ID;
  366. gettoken := token;
  367. exit;
  368. end;
  369. { override operator... not supported }
  370. '&': begin
  371. Message(assem_w_override_op_not_supported);
  372. c:=current_scanner^.asmgetchar;
  373. gettoken := AS_NONE;
  374. end;
  375. { string or character }
  376. '''' :
  377. begin
  378. actasmpattern:='';
  379. while true do
  380. begin
  381. if c = '''' then
  382. begin
  383. c:=current_scanner^.asmgetchar;
  384. if c=newline then
  385. begin
  386. Message(scan_f_string_exceeds_line);
  387. break;
  388. end;
  389. repeat
  390. if c=''''then
  391. begin
  392. c:=current_scanner^.asmgetchar;
  393. if c='''' then
  394. begin
  395. actasmpattern:=actasmpattern+'''';
  396. c:=current_scanner^.asmgetchar;
  397. if c=newline then
  398. begin
  399. Message(scan_f_string_exceeds_line);
  400. break;
  401. end;
  402. end
  403. else break;
  404. end
  405. else
  406. begin
  407. actasmpattern:=actasmpattern+c;
  408. c:=current_scanner^.asmgetchar;
  409. if c=newline then
  410. begin
  411. Message(scan_f_string_exceeds_line);
  412. break
  413. end;
  414. end;
  415. until false; { end repeat }
  416. end
  417. else break; { end if }
  418. end; { end while }
  419. token:=AS_STRING;
  420. gettoken := token;
  421. exit;
  422. end;
  423. { string or character }
  424. '"' :
  425. begin
  426. actasmpattern:='';
  427. while true do
  428. begin
  429. if c = '"' then
  430. begin
  431. c:=current_scanner^.asmgetchar;
  432. if c=newline then
  433. begin
  434. Message(scan_f_string_exceeds_line);
  435. break;
  436. end;
  437. repeat
  438. if c='"'then
  439. begin
  440. c:=current_scanner^.asmgetchar;
  441. if c='"' then
  442. begin
  443. actasmpattern:=actasmpattern+'"';
  444. c:=current_scanner^.asmgetchar;
  445. if c=newline then
  446. begin
  447. Message(scan_f_string_exceeds_line);
  448. break;
  449. end;
  450. end
  451. else break;
  452. end
  453. else
  454. begin
  455. actasmpattern:=actasmpattern+c;
  456. c:=current_scanner^.asmgetchar;
  457. if c=newline then
  458. begin
  459. Message(scan_f_string_exceeds_line);
  460. break
  461. end;
  462. end;
  463. until false; { end repeat }
  464. end
  465. else break; { end if }
  466. end; { end while }
  467. token := AS_STRING;
  468. gettoken := token;
  469. exit;
  470. end;
  471. '$' : begin
  472. c:=current_scanner^.asmgetchar;
  473. while c in ['0'..'9','A'..'F','a'..'f'] do
  474. begin
  475. actasmpattern := actasmpattern + c;
  476. c := current_scanner^.asmgetchar;
  477. end;
  478. gettoken := AS_HEXNUM;
  479. exit;
  480. end;
  481. ',' : begin
  482. gettoken := AS_COMMA;
  483. c:=current_scanner^.asmgetchar;
  484. exit;
  485. end;
  486. '[' : begin
  487. gettoken := AS_LBRACKET;
  488. c:=current_scanner^.asmgetchar;
  489. exit;
  490. end;
  491. ']' : begin
  492. gettoken := AS_RBRACKET;
  493. c:=current_scanner^.asmgetchar;
  494. exit;
  495. end;
  496. '(' : begin
  497. gettoken := AS_LPAREN;
  498. c:=current_scanner^.asmgetchar;
  499. exit;
  500. end;
  501. ')' : begin
  502. gettoken := AS_RPAREN;
  503. c:=current_scanner^.asmgetchar;
  504. exit;
  505. end;
  506. ':' : begin
  507. gettoken := AS_COLON;
  508. c:=current_scanner^.asmgetchar;
  509. exit;
  510. end;
  511. '.' : begin
  512. gettoken := AS_DOT;
  513. c:=current_scanner^.asmgetchar;
  514. exit;
  515. end;
  516. '+' : begin
  517. gettoken := AS_PLUS;
  518. c:=current_scanner^.asmgetchar;
  519. exit;
  520. end;
  521. '-' : begin
  522. gettoken := AS_MINUS;
  523. c:=current_scanner^.asmgetchar;
  524. exit;
  525. end;
  526. '*' : begin
  527. gettoken := AS_STAR;
  528. c:=current_scanner^.asmgetchar;
  529. exit;
  530. end;
  531. '/' : begin
  532. gettoken := AS_SLASH;
  533. c:=current_scanner^.asmgetchar;
  534. exit;
  535. end;
  536. '0'..'9': begin
  537. { this flag indicates if there was an error }
  538. { if so, then we use a default value instead.}
  539. errorflag := false;
  540. actasmpattern := c;
  541. c := current_scanner^.asmgetchar;
  542. { Get the possible characters }
  543. while c in ['0'..'9','A'..'F','a'..'f'] do
  544. begin
  545. actasmpattern := actasmpattern + c;
  546. c:= current_scanner^.asmgetchar;
  547. end;
  548. { Get ending character }
  549. uppervar(actasmpattern);
  550. c:=upcase(c);
  551. { possibly a binary number. }
  552. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  553. Begin
  554. { Delete the last binary specifier }
  555. delete(actasmpattern,length(actasmpattern),1);
  556. for j:=1 to length(actasmpattern) do
  557. if not (actasmpattern[j] in ['0','1']) then
  558. begin
  559. Message1(assem_e_error_in_binary_const,actasmpattern);
  560. errorflag := TRUE;
  561. end;
  562. { if error, then suppose a binary value of zero. }
  563. if errorflag then
  564. actasmpattern := '0';
  565. gettoken := AS_BINNUM;
  566. exit;
  567. end
  568. else
  569. Begin
  570. case c of
  571. 'O': Begin
  572. for j:=1 to length(actasmpattern) do
  573. if not (actasmpattern[j] in ['0'..'7']) then
  574. begin
  575. Message1(assem_e_error_in_octal_const,actasmpattern);
  576. errorflag := TRUE;
  577. end;
  578. { if error, then suppose an octal value of zero. }
  579. if errorflag then
  580. actasmpattern := '0';
  581. gettoken := AS_OCTALNUM;
  582. c := current_scanner^.asmgetchar;
  583. exit;
  584. end;
  585. 'H': Begin
  586. for j:=1 to length(actasmpattern) do
  587. if not (actasmpattern[j] in ['0'..'9','A'..'F']) then
  588. begin
  589. Message1(assem_e_error_in_hex_const,actasmpattern);
  590. errorflag := TRUE;
  591. end;
  592. { if error, then suppose an hex value of zero. }
  593. if errorflag then
  594. actasmpattern := '0';
  595. gettoken := AS_HEXNUM;
  596. c := current_scanner^.asmgetchar;
  597. exit;
  598. end;
  599. else { must be an integer number }
  600. begin
  601. for j:=1 to length(actasmpattern) do
  602. if not (actasmpattern[j] in ['0'..'9']) then
  603. begin
  604. Message1(assem_e_error_in_integer_const,actasmpattern);
  605. errorflag := TRUE;
  606. end;
  607. { if error, then suppose an int value of zero. }
  608. if errorflag then
  609. actasmpattern := '0';
  610. gettoken := AS_INTNUM;
  611. exit;
  612. end;
  613. end; { end case }
  614. end; { end if }
  615. end;
  616. ';','{',#13,newline : begin
  617. c:=current_scanner^.asmgetchar;
  618. firsttoken := TRUE;
  619. gettoken:=AS_SEPARATOR;
  620. end;
  621. else
  622. Begin
  623. Message(scan_f_illegal_char);
  624. end;
  625. end; { end case }
  626. end; { end else if }
  627. end;
  628. {---------------------------------------------------------------------}
  629. { Routines for the output }
  630. {---------------------------------------------------------------------}
  631. { returns an appropriate ao_xxxx flag indicating the type }
  632. { of operand. }
  633. function findtype(Var Opr: TOperand): longint;
  634. Begin
  635. With Opr do
  636. Begin
  637. case operandtype of
  638. OPR_REFERENCE: Begin
  639. if assigned(ref.symbol) then
  640. { check if in local label list }
  641. { if so then it is considered }
  642. { as a displacement. }
  643. Begin
  644. if labellist.search(ref.symbol^.name) <> nil then
  645. findtype := ao_disp
  646. else
  647. findtype := ao_mem; { probably a mem ref. }
  648. end
  649. else
  650. findtype := ao_mem;
  651. end;
  652. OPR_CONSTANT: Begin
  653. { check if there is not already a default size }
  654. if opr.size <> S_NO then
  655. Begin
  656. findtype := const_2_type[opr.size];
  657. exit;
  658. end;
  659. if val < $ff then
  660. Begin
  661. findtype := ao_imm8;
  662. opr.size := S_B;
  663. end
  664. else if val < $ffff then
  665. Begin
  666. findtype := ao_imm16;
  667. opr.size := S_W;
  668. end
  669. else
  670. Begin
  671. findtype := ao_imm32;
  672. opr.size := S_L;
  673. end
  674. end;
  675. OPR_REGISTER: Begin
  676. findtype := reg_2_type[reg];
  677. exit;
  678. end;
  679. OPR_SYMBOL: Begin
  680. findtype := ao_jumpabsolute;
  681. end;
  682. OPR_NONE: Begin
  683. findtype := 0;
  684. end;
  685. else
  686. Begin
  687. Message(assem_f_internal_error_in_findtype);
  688. end;
  689. end;
  690. end;
  691. end;
  692. Procedure ConcatLabeledInstr(var instr: TInstruction);
  693. Var Instruc: TAsmOp;
  694. Begin
  695. Instruc := instr.getinstruction;
  696. case Instruc Of
  697. A_JO,A_JNO,A_JB,A_JC,A_JNAE,
  698. A_JNB,A_JNC,A_JAE,A_JE,A_JZ,A_JNE,A_JNZ,A_JBE,A_JNA,A_JNBE,
  699. A_JA,A_JS,A_JNS,A_JP,A_JPE,A_JNP,A_JPO,A_JL,A_JNGE,A_JNL,A_JGE,
  700. A_JLE,A_JNG,A_JNLE,A_JG,A_JCXZ,A_JECXZ,A_LOOP,A_LOOPZ,A_LOOPE,
  701. A_LOOPNZ,A_LOOPNE,A_JMP,A_CALL:
  702. Begin
  703. if instr.numops > 1 then
  704. Message(assem_e_invalid_labeled_opcode)
  705. else if instr.operands[1].operandtype <> OPR_LABINSTR then
  706. Message(assem_e_invalid_labeled_opcode)
  707. else if (instr.operands[1].operandtype = OPR_LABINSTR) and
  708. (instr.numops = 1) then
  709. if assigned(instr.operands[1].hl) then
  710. p^.concat(new(pai386_labeled,op_lab(instruc, instr.operands[1].hl)))
  711. else
  712. Message(assem_f_internal_error_in_findtype);
  713. end;
  714. A_MOV:
  715. Begin
  716. { MOV to rel8 }
  717. end;
  718. else
  719. Message1(assem_e_invalid_operand,'');
  720. end;
  721. end;
  722. Procedure HandleExtend(var instr: TInstruction);
  723. { Handles MOVZX, MOVSX ... }
  724. var
  725. instruc: tasmop;
  726. opsize: topsize;
  727. Begin
  728. instruc:=instr.getinstruction;
  729. { return the old types ..}
  730. { these tokens still point to valid intel strings, }
  731. { but we must convert them to TRUE intel tokens }
  732. Case instruc Of
  733. A_MOVSB,A_MOVSBL,A_MOVSBW,A_MOVSWL: instruc := A_MOVSX;
  734. A_MOVZB,A_MOVZWL: instruc := A_MOVZX;
  735. End;
  736. With instr do
  737. Begin
  738. if operands[1].size = S_B then
  739. Begin
  740. if operands[2].size = S_L then
  741. opsize := S_BL
  742. else
  743. if operands[2].size = S_W then
  744. opsize := S_BW
  745. else
  746. begin
  747. Message(assem_e_invalid_size_movzx);
  748. exit;
  749. end;
  750. end
  751. else
  752. if operands[1].size = S_W then
  753. Begin
  754. if operands[2].size = S_L then
  755. opsize := S_WL
  756. else
  757. begin
  758. Message(assem_e_invalid_size_movzx);
  759. exit;
  760. end;
  761. end
  762. else
  763. begin
  764. Message(assem_e_invalid_size_movzx);
  765. exit;
  766. end;
  767. if operands[1].operandtype = OPR_REGISTER then
  768. Begin
  769. if operands[2].operandtype <> OPR_REGISTER then
  770. Message(assem_e_invalid_opcode)
  771. else
  772. p^.concat(new(pai386,op_reg_reg(instruc,opsize,
  773. operands[1].reg,operands[2].reg)));
  774. end
  775. else
  776. if operands[1].operandtype = OPR_REFERENCE then
  777. Begin
  778. if operands[2].operandtype <> OPR_REGISTER then
  779. Message(assem_e_invalid_opcode)
  780. else
  781. p^.concat(new(pai386,op_ref_reg(instruc,opsize,
  782. newreference(operands[1].ref),operands[2].reg)));
  783. end
  784. end; { end with }
  785. end;
  786. Procedure ConcatOpCode(var instr: TInstruction);
  787. {*********************************************************************}
  788. { First Pass: }
  789. { if instr = Lxxx with a 16bit offset, we emit an error. }
  790. { If the instruction is INS,IN,OUT,OUTS,RCL,ROL,RCR,ROR, }
  791. { SAL,SAR,SHL,SHR,SHLD,SHRD,DIV,IDIV,BT,BTC,BTR,BTS,INT, }
  792. { RET,ENTER,SCAS,CMPS,STOS,LODS,FNSTSW,FSTSW. }
  793. { set up the optypes variables manually, as well as setting }
  794. { operand sizes. }
  795. { Second pass: }
  796. { Check if the combination of opcodes and operands are valid, using }
  797. { the opcode table. }
  798. { Third pass: }
  799. { If there was no error on the 2nd pass , then we check the }
  800. { following: }
  801. { - If this is a 0 operand opcode }
  802. { we verify if it is a string opcode, if so we emit a size also}
  803. { otherwise simply emit the opcode by itself. }
  804. { - If this is a 1 operand opcode, and it is a reference, we make }
  805. { sure that the operand size is valid; we emit the opcode. }
  806. { - If this is a two operand opcode }
  807. { o if the opcode is MOVSX or MOVZX then we handle it specially }
  808. { o we check the operand types (most important combinations): }
  809. { if reg,reg we make sure that both registers are of the }
  810. { same size. }
  811. { if reg,ref or ref,reg we check if the symbol name is }
  812. { assigned, if so a size must be specified and compared }
  813. { to the register size, both must be equal. If there is }
  814. { no symbol name, then we check : }
  815. { if refsize = NO_SIZE then OPCODE_SIZE = regsize }
  816. { else if refsize = regsize then OPCODE_SIZE = regsize}
  817. { else error. }
  818. { if no_error emit the opcode. }
  819. { if ref,const or const,ref if ref does not have any size }
  820. { then error, otherwise emit the opcode. }
  821. { - If this is a three operand opcode: }
  822. { imul,shld,and shrd -> check them manually. }
  823. {*********************************************************************}
  824. var
  825. fits : boolean;
  826. i: longint;
  827. opsize: topsize;
  828. optyp1, optyp2, optyp3: longint;
  829. instruc: tasmop;
  830. Begin
  831. fits := FALSE;
  832. for i:=1 to instr.numops do
  833. Begin
  834. case instr.operands[i].operandtype of
  835. OPR_REGISTER: instr.operands[i].size :=
  836. reg_2_size[instr.operands[i].reg];
  837. end; { end case }
  838. end; { endif }
  839. { setup specific instructions for first pass }
  840. instruc := instr.getinstruction;
  841. Case instruc Of
  842. A_LEA,A_LDS,A_LSS,A_LES,A_LFS,A_LGS:
  843. Begin
  844. if instr.operands[1].size <> S_L then
  845. Begin
  846. Message(assem_e_16bit_base_in_32bit_segment);
  847. exit;
  848. end; { endif }
  849. { In this case the size of the reference is not taken into account! }
  850. instr.operands[2].size := S_NO;
  851. end;
  852. end; { case }
  853. With instr do
  854. Begin
  855. for i:=1 to numops do
  856. Begin
  857. With operands[i] do
  858. Begin
  859. { check for 16-bit bases/indexes and emit an error. }
  860. { we cannot only emit a warning since gas does not }
  861. { accept 16-bit indexes and bases. }
  862. if (operandtype = OPR_REFERENCE) and
  863. ((ref.base <> R_NO) or
  864. (ref.index <> R_NO)) then
  865. Begin
  866. { index or base defined. }
  867. if (ref.base <> R_NO) then
  868. Begin
  869. if not (ref.base in
  870. [R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESI,R_EDI,R_ESP]) then
  871. Message(assem_e_16bit_base_in_32bit_segment);
  872. end;
  873. { index or base defined. }
  874. if (ref.index <> R_NO) then
  875. Begin
  876. if not (ref.index in
  877. [R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESI,R_EDI,R_ESP]) then
  878. Message(assem_e_16bit_index_in_32bit_segment);
  879. end;
  880. end;
  881. { Check for constants without bases/indexes in memory }
  882. { references. }
  883. { Update: allow constant references under Go32v2, to }
  884. { access data in the bios data segmement (JM) }
  885. {$ifndef Go32v2}
  886. if (operandtype = OPR_REFERENCE) and
  887. (ref.base = R_NO) and
  888. (ref.index = R_NO) and
  889. (ref.symbol = nil) and
  890. (ref.offset <> 0) then
  891. Begin
  892. ref.is_immediate := TRUE;
  893. Message(assem_e_const_ref_not_allowed);
  894. end;
  895. {$endif Go32v2}
  896. opinfo := findtype(operands[i]);
  897. end; { end with }
  898. end; {endfor}
  899. { TAKE CARE OF SPECIAL OPCODES, TAKE CARE OF THEM INDIVUALLY. }
  900. { ALL THE REST ARE TAKEN CARE BY OPCODE TABLE AND THIRD PASS. }
  901. Case instruc Of
  902. A_FST:;
  903. A_FILD:;
  904. A_FLD: {A_FLDS,A_FLDL,A_FLDT};
  905. A_FIST: {A_FISTQ,A_FISTS,A_FISTL};
  906. A_FWAIT: FWaitWarning;
  907. A_MOVSX:
  908. Begin
  909. { change the instruction to conform to GAS }
  910. if operands[1].size = S_W then
  911. Begin
  912. addinstr(A_MOVSBW)
  913. end
  914. else
  915. if operands[1].size = S_L then
  916. Begin
  917. if operands[2].size = S_B then
  918. addinstr(A_MOVSBL)
  919. else
  920. addinstr(A_MOVSWL);
  921. end;
  922. instruc := getinstruction; { reload instruction }
  923. end;
  924. A_MOVZX:
  925. Begin
  926. { change the instruction to conform to GAS }
  927. if operands[1].size = S_W then
  928. Begin
  929. addinstr(A_MOVZB)
  930. end
  931. else
  932. if operands[1].size = S_L then
  933. Begin
  934. if operands[2].size = S_B then
  935. addinstr(A_MOVZB)
  936. else
  937. addinstr(A_MOVZWL);
  938. end;
  939. instruc := getinstruction; { reload instruction }
  940. end;
  941. A_BT,A_BTC,A_BTR,A_BTS:
  942. Begin
  943. if numops = 2 then
  944. Begin
  945. if (operands[2].operandtype = OPR_CONSTANT) and
  946. (operands[2].val <= $ff) then
  947. Begin
  948. operands[2].opinfo := ao_imm8;
  949. { no operand size if using constant. }
  950. operands[2].size := S_NO;
  951. fits := TRUE;
  952. end
  953. end
  954. else
  955. Begin
  956. Message(assem_e_invalid_opcode_and_operand);
  957. exit;
  958. end;
  959. end;
  960. A_ENTER:
  961. Begin
  962. if numops =2 then
  963. Begin
  964. if (operands[1].operandtype = OPR_CONSTANT) and
  965. (operands[1].val <= $ffff) then
  966. Begin
  967. operands[1].opinfo := ao_imm16;
  968. end { endif }
  969. end { endif }
  970. else
  971. Begin
  972. Message(assem_e_invalid_opcode_and_operand);
  973. exit;
  974. end
  975. end;
  976. { Handle special opcodes for the opcode }
  977. { table. Set them up correctly. }
  978. A_IN,A_INS:
  979. Begin
  980. if numops =2 then
  981. Begin
  982. if (operands[2].operandtype = OPR_REGISTER) and (operands[2].reg = R_DX)
  983. then
  984. Begin
  985. operands[2].opinfo := ao_inoutportreg;
  986. if (operands[1].operandtype = OPR_REGISTER) and
  987. (operands[1].reg in [R_EAX,R_AX,R_AL]) and
  988. (instruc = A_IN) then
  989. Begin
  990. operands[1].opinfo := ao_acc;
  991. case operands[1].reg of
  992. R_EAX: operands[1].size := S_L;
  993. R_AX: operands[1].size := S_W;
  994. R_AL: operands[1].size := S_B;
  995. end;
  996. end
  997. end
  998. else
  999. if (operands[2].operandtype = OPR_CONSTANT) and
  1000. (operands[2].val <= $ff) and
  1001. (instruc = A_IN) then
  1002. Begin
  1003. operands[2].opinfo := ao_imm8;
  1004. operands[2].size := S_B;
  1005. if (operands[1].operandtype = OPR_REGISTER) and
  1006. (operands[1].reg in [R_EAX,R_AX,R_AL]) and
  1007. (instruc = A_IN) then
  1008. Begin
  1009. operands[1].opinfo := ao_acc;
  1010. end
  1011. end;
  1012. end
  1013. else
  1014. if not ((numops=0) and (instruc=A_INS)) then
  1015. Begin
  1016. Message(assem_e_invalid_opcode_and_operand);
  1017. exit;
  1018. end;
  1019. end;
  1020. A_OUT,A_OUTS:
  1021. Begin
  1022. if numops =2 then
  1023. Begin
  1024. if (operands[1].operandtype = OPR_REGISTER) and
  1025. (operands[1].reg = R_DX)then
  1026. Begin
  1027. operands[1].opinfo := ao_inoutportreg;
  1028. if (operands[2].operandtype = OPR_REGISTER) and
  1029. (operands[2].reg in [R_EAX,R_AX,R_AL]) and
  1030. (instruc = A_OUT) then
  1031. Begin
  1032. operands[2].opinfo := ao_acc;
  1033. fits := TRUE;
  1034. end
  1035. end
  1036. else
  1037. if (operands[1].operandtype = OPR_CONSTANT) and
  1038. (operands[1].val <= $ff) and
  1039. (instruc = A_OUT) then
  1040. Begin
  1041. operands[1].opinfo := ao_imm8;
  1042. operands[1].size := S_B;
  1043. if (operands[2].operandtype = OPR_REGISTER) and
  1044. (operands[2].reg in [R_EAX,R_AX,R_AL]) and
  1045. (instruc = A_OUT) then
  1046. Begin
  1047. operands[2].opinfo := ao_acc;
  1048. case operands[2].reg of
  1049. R_EAX: operands[2].size := S_L;
  1050. R_AX: operands[2].size := S_W;
  1051. R_AL: operands[2].size := S_B;
  1052. end;
  1053. fits := TRUE;
  1054. end
  1055. end;
  1056. end
  1057. else
  1058. if not ((numops=0) and (instruc=A_OUTS)) then
  1059. Begin
  1060. Message(assem_e_invalid_opcode_and_operand);
  1061. exit;
  1062. end;
  1063. end;
  1064. A_RCL,A_RCR,A_ROL,A_ROR,A_SAL,A_SAR,A_SHL,A_SHR:
  1065. Begin
  1066. if numops =2 then
  1067. Begin
  1068. if (operands[2].operandtype = OPR_REGISTER) and
  1069. (operands[2].reg = R_CL) then
  1070. Begin
  1071. operands[2].opinfo := ao_shiftcount
  1072. end
  1073. else
  1074. if (operands[2].operandtype = OPR_CONSTANT) and
  1075. (operands[2].val <= $ff) then
  1076. Begin
  1077. operands[2].opinfo := ao_imm8;
  1078. operands[2].size := S_B;
  1079. end;
  1080. end
  1081. else { if numops = 2 }
  1082. Begin
  1083. Message(assem_e_invalid_opcode_and_operand);
  1084. exit;
  1085. end;
  1086. end;
  1087. A_DIV, A_IDIV:
  1088. Begin
  1089. if (operands[1].operandtype = OPR_REGISTER) and
  1090. (operands[1].reg in [R_AL,R_AX,R_EAX]) then
  1091. operands[1].opinfo := ao_acc;
  1092. end;
  1093. A_FNSTSW,A_FSTSW:
  1094. Begin
  1095. if numops = 1 then
  1096. Begin
  1097. if (operands[1].operandtype = OPR_REGISTER) and
  1098. (operands[1].reg = R_AX) then
  1099. operands[1].opinfo := ao_acc;
  1100. end
  1101. else
  1102. Begin
  1103. Message(assem_e_invalid_opcode_and_operand);
  1104. exit;
  1105. end;
  1106. end;
  1107. A_SHLD,A_SHRD:
  1108. { these instruction are fully parsed individually on pass three }
  1109. { so we just do a summary checking here. }
  1110. Begin
  1111. if numops = 3 then
  1112. Begin
  1113. if (operands[3].operandtype = OPR_CONSTANT) and
  1114. (operands[3].val <= $ff) then
  1115. Begin
  1116. operands[3].opinfo := ao_imm8;
  1117. operands[3].size := S_B;
  1118. end;
  1119. end
  1120. else
  1121. Begin
  1122. Message(assem_e_invalid_opcode_and_operand);
  1123. exit;
  1124. end;
  1125. end;
  1126. A_INT:
  1127. Begin
  1128. if numops = 1 then
  1129. Begin
  1130. if (operands[1].operandtype = OPR_CONSTANT) and
  1131. (operands[1].val <= $ff) then
  1132. operands[1].opinfo := ao_imm8;
  1133. end
  1134. end;
  1135. A_RET:
  1136. Begin
  1137. if numops =1 then
  1138. Begin
  1139. if (operands[1].operandtype = OPR_CONSTANT) and
  1140. (operands[1].val <= $ffff) then
  1141. operands[1].opinfo := ao_imm16;
  1142. end
  1143. end;
  1144. { all string instructions have default memory }
  1145. { location which are ignored. Take care of }
  1146. { those. }
  1147. { Here could be added the code for segment }
  1148. { overrides. }
  1149. A_SCAS,A_CMPS,A_STOS,A_LODS:
  1150. Begin
  1151. if numops =1 then
  1152. Begin
  1153. if (operands[1].operandtype = OPR_REFERENCE) and
  1154. (assigned(operands[1].ref.symbol)) then
  1155. Freemem(operands[1].ref.symbol,length(operands[1].ref.symbol^.name)+1);
  1156. operands[1].operandtype := OPR_NONE;
  1157. numops := 0;
  1158. end;
  1159. end;
  1160. { handle parameter for segment overrides }
  1161. A_XLAT:
  1162. Begin
  1163. { handle special TP syntax case for XLAT }
  1164. { here we accept XLAT, XLATB and XLAT m8 }
  1165. if (numops = 1) or (numops = 0) then
  1166. Begin
  1167. operands[1].operandtype := OPR_NONE;
  1168. numops := 0;
  1169. { always a byte for XLAT }
  1170. instr.stropsize := S_B;
  1171. end;
  1172. end;
  1173. end; { case }
  1174. { we have to start a new case because INS etc are already handled before
  1175. as well (JM) }
  1176. Case instruc Of
  1177. A_INS,A_MOVS,A_OUTS:
  1178. Begin
  1179. if numops =2 then
  1180. Begin
  1181. operands[1].operandtype := OPR_NONE;
  1182. operands[2].operandtype := OPR_NONE;
  1183. numops := 0;
  1184. end;
  1185. end;
  1186. end;
  1187. { swap the destination and source }
  1188. { to put in AT&T style direction }
  1189. { only if there are 2/3 operand }
  1190. { numbers. }
  1191. if (instruc <> A_ENTER) then
  1192. SwapOperands(instr);
  1193. { copy them to local variables }
  1194. { for faster access }
  1195. optyp1:=operands[1].opinfo;
  1196. optyp2:=operands[2].opinfo;
  1197. optyp3:=operands[3].opinfo;
  1198. end; { end with }
  1199. { after reading the operands }
  1200. { search the instruction }
  1201. { setup startvalue from cache }
  1202. if itcache^[instruc]<>-1 then
  1203. i:=itcache^[instruc]
  1204. else
  1205. i:=0;
  1206. { this makes cpu.pp uncompilable, but i think this code should be }
  1207. { inserted in the system unit anyways. }
  1208. if (instruc > lastop_ittable) then
  1209. begin
  1210. Message1(assem_w_opcode_not_in_table,upper(int_op2str[instruc]));
  1211. fits:=true;
  1212. end
  1213. else while not(fits) do
  1214. begin
  1215. { set the instruction cache, if the instruction }
  1216. { occurs the first time }
  1217. if (it[i].i=instruc) and (itcache^[instruc]=-1) then
  1218. itcache^[instruc]:=i;
  1219. if (it[i].i=instruc) and (instr.numops=it[i].ops) then
  1220. begin
  1221. { first fit }
  1222. case instr.numops of
  1223. 0 : begin
  1224. fits:=true;
  1225. break;
  1226. end;
  1227. 1 :
  1228. Begin
  1229. if (optyp1 and it[i].o1)<>0 then
  1230. Begin
  1231. fits:=true;
  1232. break;
  1233. end;
  1234. { I consider sign-extended 8bit value to }
  1235. { be equal to immediate 8bit therefore }
  1236. { convert... }
  1237. if (optyp1 = ao_imm8) then
  1238. Begin
  1239. { check if this is a simple sign extend. }
  1240. if (it[i].o1<>ao_imm8s) then
  1241. Begin
  1242. fits:=true;
  1243. break;
  1244. end;
  1245. end;
  1246. end;
  1247. 2 : if ((optyp1 and it[i].o1)<>0) and
  1248. ((optyp2 and it[i].o2)<>0) then
  1249. Begin
  1250. fits:=true;
  1251. break;
  1252. end
  1253. { if the operands can be swaped }
  1254. { then swap them }
  1255. else if ((it[i].m and af_d)<>0) and
  1256. ((optyp1 and it[i].o2)<>0) and
  1257. ((optyp2 and it[i].o1)<>0) then
  1258. begin
  1259. { swap the destination and source }
  1260. { to put in AT&T style direction }
  1261. { What does this mean !!!! ???????????????????????? }
  1262. { if (output_format in [of_o,of_att]) then }
  1263. { ???????????? }
  1264. { SwapOperands(instr); }
  1265. fits:=true;
  1266. break;
  1267. end;
  1268. 3 : if ((optyp1 and it[i].o1)<>0) and
  1269. ((optyp2 and it[i].o2)<>0) and
  1270. ((optyp3 and it[i].o3)<>0) then
  1271. Begin
  1272. fits:=true;
  1273. break;
  1274. end;
  1275. end; { end case }
  1276. end; { endif }
  1277. if it[i].i=A_NONE then
  1278. begin
  1279. { NO MATCH! }
  1280. Message(assem_e_invalid_opcode_and_operand);
  1281. exit;
  1282. end;
  1283. inc(i);
  1284. end; { end while }
  1285. { We add the opcode to the opcode linked list }
  1286. if fits then
  1287. Begin
  1288. if instr.getprefix <> A_NONE then
  1289. Begin
  1290. p^.concat(new(pai386,op_none(instr.getprefix,S_NO)));
  1291. end;
  1292. case instr.numops of
  1293. 0:
  1294. if instr.stropsize <> S_NO then
  1295. { is this a string operation opcode or xlat then check }
  1296. { the size of the operation. }
  1297. p^.concat(new(pai386,op_none(instruc,instr.stropsize)))
  1298. else
  1299. p^.concat(new(pai386,op_none(instruc,S_NO)));
  1300. 1: Begin
  1301. case instr.operands[1].operandtype of
  1302. { all one operand opcodes with constant have no defined sizes }
  1303. { at least that is what it seems in the tasm 2.0 manual. }
  1304. OPR_CONSTANT: p^.concat(new(pai386,op_const(instruc,
  1305. S_NO, instr.operands[1].val)));
  1306. OPR_REGISTER: Case Instruc Of
  1307. A_INC,A_DEC, A_NEG,A_NOT:
  1308. Begin
  1309. p^.concat(new(pai386,op_reg(instruc,
  1310. instr.operands[1].size,instr.operands[1].reg)));
  1311. end
  1312. else
  1313. p^.concat(new(pai386,op_reg(instruc,
  1314. S_NO,instr.operands[1].reg)));
  1315. end;
  1316. { this is where it gets a bit more complicated... }
  1317. OPR_REFERENCE:
  1318. if instr.operands[1].size <> S_NO then
  1319. Begin
  1320. p^.concat(new(pai386,op_ref(instruc,
  1321. instr.operands[1].size,newreference(instr.operands[1].ref))));
  1322. end
  1323. else
  1324. Begin
  1325. { special jmp and call case with }
  1326. { symbolic references. }
  1327. case instruc of
  1328. A_CALL,A_JMP:
  1329. Begin
  1330. p^.concat(new(pai386,op_ref(instruc,
  1331. S_NO,newreference(instr.operands[1].ref))));
  1332. end
  1333. else
  1334. Message(assem_e_invalid_opcode_and_operand);
  1335. end;
  1336. End;
  1337. OPR_SYMBOL: Begin
  1338. p^.concat(new(pai386,op_sym(instruc,
  1339. instr.stropsize,instr.operands[1].symbol)));
  1340. End;
  1341. OPR_NONE: Begin
  1342. Message(assem_f_internal_error_in_concatopcode);
  1343. end;
  1344. else
  1345. Begin
  1346. Message(assem_f_internal_error_in_concatopcode);
  1347. end;
  1348. end;
  1349. end;
  1350. 2:
  1351. Begin
  1352. Case Instruc Of
  1353. A_MOVSX,A_MOVZX,A_MOVSB,A_MOVSBL,A_MOVSBW,
  1354. A_MOVSWL,A_MOVZB,A_MOVZWL:
  1355. { movzx and movsx }
  1356. HandleExtend(instr);
  1357. else
  1358. { other instructions }
  1359. Begin
  1360. With instr do
  1361. Begin
  1362. { source }
  1363. opsize := operands[1].size;
  1364. case operands[1].operandtype of
  1365. { reg,reg }
  1366. { reg,ref }
  1367. OPR_REGISTER:
  1368. Begin
  1369. case operands[2].operandtype of
  1370. OPR_REGISTER:
  1371. Begin
  1372. { see info in ratti386.pas, about the problem }
  1373. { which can cause gas here. }
  1374. if (opsize = operands[2].size) then
  1375. begin
  1376. p^.concat(new(pai386,op_reg_reg(instruc,
  1377. opsize,operands[1].reg,operands[2].reg)));
  1378. end
  1379. else
  1380. Case instruc Of
  1381. A_IN:
  1382. p^.concat(new(pai386,op_reg_reg(instruc,
  1383. operands[2].size,operands[1].reg,operands[2].reg)));
  1384. A_OUT:
  1385. p^.concat(new(pai386,op_reg_reg(instruc,
  1386. operands[1].size,operands[1].reg,operands[2].reg)));
  1387. { these do not require any size specification. }
  1388. A_SAL,A_SAR,A_SHL,A_SHR,A_ROL,A_ROR,A_RCR,
  1389. A_RCL:
  1390. { outs and ins are already taken care by }
  1391. { the first pass. }
  1392. p^.concat(new(pai386,op_reg_reg(instruc,
  1393. S_NO,operands[1].reg,operands[2].reg)))
  1394. else
  1395. Message(assem_e_invalid_opcode_and_operand);
  1396. end;
  1397. end; {case}
  1398. OPR_REFERENCE:
  1399. { variable name. }
  1400. { here we must check the instruction type }
  1401. { before deciding if to use and compare }
  1402. { any sizes. }
  1403. if assigned(operands[2].ref.symbol) then
  1404. Begin
  1405. if (opsize = operands[2].size) then
  1406. p^.concat(new(pai386,op_reg_ref(instruc,
  1407. opsize,operands[1].reg,newreference(operands[2].ref))))
  1408. Else
  1409. Case instruc Of
  1410. A_RCL,A_RCR,A_ROL,A_ROR,A_SAL,A_SAR,A_SHR,
  1411. A_SHL:
  1412. p^.concat(new(pai386,op_reg_ref(instruc,
  1413. opsize,operands[1].reg,newreference(operands[2].ref))))
  1414. else
  1415. Message(assem_e_invalid_size_in_ref);
  1416. End; {case}
  1417. end
  1418. else
  1419. Begin
  1420. { register reference }
  1421. { possiblities:1) local variable which }
  1422. { has been replaced by bp and offset }
  1423. { in this case size should be valid }
  1424. { 2) Indirect register }
  1425. { adressing, 1st operand determines }
  1426. { size. }
  1427. if (opsize = operands[2].size) or (operands[2].size = S_NO) then
  1428. p^.concat(new(pai386,op_reg_ref(instruc,
  1429. opsize,operands[1].reg,newreference(operands[2].ref))))
  1430. else
  1431. Message(assem_e_invalid_size_in_ref);
  1432. end;
  1433. OPR_CONSTANT: { const,reg }
  1434. Begin { OUT const,reg }
  1435. if (instruc = A_OUT) and (opsize = S_B) then
  1436. p^.concat(new(pai386,op_reg_const(instruc,
  1437. opsize,operands[1].reg,operands[2].val)))
  1438. else
  1439. Message(assem_e_invalid_size_in_ref);
  1440. end;
  1441. else { else case }
  1442. Begin
  1443. Message(assem_f_internal_error_in_concatopcode);
  1444. end;
  1445. end; { end inner case }
  1446. end;
  1447. { const,reg }
  1448. { const,const }
  1449. { const,ref }
  1450. OPR_CONSTANT:
  1451. case instr.operands[2].operandtype of
  1452. { constant, constant does not have a specific size. }
  1453. OPR_CONSTANT:
  1454. p^.concat(new(pai386,op_const_const(instruc,
  1455. S_NO,operands[1].val,operands[2].val)));
  1456. OPR_REFERENCE:
  1457. Begin
  1458. if (operands[1].val <= $ff) and
  1459. (operands[2].size in [S_B,S_W,S_L,
  1460. S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1461. p^.concat(new(pai386,op_const_ref(instruc,
  1462. operands[2].size,operands[1].val,
  1463. newreference(operands[2].ref))))
  1464. else
  1465. if (operands[1].val <= $ffff) and
  1466. (operands[2].size in [S_W,S_L,
  1467. S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1468. p^.concat(new(pai386,op_const_ref(instruc,
  1469. operands[2].size,operands[1].val,
  1470. newreference(operands[2].ref))))
  1471. else
  1472. if (operands[1].val <= $7fffffff) and
  1473. (operands[2].size in [S_L,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1474. p^.concat(new(pai386,op_const_ref(instruc,
  1475. operands[2].size,operands[1].val,
  1476. newreference(operands[2].ref))))
  1477. else
  1478. Message(assem_e_invalid_size_in_ref);
  1479. end;
  1480. OPR_REGISTER:
  1481. Begin
  1482. { size of opcode determined by register }
  1483. if (operands[1].val <= $ff) and
  1484. (operands[2].size in [S_B,S_W,S_L,S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1485. p^.concat(new(pai386,op_const_reg(instruc,
  1486. operands[2].size,operands[1].val,
  1487. operands[2].reg)))
  1488. else
  1489. if (operands[1].val <= $ffff) and
  1490. (operands[2].size in [S_W,S_L,S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1491. p^.concat(new(pai386,op_const_reg(instruc,
  1492. operands[2].size,operands[1].val,
  1493. operands[2].reg)))
  1494. else
  1495. if (operands[1].val <= $7fffffff) and
  1496. (operands[2].size in [S_L,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1497. p^.concat(new(pai386,op_const_reg(instruc,
  1498. operands[2].size,operands[1].val,
  1499. operands[2].reg)))
  1500. else
  1501. Message(assem_e_invalid_opcode_size);
  1502. end;
  1503. else
  1504. Begin
  1505. Message(assem_f_internal_error_in_concatopcode);
  1506. end;
  1507. end; { end case }
  1508. { ref,reg }
  1509. { ref,ref }
  1510. OPR_REFERENCE:
  1511. case instr.operands[2].operandtype of
  1512. OPR_REGISTER:
  1513. if assigned(operands[1].ref.symbol) then
  1514. { global variable }
  1515. Begin
  1516. Case instruc Of
  1517. A_LEA,A_LDS,A_LES,A_LFS,A_LGS,A_LSS:
  1518. p^.concat(new(pai386,op_ref_reg(instruc,
  1519. S_NO,newreference(operands[1].ref),
  1520. operands[2].reg)))
  1521. else
  1522. if (opsize = operands[2].size) then
  1523. p^.concat(new(pai386,op_ref_reg(instruc,
  1524. opsize,newreference(operands[1].ref),
  1525. operands[2].reg)))
  1526. else
  1527. Begin
  1528. Message(assem_e_invalid_opcode_and_operand);
  1529. end;
  1530. end; { case }
  1531. end
  1532. else
  1533. Begin
  1534. { register reference }
  1535. { possiblities:1) local variable which }
  1536. { has been replaced by bp and offset }
  1537. { in this case size should be valid }
  1538. { 2) Indirect register }
  1539. { adressing, 2nd operand determines }
  1540. { size. }
  1541. if (opsize = operands[2].size) or (opsize = S_NO) then
  1542. Begin
  1543. p^.concat(new(pai386,op_ref_reg(instruc,
  1544. operands[2].size,newreference(operands[1].ref),
  1545. operands[2].reg)));
  1546. end
  1547. else
  1548. Message(assem_e_invalid_size_in_ref);
  1549. end;
  1550. OPR_REFERENCE: { special opcodes }
  1551. p^.concat(new(pai386,op_ref_ref(instruc,
  1552. opsize,newreference(operands[1].ref),
  1553. newreference(operands[2].ref))));
  1554. else
  1555. Begin
  1556. Message(assem_f_internal_error_in_concatopcode);
  1557. end;
  1558. end; { end inner case }
  1559. end; { end case }
  1560. end; { end with }
  1561. end; {end else of case... }
  1562. end; { end case }
  1563. end;
  1564. 3: Begin
  1565. { only imul, shld and shrd }
  1566. { middle must be a register }
  1567. if ((instruc = A_SHLD) or (instruc = A_SHRD)) and (instr.operands[2].operandtype =
  1568. OPR_REGISTER) then
  1569. Begin
  1570. case instr.operands[2].size of
  1571. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  1572. Begin
  1573. if instr.operands[1].val <= $ff then
  1574. Begin
  1575. if instr.operands[3].size in [S_W] then
  1576. Begin
  1577. case instr.operands[3].operandtype of
  1578. OPR_REFERENCE: { MISSING !!!! } ;
  1579. OPR_REGISTER: p^.concat(new(pai386,
  1580. op_const_reg_reg(instruc, S_W,
  1581. instr.operands[1].val, instr.operands[2].reg,
  1582. instr.operands[3].reg)));
  1583. else
  1584. Message(assem_e_invalid_opcode_and_operand);
  1585. Message(assem_e_invalid_opcode_and_operand);
  1586. end;
  1587. end
  1588. else
  1589. Message(assem_e_invalid_opcode_and_operand);
  1590. end;
  1591. end
  1592. else
  1593. Message(assem_e_invalid_opcode_and_operand);
  1594. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  1595. Begin
  1596. if instr.operands[1].val <= $ff then
  1597. Begin
  1598. if instr.operands[3].size in [S_L] then
  1599. Begin
  1600. case instr.operands[3].operandtype of
  1601. OPR_REFERENCE: { MISSING !!!! } ;
  1602. OPR_REGISTER: p^.concat(new(pai386,
  1603. op_const_reg_reg(instruc, S_L,
  1604. instr.operands[1].val, instr.operands[2].reg,
  1605. instr.operands[3].reg)));
  1606. else
  1607. Message(assem_e_invalid_opcode_and_operand);
  1608. end;
  1609. end
  1610. else
  1611. Message(assem_e_invalid_opcode_and_operand);
  1612. end;
  1613. end;
  1614. else
  1615. Message(assem_e_invalid_opcode_and_operand);
  1616. end; { end case }
  1617. end
  1618. else
  1619. if (instruc = A_IMUL) and
  1620. (instr.operands[3].operandtype = OPR_REGISTER) then
  1621. Begin
  1622. case instr.operands[3].size of
  1623. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  1624. Begin
  1625. if instr.operands[1].val <= $ffff then
  1626. Begin
  1627. if instr.operands[2].size in [S_W] then
  1628. Begin
  1629. case instr.operands[2].operandtype of
  1630. OPR_REFERENCE: { MISSING !!!! } ;
  1631. OPR_REGISTER: p^.concat(new(pai386,
  1632. op_const_reg_reg(instruc, S_W,
  1633. instr.operands[1].val, instr.operands[2].reg,
  1634. instr.operands[3].reg)));
  1635. else
  1636. Message(assem_e_invalid_opcode_and_operand);
  1637. end; { end case }
  1638. end
  1639. else
  1640. Message(assem_e_invalid_opcode_and_operand);
  1641. end;
  1642. end
  1643. else
  1644. Message(assem_e_invalid_opcode_and_operand);
  1645. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  1646. Begin
  1647. if instr.operands[1].val <= $7fffffff then
  1648. Begin
  1649. if instr.operands[2].size in [S_L] then
  1650. Begin
  1651. case instr.operands[2].operandtype of
  1652. OPR_REFERENCE: { MISSING !!!! } ;
  1653. OPR_REGISTER: p^.concat(new(pai386,
  1654. op_const_reg_reg(instruc, S_L,
  1655. instr.operands[1].val, instr.operands[2].reg,
  1656. instr.operands[3].reg)));
  1657. else
  1658. Message(assem_e_invalid_opcode_and_operand);
  1659. end; { end case }
  1660. end
  1661. else
  1662. Message(assem_e_invalid_opcode_and_operand);
  1663. end;
  1664. end
  1665. else
  1666. Message(assem_e_invalid_opcode_and_operand);
  1667. else
  1668. Message(assem_e_invalid_middle_sized_operand);
  1669. end; { end case }
  1670. end { endif }
  1671. else
  1672. Message(assem_e_invalid_three_operand_opcode);
  1673. end;
  1674. end; { end case }
  1675. end; { end "if fits then" ... }
  1676. end;
  1677. {---------------------------------------------------------------------}
  1678. { Routines for the parsing }
  1679. {---------------------------------------------------------------------}
  1680. procedure consume(t : tinteltoken);
  1681. begin
  1682. if t<>actasmtoken then
  1683. Message(assem_e_syntax_error);
  1684. actasmtoken:=gettoken;
  1685. { if the token must be ignored, then }
  1686. { get another token to parse. }
  1687. if actasmtoken = AS_NONE then
  1688. actasmtoken := gettoken;
  1689. end;
  1690. function findregister(const s : string): tregister;
  1691. {*********************************************************************}
  1692. { FUNCTION findregister(s: string):tasmop; }
  1693. { Description: Determines if the s string is a valid register, }
  1694. { if so returns correct tregister token, or R_NO if not found. }
  1695. {*********************************************************************}
  1696. var
  1697. i: tregister;
  1698. begin
  1699. findregister := R_NO;
  1700. for i:=firstreg to lastreg do
  1701. if s = iasmregs[i] then
  1702. Begin
  1703. findregister := i;
  1704. exit;
  1705. end;
  1706. end;
  1707. function findoverride(const s: string; var reg:tregister): boolean;
  1708. var
  1709. i: byte;
  1710. begin
  1711. findoverride := FALSE;
  1712. reg := R_NO;
  1713. for i:=0 to _count_asmoverrides do
  1714. Begin
  1715. if s = _asmoverrides[i] then
  1716. begin
  1717. reg := _overridetokens[i];
  1718. findoverride := TRUE;
  1719. exit;
  1720. end;
  1721. end;
  1722. end;
  1723. function findprefix(const s: string; var token: tasmop): boolean;
  1724. var i: byte;
  1725. Begin
  1726. findprefix := FALSE;
  1727. for i:=0 to _count_asmprefixes do
  1728. Begin
  1729. if s = _asmprefixes[i] then
  1730. begin
  1731. token := _prefixtokens[i];
  1732. findprefix := TRUE;
  1733. exit;
  1734. end;
  1735. end;
  1736. end;
  1737. function findsegment(const s:string): tregister;
  1738. {*********************************************************************}
  1739. { FUNCTION findsegment(s: string):tasmop; }
  1740. { Description: Determines if the s string is a valid segment register}
  1741. { if so returns correct tregister token, or R_NO if not found. }
  1742. {*********************************************************************}
  1743. var
  1744. i: tregister;
  1745. Begin
  1746. findsegment := R_DEFAULT_SEG;
  1747. for i:=firstsreg to lastsreg do
  1748. if s = iasmregs[i] then
  1749. Begin
  1750. findsegment := i;
  1751. exit;
  1752. end;
  1753. end;
  1754. function findopcode(const s: string): tasmop;
  1755. {*********************************************************************}
  1756. { FUNCTION findopcode(s: string): tasmop; }
  1757. { Description: Determines if the s string is a valid opcode }
  1758. { if so returns correct tasmop token. }
  1759. {*********************************************************************}
  1760. var
  1761. i: tasmop;
  1762. j: byte;
  1763. Begin
  1764. findopcode := A_NONE;
  1765. for i:=firstop to lastop do
  1766. if s = iasmops^[i] then
  1767. begin
  1768. findopcode:=i;
  1769. exit;
  1770. end;
  1771. { not found yet, search for extended opcodes }
  1772. { now, in this case, we must use the suffix }
  1773. { to determine the size of the instruction }
  1774. for j:=0 to _count_asmspecialops do
  1775. Begin
  1776. if s = _specialops[j] then
  1777. Begin
  1778. findopcode := _specialopstokens[j];
  1779. { set the size }
  1780. case s[length(s)] of
  1781. 'B': instr.stropsize := S_B;
  1782. 'D': instr.stropsize := S_L;
  1783. 'W': instr.stropsize := S_W;
  1784. end;
  1785. exit;
  1786. end;
  1787. end;
  1788. end;
  1789. Function CheckPrefix(prefix: tasmop; opcode:tasmop): Boolean;
  1790. { Checks if the prefix is valid with the following instruction }
  1791. { return false if not, otherwise true }
  1792. Begin
  1793. CheckPrefix := TRUE;
  1794. Case prefix of
  1795. A_REP,A_REPNE,A_REPE:
  1796. Case opcode Of
  1797. A_SCAS,A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  1798. Else
  1799. Begin
  1800. CheckPrefix := FALSE;
  1801. exit;
  1802. end;
  1803. end; { case }
  1804. A_LOCK:
  1805. Case opcode Of
  1806. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  1807. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  1808. Else
  1809. Begin
  1810. CheckPrefix := FALSE;
  1811. Exit;
  1812. end;
  1813. end; { case }
  1814. A_NONE: exit; { no prefix here }
  1815. else
  1816. CheckPrefix := FALSE;
  1817. end; { end case }
  1818. end;
  1819. Procedure InitAsmRef(var instr: TInstruction);
  1820. {*********************************************************************}
  1821. { Description: This routine first check if the instruction is of }
  1822. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  1823. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  1824. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  1825. { to point to the default segment. }
  1826. {*********************************************************************}
  1827. Begin
  1828. With instr do
  1829. Begin
  1830. case operands[operandnum].operandtype of
  1831. OPR_REFERENCE: exit;
  1832. OPR_NONE: ;
  1833. else
  1834. Message(assem_e_invalid_operand_type);
  1835. end;
  1836. operands[operandnum].operandtype := OPR_REFERENCE;
  1837. operands[operandnum].ref.segment := R_DEFAULT_SEG;
  1838. end;
  1839. end;
  1840. Function CheckOverride(segreg: tregister; var instr: TInstruction): Boolean;
  1841. { Check if the override is valid, and if so then }
  1842. { update the instr variable accordingly. }
  1843. Begin
  1844. CheckOverride := FALSE;
  1845. Case instr.getinstruction of
  1846. A_MOVS,A_XLAT,A_CMPS:
  1847. Begin
  1848. CheckOverride := TRUE;
  1849. Message(assem_e_segment_override_not_supported);
  1850. end
  1851. end;
  1852. End;
  1853. Procedure GetRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  1854. {*********************************************************************}
  1855. { PROCEDURE GetRecordOffsetSize }
  1856. { Description: This routine builds up a record offset after a AS_DOT }
  1857. { token is encountered. }
  1858. { On entry actasmtoken should be equal to AS_DOT }
  1859. {*********************************************************************}
  1860. { EXIT CONDITION: On exit the routine should point to either the }
  1861. { ERROR RECOVER: read until AS_COMMA or AS_SEPARATOR token. }
  1862. { Warning: This is called recursively. }
  1863. {*********************************************************************}
  1864. var
  1865. toffset,tsize : longint;
  1866. Begin
  1867. offset:=0;
  1868. size:=0;
  1869. Consume(AS_DOT);
  1870. if actasmtoken = AS_ID then
  1871. Begin
  1872. if not GetTypeOffsetSize(expr,actasmpattern,toffset,tsize) and
  1873. not GetVarOffsetSize(expr,actasmpattern,toffset,tsize) then
  1874. begin
  1875. Message(assem_e_syntax_error);
  1876. toffset:=0;
  1877. tsize:=0;
  1878. end;
  1879. inc(offset,toffset);
  1880. size:=tsize;
  1881. Consume(AS_ID);
  1882. if actasmtoken=AS_DOT then
  1883. begin
  1884. GetRecordOffsetSize(expr,toffset,tsize);
  1885. inc(offset,toffset);
  1886. size:=tsize;
  1887. end;
  1888. end
  1889. else
  1890. Begin
  1891. Message(assem_e_syntax_error);
  1892. repeat
  1893. consume(actasmtoken)
  1894. until (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA);
  1895. end;
  1896. end;
  1897. Function BuildRefExpression: longint;
  1898. {*********************************************************************}
  1899. { FUNCTION BuildExpression: longint }
  1900. { Description: This routine calculates a constant expression to }
  1901. { a given value. The return value is the value calculated from }
  1902. { the expression. }
  1903. { The following tokens (not strings) are recognized: }
  1904. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  1905. {*********************************************************************}
  1906. { ENTRY: On entry the token should be any valid expression token. }
  1907. { EXIT: On Exit the token points to any token after the closing }
  1908. { RBRACKET }
  1909. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  1910. { invalid tokens. }
  1911. {*********************************************************************}
  1912. var
  1913. tempstr,expr : string;
  1914. l,k : longint;
  1915. errorflag : boolean;
  1916. Begin
  1917. errorflag := FALSE;
  1918. tempstr := '';
  1919. expr := '';
  1920. { tell tokenizer that we are in }
  1921. { an expression. }
  1922. inexpression := TRUE;
  1923. Repeat
  1924. Case actasmtoken of
  1925. AS_LPAREN:
  1926. Begin
  1927. Consume(AS_LPAREN);
  1928. expr := expr + '(';
  1929. end;
  1930. AS_RPAREN:
  1931. Begin
  1932. Consume(AS_RPAREN);
  1933. expr := expr + ')';
  1934. end;
  1935. AS_SHL:
  1936. Begin
  1937. Consume(AS_SHL);
  1938. expr := expr + '<';
  1939. end;
  1940. AS_SHR:
  1941. Begin
  1942. Consume(AS_SHR);
  1943. expr := expr + '>';
  1944. end;
  1945. AS_SLASH:
  1946. Begin
  1947. Consume(AS_SLASH);
  1948. expr := expr + '/';
  1949. end;
  1950. AS_MOD:
  1951. Begin
  1952. Consume(AS_MOD);
  1953. expr := expr + '%';
  1954. end;
  1955. AS_STAR:
  1956. Begin
  1957. Consume(AS_STAR);
  1958. expr := expr + '*';
  1959. end;
  1960. AS_PLUS:
  1961. Begin
  1962. Consume(AS_PLUS);
  1963. expr := expr + '+';
  1964. end;
  1965. AS_MINUS:
  1966. Begin
  1967. Consume(AS_MINUS);
  1968. expr := expr + '-';
  1969. end;
  1970. AS_AND:
  1971. Begin
  1972. Consume(AS_AND);
  1973. expr := expr + '&';
  1974. end;
  1975. AS_NOT:
  1976. Begin
  1977. Consume(AS_NOT);
  1978. expr := expr + '~';
  1979. end;
  1980. AS_XOR:
  1981. Begin
  1982. Consume(AS_XOR);
  1983. expr := expr + '^';
  1984. end;
  1985. AS_OR:
  1986. Begin
  1987. Consume(AS_OR);
  1988. expr := expr + '|';
  1989. end;
  1990. AS_INTNUM:
  1991. Begin
  1992. expr := expr + actasmpattern;
  1993. Consume(AS_INTNUM);
  1994. end;
  1995. AS_BINNUM:
  1996. Begin
  1997. expr:=expr+BinaryToDec(actasmpattern);
  1998. Consume(AS_BINNUM);
  1999. end;
  2000. AS_HEXNUM:
  2001. Begin
  2002. expr:=expr+HexToDec(actasmpattern);
  2003. Consume(AS_HEXNUM);
  2004. end;
  2005. AS_OCTALNUM:
  2006. Begin
  2007. expr:=expr+OctalToDec(actasmpattern);
  2008. Consume(AS_OCTALNUM);
  2009. end;
  2010. AS_ID:
  2011. Begin
  2012. tempstr:=actasmpattern;
  2013. consume(AS_ID);
  2014. if actasmtoken=AS_DOT then
  2015. begin
  2016. GetRecordOffsetSize(tempstr,l,k);
  2017. str(l, tempstr);
  2018. expr := expr + tempstr;
  2019. end
  2020. else
  2021. begin
  2022. if SearchIConstant(tempstr,l) then
  2023. begin
  2024. str(l, tempstr);
  2025. expr := expr + tempstr;
  2026. end
  2027. else
  2028. Message1(assem_e_invalid_const_symbol,tempstr);
  2029. end;
  2030. end;
  2031. AS_RBRACKET: { End of reference }
  2032. Begin
  2033. if not ErrorFlag then
  2034. BuildRefExpression := CalculateExpression(expr)
  2035. else
  2036. BuildRefExpression := 0;
  2037. Consume(AS_RBRACKET);
  2038. { no longer in an expression }
  2039. inexpression := FALSE;
  2040. exit;
  2041. end;
  2042. else
  2043. Begin
  2044. { write error only once. }
  2045. if not errorflag then
  2046. Message(assem_e_invalid_constant_expression);
  2047. BuildRefExpression := 0;
  2048. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then
  2049. exit;
  2050. { consume tokens until we find COMMA or SEPARATOR }
  2051. Consume(actasmtoken);
  2052. errorflag := TRUE;
  2053. end;
  2054. end;
  2055. Until false;
  2056. end;
  2057. Procedure BuildRecordOffset(var instr: TInstruction; varname: string);
  2058. {*********************************************************************}
  2059. { PROCEDURE BuildRecordOffset(var Instr: TInstruction) }
  2060. { Description: This routine takes care of field specifiers of records }
  2061. { and/or variables in asm operands. It updates the offset accordingly}
  2062. {*********************************************************************}
  2063. { ENTRY: On entry the token should be DOT. }
  2064. { name: should be the name of the variable to be expanded. '' if }
  2065. { no variabled specified. }
  2066. { EXIT: On Exit the token points to SEPARATOR or COMMA. }
  2067. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2068. { invalid tokens. }
  2069. {*********************************************************************}
  2070. var
  2071. firstpass: boolean;
  2072. offset: longint;
  2073. tsize,toffset : longint;
  2074. basetypename : string;
  2075. Begin
  2076. basetypename := '';
  2077. firstpass := TRUE;
  2078. { .ID[REG].ID ... }
  2079. { .ID.ID... }
  2080. Consume(AS_DOT);
  2081. Repeat
  2082. case actasmtoken of
  2083. AS_ID: Begin
  2084. { we must reset the operand size - since only the last field }
  2085. { will give us the size of the operand. }
  2086. { instr.opsize := S_NO;}
  2087. InitAsmRef(instr);
  2088. { var_name.typefield.typefield }
  2089. if (varname <> '') then
  2090. Begin
  2091. if GetVarOffsetSize(varname,actasmpattern,toffset,tsize) then
  2092. Begin
  2093. Inc(instr.operands[operandnum].ref.offset,tOffset);
  2094. SetOperandSize(instr,operandnum,tsize);
  2095. end
  2096. else
  2097. Message1(assem_e_unknown_id,actasmpattern);
  2098. end
  2099. else
  2100. { [ref].var_name.typefield.typefield ... }
  2101. { [ref].var_name[reg] }
  2102. if not assigned(instr.operands[operandnum].ref.symbol) and
  2103. firstpass then
  2104. Begin
  2105. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2106. Begin
  2107. { type field ? }
  2108. basetypename := actasmpattern;
  2109. end
  2110. else
  2111. varname := actasmpattern;
  2112. end
  2113. else
  2114. if firstpass then
  2115. { [ref].typefield.typefield ... }
  2116. { where the first typefield must specifiy the base }
  2117. { object or record type. }
  2118. Begin
  2119. basetypename := actasmpattern;
  2120. end
  2121. else
  2122. { [ref].typefield.typefield ... }
  2123. { basetpyename is already set up... now look for fields. }
  2124. Begin
  2125. if GetTypeOffsetSize(basetypename,actasmpattern,tOffset,Tsize) then
  2126. Begin
  2127. Inc(instr.operands[operandnum].ref.offset,tOffset);
  2128. SetOperandSize(instr,operandnum,Tsize);
  2129. end
  2130. else
  2131. Message1(assem_e_unknown_id,actasmpattern);
  2132. end;
  2133. Consume(AS_ID);
  2134. { Take care of index register on this variable }
  2135. if actasmtoken = AS_LBRACKET then
  2136. Begin
  2137. Consume(AS_LBRACKET);
  2138. Case actasmtoken of
  2139. AS_REGISTER: Begin
  2140. if instr.operands[operandnum].ref.index <> R_NO then
  2141. Message(assem_e_defining_index_more_than_once);
  2142. instr.operands[operandnum].ref.index :=
  2143. findregister(actasmpattern);
  2144. Consume(AS_REGISTER);
  2145. end;
  2146. else
  2147. Begin
  2148. { add offsets , assuming these are constant expressions... }
  2149. Inc(instr.operands[operandnum].ref.offset,BuildRefExpression);
  2150. end;
  2151. end;
  2152. Consume(AS_RBRACKET);
  2153. end;
  2154. { Here we should either have AS_DOT, AS_SEPARATOR or AS_COMMA }
  2155. if actasmtoken = AS_DOT then
  2156. Consume(AS_DOT);
  2157. firstpass := FALSE;
  2158. Offset := 0;
  2159. end;
  2160. AS_SEPARATOR: exit;
  2161. AS_COMMA: exit;
  2162. else
  2163. Begin
  2164. Message(assem_e_invalid_field_specifier);
  2165. Consume(actasmtoken);
  2166. firstpass := FALSE;
  2167. end;
  2168. end; { end case }
  2169. Until (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA);
  2170. end;
  2171. Function BuildExpression: longint;
  2172. {*********************************************************************}
  2173. { FUNCTION BuildExpression: longint }
  2174. { Description: This routine calculates a constant expression to }
  2175. { a given value. The return value is the value calculated from }
  2176. { the expression. }
  2177. { The following tokens (not strings) are recognized: }
  2178. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  2179. {*********************************************************************}
  2180. { ENTRY: On entry the token should be any valid expression token. }
  2181. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  2182. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2183. { invalid tokens. }
  2184. {*********************************************************************}
  2185. var expr: string;
  2186. tempstr: string;
  2187. l,k : longint;
  2188. errorflag: boolean;
  2189. Begin
  2190. errorflag := FALSE;
  2191. expr := '';
  2192. tempstr := '';
  2193. { tell tokenizer that we are in an expression. }
  2194. inexpression := TRUE;
  2195. Repeat
  2196. Case actasmtoken of
  2197. AS_LPAREN: Begin
  2198. Consume(AS_LPAREN);
  2199. expr := expr + '(';
  2200. end;
  2201. AS_RPAREN: Begin
  2202. Consume(AS_RPAREN);
  2203. expr := expr + ')';
  2204. end;
  2205. AS_SHL: Begin
  2206. Consume(AS_SHL);
  2207. expr := expr + '<';
  2208. end;
  2209. AS_SHR: Begin
  2210. Consume(AS_SHR);
  2211. expr := expr + '>';
  2212. end;
  2213. AS_SLASH: Begin
  2214. Consume(AS_SLASH);
  2215. expr := expr + '/';
  2216. end;
  2217. AS_MOD: Begin
  2218. Consume(AS_MOD);
  2219. expr := expr + '%';
  2220. end;
  2221. AS_STAR: Begin
  2222. Consume(AS_STAR);
  2223. expr := expr + '*';
  2224. end;
  2225. AS_PLUS: Begin
  2226. Consume(AS_PLUS);
  2227. expr := expr + '+';
  2228. end;
  2229. AS_MINUS: Begin
  2230. Consume(AS_MINUS);
  2231. expr := expr + '-';
  2232. end;
  2233. AS_AND: Begin
  2234. Consume(AS_AND);
  2235. expr := expr + '&';
  2236. end;
  2237. AS_NOT: Begin
  2238. Consume(AS_NOT);
  2239. expr := expr + '~';
  2240. end;
  2241. AS_XOR: Begin
  2242. Consume(AS_XOR);
  2243. expr := expr + '^';
  2244. end;
  2245. AS_OR: Begin
  2246. Consume(AS_OR);
  2247. expr := expr + '|';
  2248. end;
  2249. AS_ID: Begin
  2250. tempstr:=actasmpattern;
  2251. consume(AS_ID);
  2252. if actasmtoken=AS_DOT then
  2253. begin
  2254. GetRecordOffsetSize(tempstr,l,k);
  2255. str(l, tempstr);
  2256. expr := expr + tempstr;
  2257. end
  2258. else
  2259. begin
  2260. if SearchIConstant(tempstr,l) then
  2261. begin
  2262. str(l, tempstr);
  2263. expr := expr + tempstr;
  2264. end
  2265. else
  2266. Message1(assem_e_invalid_const_symbol,actasmpattern);
  2267. end;
  2268. end;
  2269. AS_INTNUM: Begin
  2270. expr := expr + actasmpattern;
  2271. Consume(AS_INTNUM);
  2272. end;
  2273. AS_BINNUM: Begin
  2274. tempstr := BinaryToDec(actasmpattern);
  2275. if tempstr = '' then
  2276. Message(assem_f_error_converting_bin);
  2277. expr:=expr+tempstr;
  2278. Consume(AS_BINNUM);
  2279. end;
  2280. AS_HEXNUM: Begin
  2281. tempstr := HexToDec(actasmpattern);
  2282. if tempstr = '' then
  2283. Message(assem_f_error_converting_hex);
  2284. expr:=expr+tempstr;
  2285. Consume(AS_HEXNUM);
  2286. end;
  2287. AS_OCTALNUM: Begin
  2288. tempstr := OctalToDec(actasmpattern);
  2289. if tempstr = '' then
  2290. Message(assem_f_error_converting_octal);
  2291. expr:=expr+tempstr;
  2292. Consume(AS_OCTALNUM);
  2293. end;
  2294. { go to next term }
  2295. AS_COMMA: Begin
  2296. if not ErrorFlag then
  2297. BuildExpression := CalculateExpression(expr)
  2298. else
  2299. BuildExpression := 0;
  2300. inexpression := FALSE;
  2301. Exit;
  2302. end;
  2303. { go to next symbol }
  2304. AS_SEPARATOR: Begin
  2305. if not ErrorFlag then
  2306. BuildExpression := CalculateExpression(expr)
  2307. else
  2308. BuildExpression := 0;
  2309. inexpression := FALSE;
  2310. Exit;
  2311. end;
  2312. else
  2313. Begin
  2314. { only write error once. }
  2315. if not errorflag then
  2316. Message(assem_e_invalid_constant_expression);
  2317. { consume tokens until we find COMMA or SEPARATOR }
  2318. Consume(actasmtoken);
  2319. errorflag := TRUE;
  2320. End;
  2321. end;
  2322. Until false;
  2323. end;
  2324. Procedure BuildScaling(Var instr: TInstruction);
  2325. {*********************************************************************}
  2326. { Takes care of parsing expression starting from the scaling value }
  2327. { up to and including possible field specifiers. }
  2328. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR }
  2329. { or AS_COMMA. On entry should point to AS_STAR token. }
  2330. {*********************************************************************}
  2331. var str:string;
  2332. l: longint;
  2333. code: integer;
  2334. Begin
  2335. Consume(AS_STAR);
  2336. if (instr.operands[operandnum].ref.scalefactor <> 0)
  2337. and (instr.operands[operandnum].ref.scalefactor <> 1) then
  2338. Begin
  2339. Message(assem_f_internal_error_in_buildscale);
  2340. end;
  2341. case actasmtoken of
  2342. AS_INTNUM: str := actasmpattern;
  2343. AS_HEXNUM: str := HexToDec(actasmpattern);
  2344. AS_BINNUM: str := BinaryToDec(actasmpattern);
  2345. AS_OCTALNUM: str := OctalToDec(actasmpattern);
  2346. else
  2347. Message(assem_e_syntax_error);
  2348. end;
  2349. val(str, l, code);
  2350. if code <> 0 then
  2351. Message(assem_e_invalid_scaling_factor);
  2352. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) and (code = 0) then
  2353. begin
  2354. instr.operands[operandnum].ref.scalefactor := l;
  2355. end
  2356. else
  2357. Begin
  2358. Message(assem_e_invalid_scaling_value);
  2359. instr.operands[operandnum].ref.scalefactor := 0;
  2360. end;
  2361. if instr.operands[operandnum].ref.index = R_NO then
  2362. Begin
  2363. Message(assem_e_scaling_value_only_allowed_with_index);
  2364. instr.operands[operandnum].ref.scalefactor := 0;
  2365. end;
  2366. { Consume the scaling number }
  2367. Consume(actasmtoken);
  2368. case actasmtoken of
  2369. { [...*SCALING-expr] ... }
  2370. AS_MINUS: Begin
  2371. if instr.operands[operandnum].ref.offset <> 0 then
  2372. Message(assem_f_internal_error_in_buildscale);
  2373. instr.operands[operandnum].ref.offset :=
  2374. BuildRefExpression;
  2375. end;
  2376. { [...*SCALING+expr] ... }
  2377. AS_PLUS: Begin
  2378. if instr.operands[operandnum].ref.offset <> 0 then
  2379. Message(assem_f_internal_error_in_buildscale);
  2380. instr.operands[operandnum].ref.offset :=
  2381. BuildRefExpression;
  2382. end;
  2383. { [...*SCALING] ... }
  2384. AS_RBRACKET: Consume(AS_RBRACKET);
  2385. else
  2386. Message(assem_e_invalid_scaling_value);
  2387. end;
  2388. { .Field.Field ... or separator/comma }
  2389. Case actasmtoken of
  2390. AS_DOT: BuildRecordOffset(instr,'');
  2391. AS_COMMA, AS_SEPARATOR: ;
  2392. else
  2393. Message(assem_e_syntax_error);
  2394. end;
  2395. end;
  2396. Procedure BuildReference(var instr: TInstruction);
  2397. {*********************************************************************}
  2398. { EXIT CONDITION: On exit the routine should point to either the }
  2399. { AS_COMMA or AS_SEPARATOR token. }
  2400. { On entry: contains the register after the opening bracket if any. }
  2401. {*********************************************************************}
  2402. var
  2403. reg:string;
  2404. segreg: boolean;
  2405. negative: boolean;
  2406. expr: string;
  2407. Begin
  2408. expr := '';
  2409. if instr.operands[operandnum].operandtype <> OPR_REFERENCE then
  2410. Begin
  2411. Message(assem_e_syn_no_ref_with_brackets);
  2412. InitAsmRef(instr);
  2413. consume(AS_REGISTER);
  2414. end
  2415. else
  2416. Begin
  2417. { save the reg }
  2418. reg := actasmpattern;
  2419. consume(AS_REGISTER);
  2420. { is the syntax of the form: [REG:REG...] }
  2421. if actasmtoken = AS_COLON then
  2422. begin
  2423. segreg := TRUE;
  2424. Message(assem_e_expression_form_not_supported);
  2425. if instr.operands[operandnum].ref.segment <> R_NO then
  2426. Message(assem_e_defining_seg_more_than_once);
  2427. instr.operands[operandnum].ref.segment := findsegment(reg);
  2428. { Here we should process the syntax of the form }
  2429. { [reg:reg...] }
  2430. end
  2431. else { SREG:[REG...] where SReg: is optional. }
  2432. Begin
  2433. if actasmtoken=AS_STAR then
  2434. begin
  2435. if instr.operands[operandnum].ref.index <> R_NO then
  2436. Message(assem_e_defining_index_more_than_once);
  2437. instr.operands[operandnum].ref.index := findregister(reg);
  2438. end
  2439. else
  2440. begin
  2441. if instr.operands[operandnum].ref.base <> R_NO then
  2442. Message(assem_e_defining_base_more_than_once);
  2443. instr.operands[operandnum].ref.base := findregister(reg);
  2444. end;
  2445. end;
  2446. { we process this type of syntax immediately... }
  2447. case actasmtoken of
  2448. { SREG:[REG].Field.Field ... }
  2449. { SREG:[REG].Field[REG].Field... }
  2450. AS_RBRACKET:
  2451. Begin
  2452. Consume(AS_RBRACKET);
  2453. { check for record fields }
  2454. if actasmtoken = AS_DOT then
  2455. BuildRecordOffset(instr,'');
  2456. if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  2457. exit
  2458. else
  2459. Message(assem_e_syn_reference);
  2460. end;
  2461. { SREG:[REG +/- ...].Field.Field ... }
  2462. AS_PLUS,
  2463. AS_MINUS:
  2464. Begin
  2465. if actasmtoken = AS_MINUS then
  2466. Begin
  2467. expr := '-';
  2468. negative := TRUE
  2469. end
  2470. else
  2471. Begin
  2472. negative := FALSE;
  2473. expr := '+';
  2474. end;
  2475. Consume(actasmtoken);
  2476. { REG:[REG+REG+/-...].Field.Field }
  2477. if actasmtoken = AS_REGISTER then
  2478. Begin
  2479. if negative then
  2480. Message(assem_e_negative_index_register);
  2481. if instr.operands[operandnum].ref.index <> R_NO then
  2482. Message(assem_e_defining_index_more_than_once);
  2483. instr.operands[operandnum].ref.index := findregister(actasmpattern);
  2484. Consume(AS_REGISTER);
  2485. case actasmtoken of
  2486. AS_RBRACKET: { SREG:[REG+REG].Field.Field... }
  2487. Begin
  2488. Consume(AS_RBRACKET);
  2489. Case actasmtoken of
  2490. AS_DOT: BuildRecordOffset(instr,'');
  2491. AS_COMMA,
  2492. AS_SEPARATOR: exit;
  2493. else
  2494. Message(assem_e_syntax_error);
  2495. end;
  2496. end;
  2497. AS_PLUS,
  2498. AS_MINUS: { REG:[REG+REG+/-expr... }
  2499. Begin
  2500. if instr.operands[operandnum].ref.offset <> 0 then
  2501. Message(assem_f_internal_error_in_buildreference);
  2502. instr.operands[operandnum].ref.offset:=BuildRefExpression;
  2503. case actasmtoken of
  2504. AS_DOT: BuildRecordOffset(instr,'');
  2505. AS_COMMA,
  2506. AS_SEPARATOR: ;
  2507. else
  2508. Message(assem_e_syntax_error);
  2509. end;
  2510. end;
  2511. AS_STAR: { REG:[REG+REG*SCALING...].Field.Field... }
  2512. begin
  2513. BuildScaling(instr);
  2514. end;
  2515. else
  2516. Message(assem_e_syntax_error);
  2517. end; { end case }
  2518. end
  2519. else
  2520. { REG:[REG*(+/-)SCALING ... ] }
  2521. if actasmtoken = AS_STAR then
  2522. Begin
  2523. BuildScaling(instr);
  2524. end
  2525. else
  2526. { REG:[REG+expr].Field.Field }
  2527. Begin
  2528. if instr.operands[operandnum].ref.offset <> 0 then
  2529. Message(assem_f_internal_error_in_buildreference);
  2530. if negative then
  2531. instr.operands[operandnum].ref.offset := -BuildRefExpression
  2532. else
  2533. instr.operands[operandnum].ref.offset := BuildRefExpression;
  2534. case actasmtoken of
  2535. AS_DOT: BuildRecordOffset(instr,'');
  2536. AS_COMMA,
  2537. AS_SEPARATOR: ;
  2538. else
  2539. Message(assem_e_syntax_error);
  2540. end;
  2541. end; { end if }
  2542. end; { end this case }
  2543. AS_STAR: { REG:[REG*scaling] ... }
  2544. Begin
  2545. BuildScaling(instr);
  2546. end;
  2547. end;
  2548. end; { end outer if }
  2549. end;
  2550. Procedure BuildBracketExpression(var Instr: TInstruction; var_prefix: boolean);
  2551. {*********************************************************************}
  2552. { PROCEDURE BuildBracketExpression }
  2553. { Description: This routine builds up an expression after a LBRACKET }
  2554. { token is encountered. }
  2555. { On entry actasmtoken should be equal to AS_LBRACKET. }
  2556. { var_prefix : Should be set to true if variable identifier has }
  2557. { been defined, such as in ID[ }
  2558. {*********************************************************************}
  2559. { EXIT CONDITION: On exit the routine should point to either the }
  2560. { AS_COMMA or AS_SEPARATOR token. }
  2561. {*********************************************************************}
  2562. var
  2563. l:longint;
  2564. Begin
  2565. Consume(AS_LBRACKET);
  2566. initAsmRef(instr);
  2567. Case actasmtoken of
  2568. { Constant reference expression OR variable reference expression }
  2569. AS_ID: Begin
  2570. if actasmpattern[1] = '@' then
  2571. Message(assem_e_local_symbol_not_allowed_as_ref);
  2572. if SearchIConstant(actasmpattern,l) then
  2573. Begin
  2574. { if there was a variable prefix then }
  2575. { add to offset }
  2576. If var_prefix then
  2577. Begin
  2578. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2579. end
  2580. else
  2581. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2582. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2583. Message(assem_e_invalid_operand_in_bracket_expression);
  2584. end
  2585. else if NOT var_prefix then
  2586. Begin
  2587. InitAsmRef(instr);
  2588. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2589. Message1(assem_e_unknown_id,actasmpattern);
  2590. Consume(AS_ID);
  2591. { is there a constant expression following }
  2592. { the variable name? }
  2593. if actasmtoken <> AS_RBRACKET then
  2594. Begin
  2595. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2596. end
  2597. else
  2598. Consume(AS_RBRACKET);
  2599. end
  2600. else
  2601. Message1(assem_e_invalid_symbol_name,actasmpattern);
  2602. end;
  2603. { Here we handle the special case in tp where }
  2604. { the + operator is allowed with reg and var }
  2605. { references, such as in mov al, byte ptr [+bx] }
  2606. AS_PLUS: Begin
  2607. Consume(AS_PLUS);
  2608. Case actasmtoken of
  2609. AS_REGISTER: Begin
  2610. BuildReference(instr);
  2611. end;
  2612. AS_ID: Begin
  2613. if actasmpattern[1] = '@' then
  2614. Message(assem_e_local_symbol_not_allowed_as_ref);
  2615. if SearchIConstant(actasmpattern,l) then
  2616. Begin
  2617. { if there was a variable prefix then }
  2618. { add to offset }
  2619. If var_prefix then
  2620. Begin
  2621. Inc(instr.operands[operandnum].ref.offset,
  2622. BuildRefExpression);
  2623. end
  2624. else
  2625. instr.operands[operandnum].ref.offset :=
  2626. BuildRefExpression;
  2627. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2628. Message(assem_e_invalid_operand_in_bracket_expression);
  2629. end
  2630. else if NOT var_prefix then
  2631. Begin
  2632. InitAsmRef(instr);
  2633. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2634. Message1(assem_e_unknown_id,actasmpattern);
  2635. Consume(AS_ID);
  2636. { is there a constant expression following }
  2637. { the variable name? }
  2638. if actasmtoken <> AS_RBRACKET then
  2639. Begin
  2640. Inc(instr.operands[operandnum].ref.offset,
  2641. BuildRefExpression);
  2642. end
  2643. else
  2644. Consume(AS_RBRACKET);
  2645. end
  2646. else
  2647. Message1(assem_e_invalid_symbol_name,actasmpattern);
  2648. end;
  2649. { Constant reference expression // }
  2650. AS_INTNUM,AS_BINNUM,AS_OCTALNUM,
  2651. AS_HEXNUM: Begin
  2652. { if there was a variable prefix then }
  2653. { add to offset instead. }
  2654. If var_prefix then
  2655. Begin
  2656. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2657. end
  2658. else
  2659. Begin
  2660. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2661. end;
  2662. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2663. Message(assem_e_invalid_operand_in_bracket_expression);
  2664. end;
  2665. else
  2666. Message(assem_e_syntax_error);
  2667. end;
  2668. end;
  2669. { Constant reference expression // }
  2670. AS_MINUS,AS_NOT,AS_LPAREN:
  2671. Begin
  2672. { if there was a variable prefix then }
  2673. { add to offset instead. }
  2674. If var_prefix then
  2675. Begin
  2676. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2677. end
  2678. else
  2679. Begin
  2680. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2681. end;
  2682. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2683. Message(assem_e_invalid_operand_in_bracket_expression);
  2684. end;
  2685. { Constant reference expression // }
  2686. AS_INTNUM,AS_OCTALNUM,AS_BINNUM,AS_HEXNUM: Begin
  2687. { if there was a variable prefix then }
  2688. { add to offset instead. }
  2689. If var_prefix then
  2690. Begin
  2691. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2692. end
  2693. else
  2694. Begin
  2695. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2696. end;
  2697. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2698. Message(assem_e_invalid_operand_in_bracket_expression);
  2699. end;
  2700. { Variable reference expression }
  2701. AS_REGISTER: BuildReference(instr);
  2702. else
  2703. Begin
  2704. Message(assem_e_invalid_reference_syntax);
  2705. while (actasmtoken <> AS_SEPARATOR) do
  2706. Consume(actasmtoken);
  2707. end;
  2708. end; { end case }
  2709. end;
  2710. Procedure BuildOperand(var instr: TInstruction);
  2711. {*********************************************************************}
  2712. { EXIT CONDITION: On exit the routine should point to either the }
  2713. { AS_COMMA or AS_SEPARATOR token. }
  2714. {*********************************************************************}
  2715. var
  2716. tempstr: string;
  2717. expr: string;
  2718. lab: Pasmlabel;
  2719. l : longint;
  2720. hl: plabel;
  2721. Begin
  2722. tempstr := '';
  2723. expr := '';
  2724. case actasmtoken of
  2725. { Constant expression // }
  2726. AS_PLUS,AS_MINUS,AS_NOT,AS_LPAREN:
  2727. Begin
  2728. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2729. Message(assem_e_invalid_operand_type);
  2730. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2731. instr.operands[operandnum].val :=BuildExpression;
  2732. end;
  2733. { Constant expression // }
  2734. AS_STRING: Begin
  2735. if not (instr.operands[operandnum].operandtype in [OPR_NONE]) then
  2736. Message(assem_e_invalid_operand_type);
  2737. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2738. if not PadZero(actasmpattern,4) then
  2739. Message1(assem_e_invalid_string_as_opcode_operand,actasmpattern);
  2740. instr.operands[operandnum].val :=
  2741. ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  2742. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1])
  2743. shl 24;
  2744. Consume(AS_STRING);
  2745. Case actasmtoken of
  2746. AS_COMMA, AS_SEPARATOR: ;
  2747. else
  2748. Message(assem_e_invalid_string_expression);
  2749. end; { end case }
  2750. end;
  2751. { Constant expression // }
  2752. AS_INTNUM,AS_BINNUM,
  2753. AS_OCTALNUM,
  2754. AS_HEXNUM: Begin
  2755. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2756. Message(assem_e_invalid_operand_type);
  2757. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2758. instr.operands[operandnum].val :=BuildExpression;
  2759. end;
  2760. { A constant expression, or a Variable ref. }
  2761. AS_ID: Begin
  2762. if actasmpattern[1] = '@' then
  2763. { Label or Special symbol reference }
  2764. Begin
  2765. if actasmpattern = '@RESULT' then
  2766. Begin
  2767. InitAsmRef(instr);
  2768. SetUpResult(instr,operandnum);
  2769. end
  2770. else
  2771. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  2772. Message(assem_w_CODE_and_DATA_not_supported)
  2773. else
  2774. Begin
  2775. delete(actasmpattern,1,1);
  2776. if actasmpattern = '' then
  2777. Message(assem_e_null_label_ref_not_allowed);
  2778. lab := labellist.search(actasmpattern);
  2779. { check if the label is already defined }
  2780. { if so, we then check if the plabel is }
  2781. { non-nil, if so we add it to instruction }
  2782. if assigned(lab) then
  2783. Begin
  2784. if assigned(lab^.lab) then
  2785. Begin
  2786. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2787. instr.operands[operandnum].hl := lab^.lab;
  2788. instr.labeled := TRUE;
  2789. end;
  2790. end
  2791. else
  2792. { the label does not exist, create it }
  2793. { emit the opcode, but set that the }
  2794. { label has not been emitted }
  2795. Begin
  2796. getlabel(hl);
  2797. labellist.insert(actasmpattern,hl,FALSE);
  2798. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2799. instr.operands[operandnum].hl := hl;
  2800. instr.labeled := TRUE;
  2801. end;
  2802. end;
  2803. Consume(AS_ID);
  2804. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2805. Begin
  2806. Message(assem_e_syntax_error);
  2807. end;
  2808. end
  2809. else
  2810. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  2811. begin
  2812. InitAsmRef(instr);
  2813. SetUpResult(instr,operandnum);
  2814. Consume(AS_ID);
  2815. end
  2816. { probably a variable or normal expression }
  2817. { or a procedure (such as in CALL ID) }
  2818. else
  2819. Begin
  2820. { is it a constant ? }
  2821. if SearchIConstant(actasmpattern,l) then
  2822. Begin
  2823. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2824. Message(assem_e_invalid_operand_type);
  2825. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2826. instr.operands[operandnum].val :=BuildExpression;
  2827. end
  2828. else { is it a label variable ? }
  2829. Begin
  2830. { ID[ , ID.Field.Field or simple ID }
  2831. { check if this is a label, if so then }
  2832. { emit it as a label. }
  2833. if SearchLabel(actasmpattern,hl) then
  2834. Begin
  2835. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2836. instr.operands[operandnum].hl := hl;
  2837. instr.labeled := TRUE;
  2838. Consume(AS_ID);
  2839. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2840. Message(assem_e_syntax_error);
  2841. end
  2842. else
  2843. { is it a normal variable ? }
  2844. Begin
  2845. initAsmRef(instr);
  2846. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2847. Begin
  2848. { not a variable.. }
  2849. { check special variables.. }
  2850. if actasmpattern = 'SELF' then
  2851. { special self variable }
  2852. Begin
  2853. if assigned(procinfo._class) then
  2854. Begin
  2855. instr.operands[operandnum].ref.offset := procinfo.ESI_offset;
  2856. instr.operands[operandnum].ref.base := procinfo.framepointer;
  2857. end
  2858. else
  2859. Message(assem_e_cannot_use_SELF_outside_a_method);
  2860. end
  2861. else
  2862. Message1(assem_e_unknown_id,actasmpattern);
  2863. end;
  2864. expr := actasmpattern;
  2865. Consume(AS_ID);
  2866. case actasmtoken of
  2867. AS_LBRACKET: { indexing }
  2868. BuildBracketExpression(instr,TRUE);
  2869. AS_DOT: BuildRecordOffset(instr,expr);
  2870. AS_SEPARATOR,AS_COMMA: ;
  2871. else
  2872. Message(assem_e_syntax_error);
  2873. end;
  2874. end;
  2875. end;
  2876. end;
  2877. end;
  2878. { Register, a variable reference or a constant reference }
  2879. AS_REGISTER: Begin
  2880. { save the type of register used. }
  2881. tempstr := actasmpattern;
  2882. Consume(AS_REGISTER);
  2883. if actasmtoken = AS_COLON then
  2884. Begin
  2885. Consume(AS_COLON);
  2886. if actasmtoken <> AS_LBRACKET then
  2887. Message(assem_e_syn_start_with_bracket)
  2888. else
  2889. Begin
  2890. initAsmRef(instr);
  2891. instr.operands[operandnum].ref.segment := findsegment(tempstr);
  2892. BuildBracketExpression(instr,false);
  2893. end;
  2894. end
  2895. { Simple register }
  2896. else if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  2897. Begin
  2898. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_REGISTER]) then
  2899. Message(assem_e_invalid_operand_type);
  2900. instr.operands[operandnum].operandtype := OPR_REGISTER;
  2901. instr.operands[operandnum].reg := findregister(tempstr);
  2902. end
  2903. else
  2904. Message1(assem_e_syn_register,tempstr);
  2905. end;
  2906. { a variable reference, register ref. or a constant reference }
  2907. AS_LBRACKET: Begin
  2908. BuildBracketExpression(instr,false);
  2909. end;
  2910. { Unsupported }
  2911. AS_SEG,AS_OFFSET: Begin
  2912. Message(assem_e_SEG_and_OFFSET_not_supported);
  2913. Consume(actasmtoken);
  2914. { error recovery }
  2915. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  2916. Consume(actasmtoken);
  2917. end;
  2918. AS_SEPARATOR, AS_COMMA: ;
  2919. else
  2920. Message(assem_e_syn_opcode_operand);
  2921. end; { end case }
  2922. end;
  2923. Procedure BuildConstant(maxvalue: longint);
  2924. {*********************************************************************}
  2925. { PROCEDURE BuildConstant }
  2926. { Description: This routine takes care of parsing a DB,DD,or DW }
  2927. { line and adding those to the assembler node. Expressions, range- }
  2928. { checking are fullly taken care of. }
  2929. { maxvalue: $ff -> indicates that this is a DB node. }
  2930. { $ffff -> indicates that this is a DW node. }
  2931. { $ffffffff -> indicates that this is a DD node. }
  2932. {*********************************************************************}
  2933. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  2934. {*********************************************************************}
  2935. var
  2936. strlength: byte;
  2937. expr: string;
  2938. value : longint;
  2939. Begin
  2940. strlength := 0; { assume it is a DB }
  2941. Repeat
  2942. Case actasmtoken of
  2943. AS_STRING: Begin
  2944. if maxvalue = $ffff then
  2945. strlength := 2
  2946. else if maxvalue = $ffffffff then
  2947. strlength := 4;
  2948. if strlength <> 0 then
  2949. { DD and DW cases }
  2950. Begin
  2951. if Not PadZero(actasmpattern,strlength) then
  2952. Message(scan_f_string_exceeds_line);
  2953. end;
  2954. expr := actasmpattern;
  2955. Consume(AS_STRING);
  2956. Case actasmtoken of
  2957. AS_COMMA: Consume(AS_COMMA);
  2958. AS_SEPARATOR: ;
  2959. else
  2960. Message(assem_e_invalid_string_expression);
  2961. end; { end case }
  2962. ConcatString(p,expr);
  2963. end;
  2964. AS_INTNUM,AS_BINNUM,
  2965. AS_OCTALNUM,AS_HEXNUM:
  2966. Begin
  2967. value:=BuildExpression;
  2968. ConcatConstant(p,value,maxvalue);
  2969. end;
  2970. AS_ID:
  2971. Begin
  2972. value:=BuildExpression;
  2973. if value > maxvalue then
  2974. Begin
  2975. Message(assem_e_expression_out_of_bounds);
  2976. { assuming a value of maxvalue }
  2977. value := maxvalue;
  2978. end;
  2979. ConcatConstant(p,value,maxvalue);
  2980. end;
  2981. { These terms can start an assembler expression }
  2982. AS_PLUS,AS_MINUS,AS_LPAREN,AS_NOT: Begin
  2983. value := BuildExpression;
  2984. ConcatConstant(p,value,maxvalue);
  2985. end;
  2986. AS_COMMA: BEGIN
  2987. Consume(AS_COMMA);
  2988. END;
  2989. AS_SEPARATOR: ;
  2990. else
  2991. Begin
  2992. Message(assem_f_internal_error_in_buildconstant);
  2993. end;
  2994. end; { end case }
  2995. Until actasmtoken = AS_SEPARATOR;
  2996. end;
  2997. Procedure BuildOpCode;
  2998. {*********************************************************************}
  2999. { PROCEDURE BuildOpcode; }
  3000. { Description: Parses the intel opcode and operands, and writes it }
  3001. { in the TInstruction object. }
  3002. {*********************************************************************}
  3003. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  3004. { On ENTRY: Token should point to AS_OPCODE }
  3005. {*********************************************************************}
  3006. var
  3007. asmtok,op : tasmop;
  3008. expr : string;
  3009. segreg : tregister;
  3010. Begin
  3011. expr := '';
  3012. asmtok := A_NONE; { assmume no prefix }
  3013. segreg := R_NO; { assume no segment override }
  3014. { prefix seg opcode }
  3015. { prefix opcode }
  3016. if findprefix(actasmpattern,asmtok) then
  3017. Begin
  3018. { standard opcode prefix }
  3019. if asmtok <> A_NONE then
  3020. instr.addprefix(asmtok);
  3021. Consume(AS_OPCODE);
  3022. if findoverride(actasmpattern,segreg) then
  3023. Begin
  3024. Consume(AS_OPCODE);
  3025. Message(assem_w_repeat_prefix_and_seg_override);
  3026. end;
  3027. end
  3028. { seg prefix opcode }
  3029. { seg opcode }
  3030. else if findoverride(actasmpattern,segreg) then
  3031. Begin
  3032. Consume(AS_OPCODE);
  3033. if findprefix(actasmpattern,asmtok) then
  3034. Begin
  3035. { standard opcode prefix }
  3036. Message(assem_w_repeat_prefix_and_seg_override);
  3037. if asmtok <> A_NONE then
  3038. instr.addprefix(asmtok);
  3039. Consume(AS_OPCODE);
  3040. end;
  3041. end;
  3042. { opcode }
  3043. if (actasmtoken <> AS_OPCODE) then
  3044. Begin
  3045. Message(assem_e_invalid_or_missing_opcode);
  3046. { error recovery }
  3047. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA,AS_END]) do
  3048. Consume(actasmtoken);
  3049. exit;
  3050. end
  3051. else
  3052. Begin
  3053. op := findopcode(actasmpattern);
  3054. instr.addinstr(op);
  3055. { Valid combination of prefix and instruction ? }
  3056. if (asmtok <> A_NONE) and (NOT CheckPrefix(asmtok,op)) then
  3057. Message1(assem_e_invalid_prefix_and_opcode,actasmpattern);
  3058. { Valid combination of segment override }
  3059. if (segreg <> R_NO) and (NOT CheckOverride(segreg,instr)) then
  3060. Message1(assem_e_invalid_override_and_opcode,actasmpattern);
  3061. Consume(AS_OPCODE);
  3062. { Zero operand opcode ? }
  3063. if actasmtoken in [AS_END,AS_SEPARATOR] then
  3064. exit
  3065. else
  3066. operandnum := 1;
  3067. end;
  3068. repeat
  3069. case actasmtoken of
  3070. { End of asm operands for this opcode }
  3071. AS_END,
  3072. AS_SEPARATOR :
  3073. break;
  3074. { Operand delimiter }
  3075. AS_COMMA :
  3076. Begin
  3077. if operandnum > MaxOperands then
  3078. Message(assem_e_too_many_operands)
  3079. else
  3080. Inc(operandnum);
  3081. Consume(AS_COMMA);
  3082. end;
  3083. { Typecast, Constant Expression, Type Specifier }
  3084. AS_DWORD,
  3085. AS_BYTE,
  3086. AS_WORD,
  3087. AS_TBYTE,
  3088. AS_QWORD :
  3089. Begin
  3090. { tell that the instruction was overriden }
  3091. { so we will NEVER override the opsize }
  3092. instr.operands[operandnum].overriden := TRUE;
  3093. Case actasmtoken of
  3094. AS_DWORD : instr.operands[operandnum].size := S_L;
  3095. AS_WORD : instr.operands[operandnum].size := S_W;
  3096. AS_BYTE : instr.operands[operandnum].size := S_B;
  3097. AS_QWORD : instr.operands[operandnum].size := S_IQ;
  3098. AS_TBYTE : instr.operands[operandnum].size := S_FX;
  3099. end;
  3100. Consume(actasmtoken);
  3101. Case actasmtoken of
  3102. { Reference }
  3103. AS_PTR :
  3104. Begin
  3105. initAsmRef(instr);
  3106. Consume(AS_PTR);
  3107. BuildOperand(instr);
  3108. end;
  3109. { Possibly a typecast or a constant }
  3110. { expression. }
  3111. AS_LPAREN :
  3112. Begin
  3113. if actasmtoken = AS_ID then
  3114. Begin
  3115. { Case vartype of }
  3116. { LOCAL: Replace by offset and }
  3117. { BP in treference. }
  3118. { GLOBAL: Replace by mangledname}
  3119. { in symbol of treference }
  3120. { Check if next token = RPAREN }
  3121. { otherwise syntax error. }
  3122. initAsmRef(instr);
  3123. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  3124. Message1(assem_e_unknown_id,actasmpattern);
  3125. end
  3126. else
  3127. begin
  3128. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3129. instr.operands[operandnum].val := BuildExpression;
  3130. end;
  3131. end;
  3132. else
  3133. BuildOperand(instr);
  3134. end; { end case }
  3135. end;
  3136. { Type specifier }
  3137. AS_NEAR,
  3138. AS_FAR :
  3139. Begin
  3140. if actasmtoken = AS_NEAR then
  3141. Message(assem_w_near_ignored)
  3142. else
  3143. Message(assem_w_far_ignored);
  3144. Consume(actasmtoken);
  3145. if actasmtoken = AS_PTR then
  3146. begin
  3147. initAsmRef(instr);
  3148. Consume(AS_PTR);
  3149. end;
  3150. BuildOperand(instr);
  3151. end;
  3152. { Constant expression }
  3153. AS_LPAREN :
  3154. Begin
  3155. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3156. instr.operands[operandnum].val := BuildExpression;
  3157. end;
  3158. else
  3159. BuildOperand(instr);
  3160. end; { end case }
  3161. until false;
  3162. end;
  3163. Function Assemble: Ptree;
  3164. {*********************************************************************}
  3165. { PROCEDURE Assemble; }
  3166. { Description: Parses the intel assembler syntax, parsing is done }
  3167. { according to the rules in the Turbo Pascal manual. }
  3168. {*********************************************************************}
  3169. Var
  3170. hl : plabel;
  3171. labelptr : pasmlabel;
  3172. Begin
  3173. Message(assem_d_start_intel);
  3174. inexpression := FALSE;
  3175. firsttoken := TRUE;
  3176. operandnum := 0;
  3177. if assigned(procinfo.retdef) and
  3178. (is_fpu(procinfo.retdef) or
  3179. ret_in_acc(procinfo.retdef)) then
  3180. procinfo.funcret_is_valid:=true;
  3181. { sets up all opcode and register tables in uppercase }
  3182. if not _asmsorted then
  3183. Begin
  3184. SetupTables;
  3185. _asmsorted := TRUE;
  3186. end;
  3187. p:=new(paasmoutput,init);
  3188. { setup label linked list }
  3189. labellist.init;
  3190. c:=current_scanner^.asmgetchar;
  3191. actasmtoken:=gettoken;
  3192. repeat
  3193. case actasmtoken of
  3194. AS_LLABEL :
  3195. Begin
  3196. labelptr := labellist.search(actasmpattern);
  3197. if not assigned(labelptr) then
  3198. Begin
  3199. getlabel(hl);
  3200. labellist.insert(actasmpattern,hl,TRUE);
  3201. ConcatLabel(p,hl);
  3202. end
  3203. else
  3204. { the label has already been inserted into the }
  3205. { label list, either as an intruction label (in }
  3206. { this case it has not been emitted), or as a }
  3207. { duplicate local symbol (in this case it has }
  3208. { already been emitted). }
  3209. Begin
  3210. if labelptr^.emitted then
  3211. Message1(assem_e_dup_local_sym,'@'+labelptr^.name^)
  3212. else
  3213. Begin
  3214. if assigned(labelptr^.lab) then
  3215. ConcatLabel(p,labelptr^.lab);
  3216. labelptr^.emitted := TRUE;
  3217. end;
  3218. end;
  3219. Consume(AS_LLABEL);
  3220. end;
  3221. AS_LABEL :
  3222. Begin
  3223. if SearchLabel(actasmpattern,hl) then
  3224. ConcatLabel(p,hl)
  3225. else
  3226. Message1(assem_e_unknown_label_identifer,actasmpattern);
  3227. Consume(AS_LABEL);
  3228. end;
  3229. AS_DW :
  3230. Begin
  3231. Consume(AS_DW);
  3232. BuildConstant($ffff);
  3233. end;
  3234. AS_DB :
  3235. Begin
  3236. Consume(AS_DB);
  3237. BuildConstant($ff);
  3238. end;
  3239. AS_DD :
  3240. Begin
  3241. Consume(AS_DD);
  3242. BuildConstant($ffffffff);
  3243. end;
  3244. AS_OPCODE :
  3245. Begin
  3246. instr.init;
  3247. BuildOpcode;
  3248. instr.numops := operandnum;
  3249. if instr.labeled then
  3250. ConcatLabeledInstr(instr)
  3251. else
  3252. ConcatOpCode(instr);
  3253. instr.done;
  3254. end;
  3255. AS_SEPARATOR :
  3256. Begin
  3257. Consume(AS_SEPARATOR);
  3258. { let us go back to the first operand }
  3259. operandnum := 0;
  3260. end;
  3261. AS_END :
  3262. break; { end assembly block }
  3263. else
  3264. Begin
  3265. Message(assem_e_assemble_node_syntax_error);
  3266. { error recovery }
  3267. Consume(actasmtoken);
  3268. end;
  3269. end; { end case }
  3270. until false;
  3271. { check if there were undefined symbols. }
  3272. { if so, then list each of those undefined }
  3273. { labels. }
  3274. if assigned(labellist.First) then
  3275. Begin
  3276. labelptr := labellist.First;
  3277. if labellist.First <> nil then
  3278. Begin
  3279. { first label }
  3280. if not labelptr^.emitted then
  3281. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  3282. { other labels ... }
  3283. While (labelptr^.Next <> nil) do
  3284. Begin
  3285. labelptr := labelptr^.Next;
  3286. if not labelptr^.emitted then
  3287. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  3288. end;
  3289. end;
  3290. end;
  3291. assemble := genasmnode(p);
  3292. labellist.done;
  3293. Message(assem_d_finish_intel);
  3294. end;
  3295. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  3296. begin
  3297. if assigned(iasmops) then
  3298. dispose(iasmops);
  3299. exitproc:=old_exit;
  3300. end;
  3301. begin
  3302. old_exit:=exitproc;
  3303. exitproc:=@ra386int_exit;
  3304. end.
  3305. {
  3306. $Log$
  3307. Revision 1.24 1999-03-26 00:05:41 peter
  3308. * released valintern
  3309. + deffile is now removed when compiling is finished
  3310. * ^( compiles now correct
  3311. + static directive
  3312. * shrd fixed
  3313. Revision 1.23 1999/03/02 22:51:08 peter
  3314. * [reg-ofs] now correctly compiles to -ofs(reg) instead of ofs(reg)
  3315. * [reg*2] is now allowed
  3316. Revision 1.22 1999/02/25 21:02:50 peter
  3317. * ag386bin updates
  3318. + coff writer
  3319. Revision 1.21 1999/02/22 02:15:37 peter
  3320. * updates for ag386bin
  3321. Revision 1.20 1999/01/10 15:37:58 peter
  3322. * moved some tables from ra386*.pas -> i386.pas
  3323. + start of coff writer
  3324. * renamed asmutils unit to rautils
  3325. Revision 1.19 1998/12/23 22:55:57 peter
  3326. + rec.field(%esi) support
  3327. + [esi+rec.field] support
  3328. Revision 1.18 1998/12/11 00:03:43 peter
  3329. + globtype,tokens,version unit splitted from globals
  3330. Revision 1.17 1998/12/08 23:03:46 jonas
  3331. * allow constant offsets for go32v2 in assembler blocks
  3332. Revision 1.16 1998/12/02 16:23:36 jonas
  3333. * changed "if longintvar in set" to case or "if () or () .." statements
  3334. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  3335. Revision 1.15 1998/11/29 12:47:22 peter
  3336. * fixes for 'asm sti end;'
  3337. Revision 1.14 1998/11/16 15:38:56 peter
  3338. * fixed instruct not in table msg
  3339. Revision 1.13 1998/11/15 14:37:16 peter
  3340. + support for result in delphimode
  3341. Revision 1.12 1998/11/13 15:40:30 pierre
  3342. + added -Se in Makefile cvstest target
  3343. + lexlevel cleanup
  3344. normal_function_level main_program_level and unit_init_level defined
  3345. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  3346. (test added in code !)
  3347. * -Un option was wrong
  3348. * _FAIL and _SELF only keyword inside
  3349. constructors and methods respectively
  3350. Revision 1.11 1998/11/13 10:12:11 peter
  3351. * constant fixes
  3352. Revision 1.10 1998/11/05 23:48:27 peter
  3353. * recordtype.field support in constant expressions
  3354. * fixed imul for oa_imm8 which was not allowed
  3355. * fixed reading of local typed constants
  3356. * fixed comment reading which is not any longer a separator
  3357. Revision 1.9 1998/10/13 16:50:17 pierre
  3358. * undid some changes of Peter that made the compiler wrong
  3359. for m68k (I had to reinsert some ifdefs)
  3360. * removed several memory leaks under m68k
  3361. * removed the meory leaks for assembler readers
  3362. * cross compiling shoud work again better
  3363. ( crosscompiling sysamiga works
  3364. but as68k still complain about some code !)
  3365. Revision 1.8 1998/10/07 04:29:44 carl
  3366. * Concatlabel now gives output on error
  3367. * in/out bugfix (still ins/outs left to fix)
  3368. Revision 1.7 1998/09/02 01:23:40 carl
  3369. * bugfix of operand overrides, VERY stupid bugfix BTW...
  3370. Revision 1.6 1998/08/27 00:42:17 carl
  3371. * bugfix of leal problem
  3372. * bugfix of using overrides with record offsets
  3373. * bugfix if using records to load values
  3374. Revision 1.5 1998/08/21 08:45:53 pierre
  3375. * better line info for asm statements
  3376. Revision 1.4 1998/07/14 14:47:00 peter
  3377. * released NEWINPUT
  3378. Revision 1.3 1998/07/07 11:20:09 peter
  3379. + NEWINPUT for a better inputfile and scanner object
  3380. Revision 1.2 1998/06/24 14:06:38 peter
  3381. * fixed the name changes
  3382. Revision 1.1 1998/06/23 14:00:18 peter
  3383. * renamed RA* units
  3384. Revision 1.11 1998/06/16 08:56:28 peter
  3385. + targetcpu
  3386. * cleaner pmodules for newppu
  3387. Revision 1.10 1998/06/12 10:32:33 pierre
  3388. * column problem hopefully solved
  3389. + C vars declaration changed
  3390. Revision 1.9 1998/05/31 14:13:32 peter
  3391. * fixed call bugs with assembler readers
  3392. + OPR_SYMBOL to hold a symbol in the asm parser
  3393. * fixed staticsymtable vars which were acessed through %ebp instead of
  3394. name
  3395. Revision 1.8 1998/05/30 14:31:07 peter
  3396. + $ASMMODE
  3397. Revision 1.7 1998/05/28 16:32:05 carl
  3398. * bugfix with operands main branch version (patched manually)
  3399. Revision 1.6 1998/05/23 01:21:26 peter
  3400. + aktasmmode, aktoptprocessor, aktoutputformat
  3401. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  3402. + $LIBNAME to set the library name where the unit will be put in
  3403. * splitted cgi386 a bit (codeseg to large for bp7)
  3404. * nasm, tasm works again. nasm moved to ag386nsm.pas
  3405. Revision 1.5 1998/05/20 09:42:36 pierre
  3406. + UseTokenInfo now default
  3407. * unit in interface uses and implementation uses gives error now
  3408. * only one error for unknown symbol (uses lastsymknown boolean)
  3409. the problem came from the label code !
  3410. + first inlined procedures and function work
  3411. (warning there might be allowed cases were the result is still wrong !!)
  3412. * UseBrower updated gives a global list of all position of all used symbols
  3413. with switch -gb
  3414. Revision 1.4 1998/04/29 10:34:03 pierre
  3415. + added some code for ansistring (not complete nor working yet)
  3416. * corrected operator overloading
  3417. * corrected nasm output
  3418. + started inline procedures
  3419. + added starstarn : use ** for exponentiation (^ gave problems)
  3420. + started UseTokenInfo cond to get accurate positions
  3421. Revision 1.3 1998/04/08 16:58:06 pierre
  3422. * several bugfixes
  3423. ADD ADC and AND are also sign extended
  3424. nasm output OK (program still crashes at end
  3425. and creates wrong assembler files !!)
  3426. procsym types sym in tdef removed !!
  3427. }