ra386int.pas 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624
  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. else
  1617. Message(assem_e_invalid_opcode_and_operand);
  1618. end; { end case }
  1619. end
  1620. else
  1621. if (instruc = A_IMUL) and
  1622. (instr.operands[3].operandtype = OPR_REGISTER) then
  1623. Begin
  1624. case instr.operands[3].size of
  1625. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  1626. Begin
  1627. if instr.operands[1].val <= $ffff then
  1628. Begin
  1629. if instr.operands[2].size in [S_W] then
  1630. Begin
  1631. case instr.operands[2].operandtype of
  1632. OPR_REFERENCE: { MISSING !!!! } ;
  1633. OPR_REGISTER: p^.concat(new(pai386,
  1634. op_const_reg_reg(instruc, S_W,
  1635. instr.operands[1].val, instr.operands[2].reg,
  1636. instr.operands[3].reg)));
  1637. else
  1638. Message(assem_e_invalid_opcode_and_operand);
  1639. end; { end case }
  1640. end
  1641. else
  1642. Message(assem_e_invalid_opcode_and_operand);
  1643. end;
  1644. end
  1645. else
  1646. Message(assem_e_invalid_opcode_and_operand);
  1647. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  1648. Begin
  1649. if instr.operands[1].val <= $7fffffff then
  1650. Begin
  1651. if instr.operands[2].size in [S_L] then
  1652. Begin
  1653. case instr.operands[2].operandtype of
  1654. OPR_REFERENCE: { MISSING !!!! } ;
  1655. OPR_REGISTER: p^.concat(new(pai386,
  1656. op_const_reg_reg(instruc, S_L,
  1657. instr.operands[1].val, instr.operands[2].reg,
  1658. instr.operands[3].reg)));
  1659. else
  1660. Message(assem_e_invalid_opcode_and_operand);
  1661. end; { end case }
  1662. end
  1663. else
  1664. Message(assem_e_invalid_opcode_and_operand);
  1665. end;
  1666. end
  1667. else
  1668. Message(assem_e_invalid_opcode_and_operand);
  1669. else
  1670. Message(assem_e_invalid_middle_sized_operand);
  1671. end; { end case }
  1672. end { endif }
  1673. else
  1674. Message(assem_e_invalid_three_operand_opcode);
  1675. end;
  1676. end; { end case }
  1677. end; { end "if fits then" ... }
  1678. end;
  1679. {---------------------------------------------------------------------}
  1680. { Routines for the parsing }
  1681. {---------------------------------------------------------------------}
  1682. procedure consume(t : tinteltoken);
  1683. begin
  1684. if t<>actasmtoken then
  1685. Message(assem_e_syntax_error);
  1686. actasmtoken:=gettoken;
  1687. { if the token must be ignored, then }
  1688. { get another token to parse. }
  1689. if actasmtoken = AS_NONE then
  1690. actasmtoken := gettoken;
  1691. end;
  1692. function findregister(const s : string): tregister;
  1693. {*********************************************************************}
  1694. { FUNCTION findregister(s: string):tasmop; }
  1695. { Description: Determines if the s string is a valid register, }
  1696. { if so returns correct tregister token, or R_NO if not found. }
  1697. {*********************************************************************}
  1698. var
  1699. i: tregister;
  1700. begin
  1701. findregister := R_NO;
  1702. for i:=firstreg to lastreg do
  1703. if s = iasmregs[i] then
  1704. Begin
  1705. findregister := i;
  1706. exit;
  1707. end;
  1708. end;
  1709. function findoverride(const s: string; var reg:tregister): boolean;
  1710. var
  1711. i: byte;
  1712. begin
  1713. findoverride := FALSE;
  1714. reg := R_NO;
  1715. for i:=0 to _count_asmoverrides do
  1716. Begin
  1717. if s = _asmoverrides[i] then
  1718. begin
  1719. reg := _overridetokens[i];
  1720. findoverride := TRUE;
  1721. exit;
  1722. end;
  1723. end;
  1724. end;
  1725. function findprefix(const s: string; var token: tasmop): boolean;
  1726. var i: byte;
  1727. Begin
  1728. findprefix := FALSE;
  1729. for i:=0 to _count_asmprefixes do
  1730. Begin
  1731. if s = _asmprefixes[i] then
  1732. begin
  1733. token := _prefixtokens[i];
  1734. findprefix := TRUE;
  1735. exit;
  1736. end;
  1737. end;
  1738. end;
  1739. function findsegment(const s:string): tregister;
  1740. {*********************************************************************}
  1741. { FUNCTION findsegment(s: string):tasmop; }
  1742. { Description: Determines if the s string is a valid segment register}
  1743. { if so returns correct tregister token, or R_NO if not found. }
  1744. {*********************************************************************}
  1745. var
  1746. i: tregister;
  1747. Begin
  1748. findsegment := R_DEFAULT_SEG;
  1749. for i:=firstsreg to lastsreg do
  1750. if s = iasmregs[i] then
  1751. Begin
  1752. findsegment := i;
  1753. exit;
  1754. end;
  1755. end;
  1756. function findopcode(const s: string): tasmop;
  1757. {*********************************************************************}
  1758. { FUNCTION findopcode(s: string): tasmop; }
  1759. { Description: Determines if the s string is a valid opcode }
  1760. { if so returns correct tasmop token. }
  1761. {*********************************************************************}
  1762. var
  1763. i: tasmop;
  1764. j: byte;
  1765. Begin
  1766. findopcode := A_NONE;
  1767. for i:=firstop to lastop do
  1768. if s = iasmops^[i] then
  1769. begin
  1770. findopcode:=i;
  1771. exit;
  1772. end;
  1773. { not found yet, search for extended opcodes }
  1774. { now, in this case, we must use the suffix }
  1775. { to determine the size of the instruction }
  1776. for j:=0 to _count_asmspecialops do
  1777. Begin
  1778. if s = _specialops[j] then
  1779. Begin
  1780. findopcode := _specialopstokens[j];
  1781. { set the size }
  1782. case s[length(s)] of
  1783. 'B': instr.stropsize := S_B;
  1784. 'D': instr.stropsize := S_L;
  1785. 'W': instr.stropsize := S_W;
  1786. end;
  1787. exit;
  1788. end;
  1789. end;
  1790. end;
  1791. Function CheckPrefix(prefix: tasmop; opcode:tasmop): Boolean;
  1792. { Checks if the prefix is valid with the following instruction }
  1793. { return false if not, otherwise true }
  1794. Begin
  1795. CheckPrefix := TRUE;
  1796. Case prefix of
  1797. A_REP,A_REPNE,A_REPE:
  1798. Case opcode Of
  1799. A_SCAS,A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  1800. Else
  1801. Begin
  1802. CheckPrefix := FALSE;
  1803. exit;
  1804. end;
  1805. end; { case }
  1806. A_LOCK:
  1807. Case opcode Of
  1808. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  1809. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  1810. Else
  1811. Begin
  1812. CheckPrefix := FALSE;
  1813. Exit;
  1814. end;
  1815. end; { case }
  1816. A_NONE: exit; { no prefix here }
  1817. else
  1818. CheckPrefix := FALSE;
  1819. end; { end case }
  1820. end;
  1821. Procedure InitAsmRef(var instr: TInstruction);
  1822. {*********************************************************************}
  1823. { Description: This routine first check if the instruction is of }
  1824. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  1825. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  1826. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  1827. { to point to the default segment. }
  1828. {*********************************************************************}
  1829. Begin
  1830. With instr do
  1831. Begin
  1832. case operands[operandnum].operandtype of
  1833. OPR_REFERENCE: exit;
  1834. OPR_NONE: ;
  1835. else
  1836. Message(assem_e_invalid_operand_type);
  1837. end;
  1838. operands[operandnum].operandtype := OPR_REFERENCE;
  1839. operands[operandnum].ref.segment := R_DEFAULT_SEG;
  1840. end;
  1841. end;
  1842. Function CheckOverride(segreg: tregister; var instr: TInstruction): Boolean;
  1843. { Check if the override is valid, and if so then }
  1844. { update the instr variable accordingly. }
  1845. Begin
  1846. CheckOverride := FALSE;
  1847. Case instr.getinstruction of
  1848. A_MOVS,A_XLAT,A_CMPS:
  1849. Begin
  1850. CheckOverride := TRUE;
  1851. Message(assem_e_segment_override_not_supported);
  1852. end
  1853. end;
  1854. End;
  1855. Procedure GetRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  1856. {*********************************************************************}
  1857. { PROCEDURE GetRecordOffsetSize }
  1858. { Description: This routine builds up a record offset after a AS_DOT }
  1859. { token is encountered. }
  1860. { On entry actasmtoken should be equal to AS_DOT }
  1861. {*********************************************************************}
  1862. { EXIT CONDITION: On exit the routine should point to either the }
  1863. { ERROR RECOVER: read until AS_COMMA or AS_SEPARATOR token. }
  1864. { Warning: This is called recursively. }
  1865. {*********************************************************************}
  1866. var
  1867. toffset,tsize : longint;
  1868. Begin
  1869. offset:=0;
  1870. size:=0;
  1871. Consume(AS_DOT);
  1872. if actasmtoken = AS_ID then
  1873. Begin
  1874. if not GetTypeOffsetSize(expr,actasmpattern,toffset,tsize) and
  1875. not GetVarOffsetSize(expr,actasmpattern,toffset,tsize) then
  1876. begin
  1877. Message(assem_e_syntax_error);
  1878. toffset:=0;
  1879. tsize:=0;
  1880. end;
  1881. inc(offset,toffset);
  1882. size:=tsize;
  1883. Consume(AS_ID);
  1884. if actasmtoken=AS_DOT then
  1885. begin
  1886. GetRecordOffsetSize(expr,toffset,tsize);
  1887. inc(offset,toffset);
  1888. size:=tsize;
  1889. end;
  1890. end
  1891. else
  1892. Begin
  1893. Message(assem_e_syntax_error);
  1894. repeat
  1895. consume(actasmtoken)
  1896. until (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA);
  1897. end;
  1898. end;
  1899. Function BuildRefExpression: longint;
  1900. {*********************************************************************}
  1901. { FUNCTION BuildExpression: longint }
  1902. { Description: This routine calculates a constant expression to }
  1903. { a given value. The return value is the value calculated from }
  1904. { the expression. }
  1905. { The following tokens (not strings) are recognized: }
  1906. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  1907. {*********************************************************************}
  1908. { ENTRY: On entry the token should be any valid expression token. }
  1909. { EXIT: On Exit the token points to any token after the closing }
  1910. { RBRACKET }
  1911. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  1912. { invalid tokens. }
  1913. {*********************************************************************}
  1914. var
  1915. tempstr,expr : string;
  1916. l,k : longint;
  1917. errorflag : boolean;
  1918. Begin
  1919. errorflag := FALSE;
  1920. tempstr := '';
  1921. expr := '';
  1922. { tell tokenizer that we are in }
  1923. { an expression. }
  1924. inexpression := TRUE;
  1925. Repeat
  1926. Case actasmtoken of
  1927. AS_LPAREN:
  1928. Begin
  1929. Consume(AS_LPAREN);
  1930. expr := expr + '(';
  1931. end;
  1932. AS_RPAREN:
  1933. Begin
  1934. Consume(AS_RPAREN);
  1935. expr := expr + ')';
  1936. end;
  1937. AS_SHL:
  1938. Begin
  1939. Consume(AS_SHL);
  1940. expr := expr + '<';
  1941. end;
  1942. AS_SHR:
  1943. Begin
  1944. Consume(AS_SHR);
  1945. expr := expr + '>';
  1946. end;
  1947. AS_SLASH:
  1948. Begin
  1949. Consume(AS_SLASH);
  1950. expr := expr + '/';
  1951. end;
  1952. AS_MOD:
  1953. Begin
  1954. Consume(AS_MOD);
  1955. expr := expr + '%';
  1956. end;
  1957. AS_STAR:
  1958. Begin
  1959. Consume(AS_STAR);
  1960. expr := expr + '*';
  1961. end;
  1962. AS_PLUS:
  1963. Begin
  1964. Consume(AS_PLUS);
  1965. expr := expr + '+';
  1966. end;
  1967. AS_MINUS:
  1968. Begin
  1969. Consume(AS_MINUS);
  1970. expr := expr + '-';
  1971. end;
  1972. AS_AND:
  1973. Begin
  1974. Consume(AS_AND);
  1975. expr := expr + '&';
  1976. end;
  1977. AS_NOT:
  1978. Begin
  1979. Consume(AS_NOT);
  1980. expr := expr + '~';
  1981. end;
  1982. AS_XOR:
  1983. Begin
  1984. Consume(AS_XOR);
  1985. expr := expr + '^';
  1986. end;
  1987. AS_OR:
  1988. Begin
  1989. Consume(AS_OR);
  1990. expr := expr + '|';
  1991. end;
  1992. AS_INTNUM:
  1993. Begin
  1994. expr := expr + actasmpattern;
  1995. Consume(AS_INTNUM);
  1996. end;
  1997. AS_BINNUM:
  1998. Begin
  1999. expr:=expr+BinaryToDec(actasmpattern);
  2000. Consume(AS_BINNUM);
  2001. end;
  2002. AS_HEXNUM:
  2003. Begin
  2004. expr:=expr+HexToDec(actasmpattern);
  2005. Consume(AS_HEXNUM);
  2006. end;
  2007. AS_OCTALNUM:
  2008. Begin
  2009. expr:=expr+OctalToDec(actasmpattern);
  2010. Consume(AS_OCTALNUM);
  2011. end;
  2012. AS_ID:
  2013. Begin
  2014. tempstr:=actasmpattern;
  2015. consume(AS_ID);
  2016. if actasmtoken=AS_DOT then
  2017. begin
  2018. GetRecordOffsetSize(tempstr,l,k);
  2019. str(l, tempstr);
  2020. expr := expr + tempstr;
  2021. end
  2022. else
  2023. begin
  2024. if SearchIConstant(tempstr,l) then
  2025. begin
  2026. str(l, tempstr);
  2027. expr := expr + tempstr;
  2028. end
  2029. else
  2030. Message1(assem_e_invalid_const_symbol,tempstr);
  2031. end;
  2032. end;
  2033. AS_RBRACKET: { End of reference }
  2034. Begin
  2035. if not ErrorFlag then
  2036. BuildRefExpression := CalculateExpression(expr)
  2037. else
  2038. BuildRefExpression := 0;
  2039. Consume(AS_RBRACKET);
  2040. { no longer in an expression }
  2041. inexpression := FALSE;
  2042. exit;
  2043. end;
  2044. else
  2045. Begin
  2046. { write error only once. }
  2047. if not errorflag then
  2048. Message(assem_e_invalid_constant_expression);
  2049. BuildRefExpression := 0;
  2050. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then
  2051. exit;
  2052. { consume tokens until we find COMMA or SEPARATOR }
  2053. Consume(actasmtoken);
  2054. errorflag := TRUE;
  2055. end;
  2056. end;
  2057. Until false;
  2058. end;
  2059. Procedure BuildRecordOffset(var instr: TInstruction; varname: string);
  2060. {*********************************************************************}
  2061. { PROCEDURE BuildRecordOffset(var Instr: TInstruction) }
  2062. { Description: This routine takes care of field specifiers of records }
  2063. { and/or variables in asm operands. It updates the offset accordingly}
  2064. {*********************************************************************}
  2065. { ENTRY: On entry the token should be DOT. }
  2066. { name: should be the name of the variable to be expanded. '' if }
  2067. { no variabled specified. }
  2068. { EXIT: On Exit the token points to SEPARATOR or COMMA. }
  2069. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2070. { invalid tokens. }
  2071. {*********************************************************************}
  2072. var
  2073. firstpass: boolean;
  2074. offset: longint;
  2075. tsize,toffset : longint;
  2076. basetypename : string;
  2077. Begin
  2078. basetypename := '';
  2079. firstpass := TRUE;
  2080. { .ID[REG].ID ... }
  2081. { .ID.ID... }
  2082. Consume(AS_DOT);
  2083. Repeat
  2084. case actasmtoken of
  2085. AS_ID: Begin
  2086. { we must reset the operand size - since only the last field }
  2087. { will give us the size of the operand. }
  2088. { instr.opsize := S_NO;}
  2089. InitAsmRef(instr);
  2090. { var_name.typefield.typefield }
  2091. if (varname <> '') then
  2092. Begin
  2093. if GetVarOffsetSize(varname,actasmpattern,toffset,tsize) then
  2094. Begin
  2095. Inc(instr.operands[operandnum].ref.offset,tOffset);
  2096. SetOperandSize(instr,operandnum,tsize);
  2097. end
  2098. else
  2099. Message1(assem_e_unknown_id,actasmpattern);
  2100. end
  2101. else
  2102. { [ref].var_name.typefield.typefield ... }
  2103. { [ref].var_name[reg] }
  2104. if not assigned(instr.operands[operandnum].ref.symbol) and
  2105. firstpass then
  2106. Begin
  2107. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2108. Begin
  2109. { type field ? }
  2110. basetypename := actasmpattern;
  2111. end
  2112. else
  2113. varname := actasmpattern;
  2114. end
  2115. else
  2116. if firstpass then
  2117. { [ref].typefield.typefield ... }
  2118. { where the first typefield must specifiy the base }
  2119. { object or record type. }
  2120. Begin
  2121. basetypename := actasmpattern;
  2122. end
  2123. else
  2124. { [ref].typefield.typefield ... }
  2125. { basetpyename is already set up... now look for fields. }
  2126. Begin
  2127. if GetTypeOffsetSize(basetypename,actasmpattern,tOffset,Tsize) then
  2128. Begin
  2129. Inc(instr.operands[operandnum].ref.offset,tOffset);
  2130. SetOperandSize(instr,operandnum,Tsize);
  2131. end
  2132. else
  2133. Message1(assem_e_unknown_id,actasmpattern);
  2134. end;
  2135. Consume(AS_ID);
  2136. { Take care of index register on this variable }
  2137. if actasmtoken = AS_LBRACKET then
  2138. Begin
  2139. Consume(AS_LBRACKET);
  2140. Case actasmtoken of
  2141. AS_REGISTER: Begin
  2142. if instr.operands[operandnum].ref.index <> R_NO then
  2143. Message(assem_e_defining_index_more_than_once);
  2144. instr.operands[operandnum].ref.index :=
  2145. findregister(actasmpattern);
  2146. Consume(AS_REGISTER);
  2147. end;
  2148. else
  2149. Begin
  2150. { add offsets , assuming these are constant expressions... }
  2151. Inc(instr.operands[operandnum].ref.offset,BuildRefExpression);
  2152. end;
  2153. end;
  2154. Consume(AS_RBRACKET);
  2155. end;
  2156. { Here we should either have AS_DOT, AS_SEPARATOR or AS_COMMA }
  2157. if actasmtoken = AS_DOT then
  2158. Consume(AS_DOT);
  2159. firstpass := FALSE;
  2160. Offset := 0;
  2161. end;
  2162. AS_SEPARATOR: exit;
  2163. AS_COMMA: exit;
  2164. else
  2165. Begin
  2166. Message(assem_e_invalid_field_specifier);
  2167. Consume(actasmtoken);
  2168. firstpass := FALSE;
  2169. end;
  2170. end; { end case }
  2171. Until (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA);
  2172. end;
  2173. Function BuildExpression: longint;
  2174. {*********************************************************************}
  2175. { FUNCTION BuildExpression: longint }
  2176. { Description: This routine calculates a constant expression to }
  2177. { a given value. The return value is the value calculated from }
  2178. { the expression. }
  2179. { The following tokens (not strings) are recognized: }
  2180. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  2181. {*********************************************************************}
  2182. { ENTRY: On entry the token should be any valid expression token. }
  2183. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  2184. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2185. { invalid tokens. }
  2186. {*********************************************************************}
  2187. var expr: string;
  2188. tempstr: string;
  2189. l,k : longint;
  2190. errorflag: boolean;
  2191. Begin
  2192. errorflag := FALSE;
  2193. expr := '';
  2194. tempstr := '';
  2195. { tell tokenizer that we are in an expression. }
  2196. inexpression := TRUE;
  2197. Repeat
  2198. Case actasmtoken of
  2199. AS_LPAREN: Begin
  2200. Consume(AS_LPAREN);
  2201. expr := expr + '(';
  2202. end;
  2203. AS_RPAREN: Begin
  2204. Consume(AS_RPAREN);
  2205. expr := expr + ')';
  2206. end;
  2207. AS_SHL: Begin
  2208. Consume(AS_SHL);
  2209. expr := expr + '<';
  2210. end;
  2211. AS_SHR: Begin
  2212. Consume(AS_SHR);
  2213. expr := expr + '>';
  2214. end;
  2215. AS_SLASH: Begin
  2216. Consume(AS_SLASH);
  2217. expr := expr + '/';
  2218. end;
  2219. AS_MOD: Begin
  2220. Consume(AS_MOD);
  2221. expr := expr + '%';
  2222. end;
  2223. AS_STAR: Begin
  2224. Consume(AS_STAR);
  2225. expr := expr + '*';
  2226. end;
  2227. AS_PLUS: Begin
  2228. Consume(AS_PLUS);
  2229. expr := expr + '+';
  2230. end;
  2231. AS_MINUS: Begin
  2232. Consume(AS_MINUS);
  2233. expr := expr + '-';
  2234. end;
  2235. AS_AND: Begin
  2236. Consume(AS_AND);
  2237. expr := expr + '&';
  2238. end;
  2239. AS_NOT: Begin
  2240. Consume(AS_NOT);
  2241. expr := expr + '~';
  2242. end;
  2243. AS_XOR: Begin
  2244. Consume(AS_XOR);
  2245. expr := expr + '^';
  2246. end;
  2247. AS_OR: Begin
  2248. Consume(AS_OR);
  2249. expr := expr + '|';
  2250. end;
  2251. AS_ID: Begin
  2252. tempstr:=actasmpattern;
  2253. consume(AS_ID);
  2254. if actasmtoken=AS_DOT then
  2255. begin
  2256. GetRecordOffsetSize(tempstr,l,k);
  2257. str(l, tempstr);
  2258. expr := expr + tempstr;
  2259. end
  2260. else
  2261. begin
  2262. if SearchIConstant(tempstr,l) then
  2263. begin
  2264. str(l, tempstr);
  2265. expr := expr + tempstr;
  2266. end
  2267. else
  2268. Message1(assem_e_invalid_const_symbol,actasmpattern);
  2269. end;
  2270. end;
  2271. AS_INTNUM: Begin
  2272. expr := expr + actasmpattern;
  2273. Consume(AS_INTNUM);
  2274. end;
  2275. AS_BINNUM: Begin
  2276. tempstr := BinaryToDec(actasmpattern);
  2277. if tempstr = '' then
  2278. Message(assem_f_error_converting_bin);
  2279. expr:=expr+tempstr;
  2280. Consume(AS_BINNUM);
  2281. end;
  2282. AS_HEXNUM: Begin
  2283. tempstr := HexToDec(actasmpattern);
  2284. if tempstr = '' then
  2285. Message(assem_f_error_converting_hex);
  2286. expr:=expr+tempstr;
  2287. Consume(AS_HEXNUM);
  2288. end;
  2289. AS_OCTALNUM: Begin
  2290. tempstr := OctalToDec(actasmpattern);
  2291. if tempstr = '' then
  2292. Message(assem_f_error_converting_octal);
  2293. expr:=expr+tempstr;
  2294. Consume(AS_OCTALNUM);
  2295. end;
  2296. { go to next term }
  2297. AS_COMMA: Begin
  2298. if not ErrorFlag then
  2299. BuildExpression := CalculateExpression(expr)
  2300. else
  2301. BuildExpression := 0;
  2302. inexpression := FALSE;
  2303. Exit;
  2304. end;
  2305. { go to next symbol }
  2306. AS_SEPARATOR: Begin
  2307. if not ErrorFlag then
  2308. BuildExpression := CalculateExpression(expr)
  2309. else
  2310. BuildExpression := 0;
  2311. inexpression := FALSE;
  2312. Exit;
  2313. end;
  2314. else
  2315. Begin
  2316. { only write error once. }
  2317. if not errorflag then
  2318. Message(assem_e_invalid_constant_expression);
  2319. { consume tokens until we find COMMA or SEPARATOR }
  2320. Consume(actasmtoken);
  2321. errorflag := TRUE;
  2322. End;
  2323. end;
  2324. Until false;
  2325. end;
  2326. Procedure BuildScaling(Var instr: TInstruction);
  2327. {*********************************************************************}
  2328. { Takes care of parsing expression starting from the scaling value }
  2329. { up to and including possible field specifiers. }
  2330. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR }
  2331. { or AS_COMMA. On entry should point to AS_STAR token. }
  2332. {*********************************************************************}
  2333. var str:string;
  2334. l: longint;
  2335. code: integer;
  2336. Begin
  2337. Consume(AS_STAR);
  2338. if (instr.operands[operandnum].ref.scalefactor <> 0)
  2339. and (instr.operands[operandnum].ref.scalefactor <> 1) then
  2340. Begin
  2341. Message(assem_f_internal_error_in_buildscale);
  2342. end;
  2343. case actasmtoken of
  2344. AS_INTNUM: str := actasmpattern;
  2345. AS_HEXNUM: str := HexToDec(actasmpattern);
  2346. AS_BINNUM: str := BinaryToDec(actasmpattern);
  2347. AS_OCTALNUM: str := OctalToDec(actasmpattern);
  2348. else
  2349. Message(assem_e_syntax_error);
  2350. end;
  2351. val(str, l, code);
  2352. if code <> 0 then
  2353. Message(assem_e_invalid_scaling_factor);
  2354. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) and (code = 0) then
  2355. begin
  2356. instr.operands[operandnum].ref.scalefactor := l;
  2357. end
  2358. else
  2359. Begin
  2360. Message(assem_e_invalid_scaling_value);
  2361. instr.operands[operandnum].ref.scalefactor := 0;
  2362. end;
  2363. if instr.operands[operandnum].ref.index = R_NO then
  2364. Begin
  2365. Message(assem_e_scaling_value_only_allowed_with_index);
  2366. instr.operands[operandnum].ref.scalefactor := 0;
  2367. end;
  2368. { Consume the scaling number }
  2369. Consume(actasmtoken);
  2370. case actasmtoken of
  2371. { [...*SCALING-expr] ... }
  2372. AS_MINUS: Begin
  2373. if instr.operands[operandnum].ref.offset <> 0 then
  2374. Message(assem_f_internal_error_in_buildscale);
  2375. instr.operands[operandnum].ref.offset :=
  2376. BuildRefExpression;
  2377. end;
  2378. { [...*SCALING+expr] ... }
  2379. AS_PLUS: Begin
  2380. if instr.operands[operandnum].ref.offset <> 0 then
  2381. Message(assem_f_internal_error_in_buildscale);
  2382. instr.operands[operandnum].ref.offset :=
  2383. BuildRefExpression;
  2384. end;
  2385. { [...*SCALING] ... }
  2386. AS_RBRACKET: Consume(AS_RBRACKET);
  2387. else
  2388. Message(assem_e_invalid_scaling_value);
  2389. end;
  2390. { .Field.Field ... or separator/comma }
  2391. Case actasmtoken of
  2392. AS_DOT: BuildRecordOffset(instr,'');
  2393. AS_COMMA, AS_SEPARATOR: ;
  2394. else
  2395. Message(assem_e_syntax_error);
  2396. end;
  2397. end;
  2398. Procedure BuildReference(var instr: TInstruction);
  2399. {*********************************************************************}
  2400. { EXIT CONDITION: On exit the routine should point to either the }
  2401. { AS_COMMA or AS_SEPARATOR token. }
  2402. { On entry: contains the register after the opening bracket if any. }
  2403. {*********************************************************************}
  2404. var
  2405. reg:string;
  2406. segreg: boolean;
  2407. negative: boolean;
  2408. expr: string;
  2409. Begin
  2410. expr := '';
  2411. if instr.operands[operandnum].operandtype <> OPR_REFERENCE then
  2412. Begin
  2413. Message(assem_e_syn_no_ref_with_brackets);
  2414. InitAsmRef(instr);
  2415. consume(AS_REGISTER);
  2416. end
  2417. else
  2418. Begin
  2419. { save the reg }
  2420. reg := actasmpattern;
  2421. consume(AS_REGISTER);
  2422. { is the syntax of the form: [REG:REG...] }
  2423. if actasmtoken = AS_COLON then
  2424. begin
  2425. segreg := TRUE;
  2426. Message(assem_e_expression_form_not_supported);
  2427. if instr.operands[operandnum].ref.segment <> R_NO then
  2428. Message(assem_e_defining_seg_more_than_once);
  2429. instr.operands[operandnum].ref.segment := findsegment(reg);
  2430. { Here we should process the syntax of the form }
  2431. { [reg:reg...] }
  2432. end
  2433. else { SREG:[REG...] where SReg: is optional. }
  2434. Begin
  2435. if actasmtoken=AS_STAR then
  2436. begin
  2437. if instr.operands[operandnum].ref.index <> R_NO then
  2438. Message(assem_e_defining_index_more_than_once);
  2439. instr.operands[operandnum].ref.index := findregister(reg);
  2440. end
  2441. else
  2442. begin
  2443. if instr.operands[operandnum].ref.base <> R_NO then
  2444. Message(assem_e_defining_base_more_than_once);
  2445. instr.operands[operandnum].ref.base := findregister(reg);
  2446. end;
  2447. end;
  2448. { we process this type of syntax immediately... }
  2449. case actasmtoken of
  2450. { SREG:[REG].Field.Field ... }
  2451. { SREG:[REG].Field[REG].Field... }
  2452. AS_RBRACKET:
  2453. Begin
  2454. Consume(AS_RBRACKET);
  2455. { check for record fields }
  2456. if actasmtoken = AS_DOT then
  2457. BuildRecordOffset(instr,'');
  2458. if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  2459. exit
  2460. else
  2461. Message(assem_e_syn_reference);
  2462. end;
  2463. { SREG:[REG +/- ...].Field.Field ... }
  2464. AS_PLUS,
  2465. AS_MINUS:
  2466. Begin
  2467. if actasmtoken = AS_MINUS then
  2468. Begin
  2469. expr := '-';
  2470. negative := TRUE
  2471. end
  2472. else
  2473. Begin
  2474. negative := FALSE;
  2475. expr := '+';
  2476. end;
  2477. Consume(actasmtoken);
  2478. { REG:[REG+REG+/-...].Field.Field }
  2479. if actasmtoken = AS_REGISTER then
  2480. Begin
  2481. if negative then
  2482. Message(assem_e_negative_index_register);
  2483. if instr.operands[operandnum].ref.index <> R_NO then
  2484. Message(assem_e_defining_index_more_than_once);
  2485. instr.operands[operandnum].ref.index := findregister(actasmpattern);
  2486. Consume(AS_REGISTER);
  2487. case actasmtoken of
  2488. AS_RBRACKET: { SREG:[REG+REG].Field.Field... }
  2489. Begin
  2490. Consume(AS_RBRACKET);
  2491. Case actasmtoken of
  2492. AS_DOT: BuildRecordOffset(instr,'');
  2493. AS_COMMA,
  2494. AS_SEPARATOR: exit;
  2495. else
  2496. Message(assem_e_syntax_error);
  2497. end;
  2498. end;
  2499. AS_PLUS,
  2500. AS_MINUS: { REG:[REG+REG+/-expr... }
  2501. Begin
  2502. if instr.operands[operandnum].ref.offset <> 0 then
  2503. Message(assem_f_internal_error_in_buildreference);
  2504. instr.operands[operandnum].ref.offset:=BuildRefExpression;
  2505. case actasmtoken of
  2506. AS_DOT: BuildRecordOffset(instr,'');
  2507. AS_COMMA,
  2508. AS_SEPARATOR: ;
  2509. else
  2510. Message(assem_e_syntax_error);
  2511. end;
  2512. end;
  2513. AS_STAR: { REG:[REG+REG*SCALING...].Field.Field... }
  2514. begin
  2515. BuildScaling(instr);
  2516. end;
  2517. else
  2518. Message(assem_e_syntax_error);
  2519. end; { end case }
  2520. end
  2521. else
  2522. { REG:[REG*(+/-)SCALING ... ] }
  2523. if actasmtoken = AS_STAR then
  2524. Begin
  2525. BuildScaling(instr);
  2526. end
  2527. else
  2528. { REG:[REG+expr].Field.Field }
  2529. Begin
  2530. if instr.operands[operandnum].ref.offset <> 0 then
  2531. Message(assem_f_internal_error_in_buildreference);
  2532. if negative then
  2533. instr.operands[operandnum].ref.offset := -BuildRefExpression
  2534. else
  2535. instr.operands[operandnum].ref.offset := BuildRefExpression;
  2536. case actasmtoken of
  2537. AS_DOT: BuildRecordOffset(instr,'');
  2538. AS_COMMA,
  2539. AS_SEPARATOR: ;
  2540. else
  2541. Message(assem_e_syntax_error);
  2542. end;
  2543. end; { end if }
  2544. end; { end this case }
  2545. AS_STAR: { REG:[REG*scaling] ... }
  2546. Begin
  2547. BuildScaling(instr);
  2548. end;
  2549. end;
  2550. end; { end outer if }
  2551. end;
  2552. Procedure BuildBracketExpression(var Instr: TInstruction; var_prefix: boolean);
  2553. {*********************************************************************}
  2554. { PROCEDURE BuildBracketExpression }
  2555. { Description: This routine builds up an expression after a LBRACKET }
  2556. { token is encountered. }
  2557. { On entry actasmtoken should be equal to AS_LBRACKET. }
  2558. { var_prefix : Should be set to true if variable identifier has }
  2559. { been defined, such as in ID[ }
  2560. {*********************************************************************}
  2561. { EXIT CONDITION: On exit the routine should point to either the }
  2562. { AS_COMMA or AS_SEPARATOR token. }
  2563. {*********************************************************************}
  2564. var
  2565. l:longint;
  2566. Begin
  2567. Consume(AS_LBRACKET);
  2568. initAsmRef(instr);
  2569. Case actasmtoken of
  2570. { Constant reference expression OR variable reference expression }
  2571. AS_ID: Begin
  2572. if actasmpattern[1] = '@' then
  2573. Message(assem_e_local_symbol_not_allowed_as_ref);
  2574. if SearchIConstant(actasmpattern,l) then
  2575. Begin
  2576. { if there was a variable prefix then }
  2577. { add to offset }
  2578. If var_prefix then
  2579. Begin
  2580. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2581. end
  2582. else
  2583. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2584. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2585. Message(assem_e_invalid_operand_in_bracket_expression);
  2586. end
  2587. else if NOT var_prefix then
  2588. Begin
  2589. InitAsmRef(instr);
  2590. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2591. Message1(assem_e_unknown_id,actasmpattern);
  2592. Consume(AS_ID);
  2593. { is there a constant expression following }
  2594. { the variable name? }
  2595. if actasmtoken <> AS_RBRACKET then
  2596. Begin
  2597. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2598. end
  2599. else
  2600. Consume(AS_RBRACKET);
  2601. end
  2602. else
  2603. Message1(assem_e_invalid_symbol_name,actasmpattern);
  2604. end;
  2605. { Here we handle the special case in tp where }
  2606. { the + operator is allowed with reg and var }
  2607. { references, such as in mov al, byte ptr [+bx] }
  2608. AS_PLUS: Begin
  2609. Consume(AS_PLUS);
  2610. Case actasmtoken of
  2611. AS_REGISTER: Begin
  2612. BuildReference(instr);
  2613. end;
  2614. AS_ID: Begin
  2615. if actasmpattern[1] = '@' then
  2616. Message(assem_e_local_symbol_not_allowed_as_ref);
  2617. if SearchIConstant(actasmpattern,l) then
  2618. Begin
  2619. { if there was a variable prefix then }
  2620. { add to offset }
  2621. If var_prefix then
  2622. Begin
  2623. Inc(instr.operands[operandnum].ref.offset,
  2624. BuildRefExpression);
  2625. end
  2626. else
  2627. instr.operands[operandnum].ref.offset :=
  2628. BuildRefExpression;
  2629. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2630. Message(assem_e_invalid_operand_in_bracket_expression);
  2631. end
  2632. else if NOT var_prefix then
  2633. Begin
  2634. InitAsmRef(instr);
  2635. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2636. Message1(assem_e_unknown_id,actasmpattern);
  2637. Consume(AS_ID);
  2638. { is there a constant expression following }
  2639. { the variable name? }
  2640. if actasmtoken <> AS_RBRACKET then
  2641. Begin
  2642. Inc(instr.operands[operandnum].ref.offset,
  2643. BuildRefExpression);
  2644. end
  2645. else
  2646. Consume(AS_RBRACKET);
  2647. end
  2648. else
  2649. Message1(assem_e_invalid_symbol_name,actasmpattern);
  2650. end;
  2651. { Constant reference expression // }
  2652. AS_INTNUM,AS_BINNUM,AS_OCTALNUM,
  2653. AS_HEXNUM: Begin
  2654. { if there was a variable prefix then }
  2655. { add to offset instead. }
  2656. If var_prefix then
  2657. Begin
  2658. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2659. end
  2660. else
  2661. Begin
  2662. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2663. end;
  2664. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2665. Message(assem_e_invalid_operand_in_bracket_expression);
  2666. end;
  2667. else
  2668. Message(assem_e_syntax_error);
  2669. end;
  2670. end;
  2671. { Constant reference expression // }
  2672. AS_MINUS,AS_NOT,AS_LPAREN:
  2673. Begin
  2674. { if there was a variable prefix then }
  2675. { add to offset instead. }
  2676. If var_prefix then
  2677. Begin
  2678. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2679. end
  2680. else
  2681. Begin
  2682. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2683. end;
  2684. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2685. Message(assem_e_invalid_operand_in_bracket_expression);
  2686. end;
  2687. { Constant reference expression // }
  2688. AS_INTNUM,AS_OCTALNUM,AS_BINNUM,AS_HEXNUM: Begin
  2689. { if there was a variable prefix then }
  2690. { add to offset instead. }
  2691. If var_prefix then
  2692. Begin
  2693. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2694. end
  2695. else
  2696. Begin
  2697. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2698. end;
  2699. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2700. Message(assem_e_invalid_operand_in_bracket_expression);
  2701. end;
  2702. { Variable reference expression }
  2703. AS_REGISTER: BuildReference(instr);
  2704. else
  2705. Begin
  2706. Message(assem_e_invalid_reference_syntax);
  2707. while (actasmtoken <> AS_SEPARATOR) do
  2708. Consume(actasmtoken);
  2709. end;
  2710. end; { end case }
  2711. end;
  2712. Procedure BuildOperand(var instr: TInstruction);
  2713. {*********************************************************************}
  2714. { EXIT CONDITION: On exit the routine should point to either the }
  2715. { AS_COMMA or AS_SEPARATOR token. }
  2716. {*********************************************************************}
  2717. var
  2718. tempstr: string;
  2719. expr: string;
  2720. lab: Pasmlabel;
  2721. l : longint;
  2722. hl: plabel;
  2723. Begin
  2724. tempstr := '';
  2725. expr := '';
  2726. case actasmtoken of
  2727. { Constant expression // }
  2728. AS_PLUS,AS_MINUS,AS_NOT,AS_LPAREN:
  2729. Begin
  2730. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2731. Message(assem_e_invalid_operand_type);
  2732. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2733. instr.operands[operandnum].val :=BuildExpression;
  2734. end;
  2735. { Constant expression // }
  2736. AS_STRING: Begin
  2737. if not (instr.operands[operandnum].operandtype in [OPR_NONE]) then
  2738. Message(assem_e_invalid_operand_type);
  2739. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2740. if not PadZero(actasmpattern,4) then
  2741. Message1(assem_e_invalid_string_as_opcode_operand,actasmpattern);
  2742. instr.operands[operandnum].val :=
  2743. ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  2744. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1])
  2745. shl 24;
  2746. Consume(AS_STRING);
  2747. Case actasmtoken of
  2748. AS_COMMA, AS_SEPARATOR: ;
  2749. else
  2750. Message(assem_e_invalid_string_expression);
  2751. end; { end case }
  2752. end;
  2753. { Constant expression // }
  2754. AS_INTNUM,AS_BINNUM,
  2755. AS_OCTALNUM,
  2756. AS_HEXNUM: Begin
  2757. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2758. Message(assem_e_invalid_operand_type);
  2759. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2760. instr.operands[operandnum].val :=BuildExpression;
  2761. end;
  2762. { A constant expression, or a Variable ref. }
  2763. AS_ID: Begin
  2764. if actasmpattern[1] = '@' then
  2765. { Label or Special symbol reference }
  2766. Begin
  2767. if actasmpattern = '@RESULT' then
  2768. Begin
  2769. InitAsmRef(instr);
  2770. SetUpResult(instr,operandnum);
  2771. end
  2772. else
  2773. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  2774. Message(assem_w_CODE_and_DATA_not_supported)
  2775. else
  2776. Begin
  2777. delete(actasmpattern,1,1);
  2778. if actasmpattern = '' then
  2779. Message(assem_e_null_label_ref_not_allowed);
  2780. lab := labellist.search(actasmpattern);
  2781. { check if the label is already defined }
  2782. { if so, we then check if the plabel is }
  2783. { non-nil, if so we add it to instruction }
  2784. if assigned(lab) then
  2785. Begin
  2786. if assigned(lab^.lab) then
  2787. Begin
  2788. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2789. instr.operands[operandnum].hl := lab^.lab;
  2790. instr.labeled := TRUE;
  2791. end;
  2792. end
  2793. else
  2794. { the label does not exist, create it }
  2795. { emit the opcode, but set that the }
  2796. { label has not been emitted }
  2797. Begin
  2798. getlabel(hl);
  2799. labellist.insert(actasmpattern,hl,FALSE);
  2800. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2801. instr.operands[operandnum].hl := hl;
  2802. instr.labeled := TRUE;
  2803. end;
  2804. end;
  2805. Consume(AS_ID);
  2806. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2807. Begin
  2808. Message(assem_e_syntax_error);
  2809. end;
  2810. end
  2811. else
  2812. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  2813. begin
  2814. InitAsmRef(instr);
  2815. SetUpResult(instr,operandnum);
  2816. Consume(AS_ID);
  2817. end
  2818. { probably a variable or normal expression }
  2819. { or a procedure (such as in CALL ID) }
  2820. else
  2821. Begin
  2822. { is it a constant ? }
  2823. if SearchIConstant(actasmpattern,l) then
  2824. Begin
  2825. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2826. Message(assem_e_invalid_operand_type);
  2827. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2828. instr.operands[operandnum].val :=BuildExpression;
  2829. end
  2830. else { is it a label variable ? }
  2831. Begin
  2832. { ID[ , ID.Field.Field or simple ID }
  2833. { check if this is a label, if so then }
  2834. { emit it as a label. }
  2835. if SearchLabel(actasmpattern,hl) then
  2836. Begin
  2837. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2838. instr.operands[operandnum].hl := hl;
  2839. instr.labeled := TRUE;
  2840. Consume(AS_ID);
  2841. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2842. Message(assem_e_syntax_error);
  2843. end
  2844. else
  2845. { is it a normal variable ? }
  2846. Begin
  2847. initAsmRef(instr);
  2848. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2849. Begin
  2850. { not a variable.. }
  2851. { check special variables.. }
  2852. if actasmpattern = 'SELF' then
  2853. { special self variable }
  2854. Begin
  2855. if assigned(procinfo._class) then
  2856. Begin
  2857. instr.operands[operandnum].ref.offset := procinfo.ESI_offset;
  2858. instr.operands[operandnum].ref.base := procinfo.framepointer;
  2859. end
  2860. else
  2861. Message(assem_e_cannot_use_SELF_outside_a_method);
  2862. end
  2863. else
  2864. Message1(assem_e_unknown_id,actasmpattern);
  2865. end;
  2866. expr := actasmpattern;
  2867. Consume(AS_ID);
  2868. case actasmtoken of
  2869. AS_LBRACKET: { indexing }
  2870. BuildBracketExpression(instr,TRUE);
  2871. AS_DOT: BuildRecordOffset(instr,expr);
  2872. AS_SEPARATOR,AS_COMMA: ;
  2873. else
  2874. Message(assem_e_syntax_error);
  2875. end;
  2876. end;
  2877. end;
  2878. end;
  2879. end;
  2880. { Register, a variable reference or a constant reference }
  2881. AS_REGISTER: Begin
  2882. { save the type of register used. }
  2883. tempstr := actasmpattern;
  2884. Consume(AS_REGISTER);
  2885. if actasmtoken = AS_COLON then
  2886. Begin
  2887. Consume(AS_COLON);
  2888. if actasmtoken <> AS_LBRACKET then
  2889. Message(assem_e_syn_start_with_bracket)
  2890. else
  2891. Begin
  2892. initAsmRef(instr);
  2893. instr.operands[operandnum].ref.segment := findsegment(tempstr);
  2894. BuildBracketExpression(instr,false);
  2895. end;
  2896. end
  2897. { Simple register }
  2898. else if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  2899. Begin
  2900. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_REGISTER]) then
  2901. Message(assem_e_invalid_operand_type);
  2902. instr.operands[operandnum].operandtype := OPR_REGISTER;
  2903. instr.operands[operandnum].reg := findregister(tempstr);
  2904. end
  2905. else
  2906. Message1(assem_e_syn_register,tempstr);
  2907. end;
  2908. { a variable reference, register ref. or a constant reference }
  2909. AS_LBRACKET: Begin
  2910. BuildBracketExpression(instr,false);
  2911. end;
  2912. { Unsupported }
  2913. AS_SEG,AS_OFFSET: Begin
  2914. Message(assem_e_SEG_and_OFFSET_not_supported);
  2915. Consume(actasmtoken);
  2916. { error recovery }
  2917. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  2918. Consume(actasmtoken);
  2919. end;
  2920. AS_SEPARATOR, AS_COMMA: ;
  2921. else
  2922. Message(assem_e_syn_opcode_operand);
  2923. end; { end case }
  2924. end;
  2925. Procedure BuildConstant(maxvalue: longint);
  2926. {*********************************************************************}
  2927. { PROCEDURE BuildConstant }
  2928. { Description: This routine takes care of parsing a DB,DD,or DW }
  2929. { line and adding those to the assembler node. Expressions, range- }
  2930. { checking are fullly taken care of. }
  2931. { maxvalue: $ff -> indicates that this is a DB node. }
  2932. { $ffff -> indicates that this is a DW node. }
  2933. { $ffffffff -> indicates that this is a DD node. }
  2934. {*********************************************************************}
  2935. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  2936. {*********************************************************************}
  2937. var
  2938. strlength: byte;
  2939. expr: string;
  2940. value : longint;
  2941. Begin
  2942. strlength := 0; { assume it is a DB }
  2943. Repeat
  2944. Case actasmtoken of
  2945. AS_STRING: Begin
  2946. if maxvalue = $ffff then
  2947. strlength := 2
  2948. else if maxvalue = $ffffffff then
  2949. strlength := 4;
  2950. if strlength <> 0 then
  2951. { DD and DW cases }
  2952. Begin
  2953. if Not PadZero(actasmpattern,strlength) then
  2954. Message(scan_f_string_exceeds_line);
  2955. end;
  2956. expr := actasmpattern;
  2957. Consume(AS_STRING);
  2958. Case actasmtoken of
  2959. AS_COMMA: Consume(AS_COMMA);
  2960. AS_SEPARATOR: ;
  2961. else
  2962. Message(assem_e_invalid_string_expression);
  2963. end; { end case }
  2964. ConcatString(p,expr);
  2965. end;
  2966. AS_INTNUM,AS_BINNUM,
  2967. AS_OCTALNUM,AS_HEXNUM:
  2968. Begin
  2969. value:=BuildExpression;
  2970. ConcatConstant(p,value,maxvalue);
  2971. end;
  2972. AS_ID:
  2973. Begin
  2974. value:=BuildExpression;
  2975. if value > maxvalue then
  2976. Begin
  2977. Message(assem_e_expression_out_of_bounds);
  2978. { assuming a value of maxvalue }
  2979. value := maxvalue;
  2980. end;
  2981. ConcatConstant(p,value,maxvalue);
  2982. end;
  2983. { These terms can start an assembler expression }
  2984. AS_PLUS,AS_MINUS,AS_LPAREN,AS_NOT: Begin
  2985. value := BuildExpression;
  2986. ConcatConstant(p,value,maxvalue);
  2987. end;
  2988. AS_COMMA: BEGIN
  2989. Consume(AS_COMMA);
  2990. END;
  2991. AS_SEPARATOR: ;
  2992. else
  2993. Begin
  2994. Message(assem_f_internal_error_in_buildconstant);
  2995. end;
  2996. end; { end case }
  2997. Until actasmtoken = AS_SEPARATOR;
  2998. end;
  2999. Procedure BuildOpCode;
  3000. {*********************************************************************}
  3001. { PROCEDURE BuildOpcode; }
  3002. { Description: Parses the intel opcode and operands, and writes it }
  3003. { in the TInstruction object. }
  3004. {*********************************************************************}
  3005. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  3006. { On ENTRY: Token should point to AS_OPCODE }
  3007. {*********************************************************************}
  3008. var
  3009. asmtok,op : tasmop;
  3010. expr : string;
  3011. segreg : tregister;
  3012. Begin
  3013. expr := '';
  3014. asmtok := A_NONE; { assmume no prefix }
  3015. segreg := R_NO; { assume no segment override }
  3016. { prefix seg opcode }
  3017. { prefix opcode }
  3018. if findprefix(actasmpattern,asmtok) then
  3019. Begin
  3020. { standard opcode prefix }
  3021. if asmtok <> A_NONE then
  3022. instr.addprefix(asmtok);
  3023. Consume(AS_OPCODE);
  3024. if findoverride(actasmpattern,segreg) then
  3025. Begin
  3026. Consume(AS_OPCODE);
  3027. Message(assem_w_repeat_prefix_and_seg_override);
  3028. end;
  3029. end
  3030. { seg prefix opcode }
  3031. { seg opcode }
  3032. else if findoverride(actasmpattern,segreg) then
  3033. Begin
  3034. Consume(AS_OPCODE);
  3035. if findprefix(actasmpattern,asmtok) then
  3036. Begin
  3037. { standard opcode prefix }
  3038. Message(assem_w_repeat_prefix_and_seg_override);
  3039. if asmtok <> A_NONE then
  3040. instr.addprefix(asmtok);
  3041. Consume(AS_OPCODE);
  3042. end;
  3043. end;
  3044. { opcode }
  3045. if (actasmtoken <> AS_OPCODE) then
  3046. Begin
  3047. Message(assem_e_invalid_or_missing_opcode);
  3048. { error recovery }
  3049. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA,AS_END]) do
  3050. Consume(actasmtoken);
  3051. exit;
  3052. end
  3053. else
  3054. Begin
  3055. op := findopcode(actasmpattern);
  3056. instr.addinstr(op);
  3057. { Valid combination of prefix and instruction ? }
  3058. if (asmtok <> A_NONE) and (NOT CheckPrefix(asmtok,op)) then
  3059. Message1(assem_e_invalid_prefix_and_opcode,actasmpattern);
  3060. { Valid combination of segment override }
  3061. if (segreg <> R_NO) and (NOT CheckOverride(segreg,instr)) then
  3062. Message1(assem_e_invalid_override_and_opcode,actasmpattern);
  3063. Consume(AS_OPCODE);
  3064. { Zero operand opcode ? }
  3065. if actasmtoken in [AS_END,AS_SEPARATOR] then
  3066. exit
  3067. else
  3068. operandnum := 1;
  3069. end;
  3070. repeat
  3071. case actasmtoken of
  3072. { End of asm operands for this opcode }
  3073. AS_END,
  3074. AS_SEPARATOR :
  3075. break;
  3076. { Operand delimiter }
  3077. AS_COMMA :
  3078. Begin
  3079. if operandnum > MaxOperands then
  3080. Message(assem_e_too_many_operands)
  3081. else
  3082. Inc(operandnum);
  3083. Consume(AS_COMMA);
  3084. end;
  3085. { Typecast, Constant Expression, Type Specifier }
  3086. AS_DWORD,
  3087. AS_BYTE,
  3088. AS_WORD,
  3089. AS_TBYTE,
  3090. AS_QWORD :
  3091. Begin
  3092. { tell that the instruction was overriden }
  3093. { so we will NEVER override the opsize }
  3094. instr.operands[operandnum].overriden := TRUE;
  3095. Case actasmtoken of
  3096. AS_DWORD : instr.operands[operandnum].size := S_L;
  3097. AS_WORD : instr.operands[operandnum].size := S_W;
  3098. AS_BYTE : instr.operands[operandnum].size := S_B;
  3099. AS_QWORD : instr.operands[operandnum].size := S_IQ;
  3100. AS_TBYTE : instr.operands[operandnum].size := S_FX;
  3101. end;
  3102. Consume(actasmtoken);
  3103. Case actasmtoken of
  3104. { Reference }
  3105. AS_PTR :
  3106. Begin
  3107. initAsmRef(instr);
  3108. Consume(AS_PTR);
  3109. BuildOperand(instr);
  3110. end;
  3111. { Possibly a typecast or a constant }
  3112. { expression. }
  3113. AS_LPAREN :
  3114. Begin
  3115. if actasmtoken = AS_ID then
  3116. Begin
  3117. { Case vartype of }
  3118. { LOCAL: Replace by offset and }
  3119. { BP in treference. }
  3120. { GLOBAL: Replace by mangledname}
  3121. { in symbol of treference }
  3122. { Check if next token = RPAREN }
  3123. { otherwise syntax error. }
  3124. initAsmRef(instr);
  3125. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  3126. Message1(assem_e_unknown_id,actasmpattern);
  3127. end
  3128. else
  3129. begin
  3130. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3131. instr.operands[operandnum].val := BuildExpression;
  3132. end;
  3133. end;
  3134. else
  3135. BuildOperand(instr);
  3136. end; { end case }
  3137. end;
  3138. { Type specifier }
  3139. AS_NEAR,
  3140. AS_FAR :
  3141. Begin
  3142. if actasmtoken = AS_NEAR then
  3143. Message(assem_w_near_ignored)
  3144. else
  3145. Message(assem_w_far_ignored);
  3146. Consume(actasmtoken);
  3147. if actasmtoken = AS_PTR then
  3148. begin
  3149. initAsmRef(instr);
  3150. Consume(AS_PTR);
  3151. end;
  3152. BuildOperand(instr);
  3153. end;
  3154. { Constant expression }
  3155. AS_LPAREN :
  3156. Begin
  3157. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3158. instr.operands[operandnum].val := BuildExpression;
  3159. end;
  3160. else
  3161. BuildOperand(instr);
  3162. end; { end case }
  3163. until false;
  3164. end;
  3165. Function Assemble: Ptree;
  3166. {*********************************************************************}
  3167. { PROCEDURE Assemble; }
  3168. { Description: Parses the intel assembler syntax, parsing is done }
  3169. { according to the rules in the Turbo Pascal manual. }
  3170. {*********************************************************************}
  3171. Var
  3172. hl : plabel;
  3173. labelptr : pasmlabel;
  3174. Begin
  3175. Message(assem_d_start_intel);
  3176. inexpression := FALSE;
  3177. firsttoken := TRUE;
  3178. operandnum := 0;
  3179. if assigned(procinfo.retdef) and
  3180. (is_fpu(procinfo.retdef) or
  3181. ret_in_acc(procinfo.retdef)) then
  3182. procinfo.funcret_is_valid:=true;
  3183. { sets up all opcode and register tables in uppercase }
  3184. if not _asmsorted then
  3185. Begin
  3186. SetupTables;
  3187. _asmsorted := TRUE;
  3188. end;
  3189. p:=new(paasmoutput,init);
  3190. { setup label linked list }
  3191. labellist.init;
  3192. c:=current_scanner^.asmgetchar;
  3193. actasmtoken:=gettoken;
  3194. repeat
  3195. case actasmtoken of
  3196. AS_LLABEL :
  3197. Begin
  3198. labelptr := labellist.search(actasmpattern);
  3199. if not assigned(labelptr) then
  3200. Begin
  3201. getlabel(hl);
  3202. labellist.insert(actasmpattern,hl,TRUE);
  3203. ConcatLabel(p,hl);
  3204. end
  3205. else
  3206. { the label has already been inserted into the }
  3207. { label list, either as an intruction label (in }
  3208. { this case it has not been emitted), or as a }
  3209. { duplicate local symbol (in this case it has }
  3210. { already been emitted). }
  3211. Begin
  3212. if labelptr^.emitted then
  3213. Message1(assem_e_dup_local_sym,'@'+labelptr^.name^)
  3214. else
  3215. Begin
  3216. if assigned(labelptr^.lab) then
  3217. ConcatLabel(p,labelptr^.lab);
  3218. labelptr^.emitted := TRUE;
  3219. end;
  3220. end;
  3221. Consume(AS_LLABEL);
  3222. end;
  3223. AS_LABEL :
  3224. Begin
  3225. if SearchLabel(actasmpattern,hl) then
  3226. ConcatLabel(p,hl)
  3227. else
  3228. Message1(assem_e_unknown_label_identifer,actasmpattern);
  3229. Consume(AS_LABEL);
  3230. end;
  3231. AS_DW :
  3232. Begin
  3233. Consume(AS_DW);
  3234. BuildConstant($ffff);
  3235. end;
  3236. AS_DB :
  3237. Begin
  3238. Consume(AS_DB);
  3239. BuildConstant($ff);
  3240. end;
  3241. AS_DD :
  3242. Begin
  3243. Consume(AS_DD);
  3244. BuildConstant($ffffffff);
  3245. end;
  3246. AS_OPCODE :
  3247. Begin
  3248. instr.init;
  3249. BuildOpcode;
  3250. instr.numops := operandnum;
  3251. if instr.labeled then
  3252. ConcatLabeledInstr(instr)
  3253. else
  3254. ConcatOpCode(instr);
  3255. instr.done;
  3256. end;
  3257. AS_SEPARATOR :
  3258. Begin
  3259. Consume(AS_SEPARATOR);
  3260. { let us go back to the first operand }
  3261. operandnum := 0;
  3262. end;
  3263. AS_END :
  3264. break; { end assembly block }
  3265. else
  3266. Begin
  3267. Message(assem_e_assemble_node_syntax_error);
  3268. { error recovery }
  3269. Consume(actasmtoken);
  3270. end;
  3271. end; { end case }
  3272. until false;
  3273. { check if there were undefined symbols. }
  3274. { if so, then list each of those undefined }
  3275. { labels. }
  3276. if assigned(labellist.First) then
  3277. Begin
  3278. labelptr := labellist.First;
  3279. if labellist.First <> nil then
  3280. Begin
  3281. { first label }
  3282. if not labelptr^.emitted then
  3283. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  3284. { other labels ... }
  3285. While (labelptr^.Next <> nil) do
  3286. Begin
  3287. labelptr := labelptr^.Next;
  3288. if not labelptr^.emitted then
  3289. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  3290. end;
  3291. end;
  3292. end;
  3293. assemble := genasmnode(p);
  3294. labellist.done;
  3295. Message(assem_d_finish_intel);
  3296. end;
  3297. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  3298. begin
  3299. if assigned(iasmops) then
  3300. dispose(iasmops);
  3301. exitproc:=old_exit;
  3302. end;
  3303. begin
  3304. old_exit:=exitproc;
  3305. exitproc:=@ra386int_exit;
  3306. end.
  3307. {
  3308. $Log$
  3309. Revision 1.23 1999-03-02 22:51:08 peter
  3310. * [reg-ofs] now correctly compiles to -ofs(reg) instead of ofs(reg)
  3311. * [reg*2] is now allowed
  3312. Revision 1.22 1999/02/25 21:02:50 peter
  3313. * ag386bin updates
  3314. + coff writer
  3315. Revision 1.21 1999/02/22 02:15:37 peter
  3316. * updates for ag386bin
  3317. Revision 1.20 1999/01/10 15:37:58 peter
  3318. * moved some tables from ra386*.pas -> i386.pas
  3319. + start of coff writer
  3320. * renamed asmutils unit to rautils
  3321. Revision 1.19 1998/12/23 22:55:57 peter
  3322. + rec.field(%esi) support
  3323. + [esi+rec.field] support
  3324. Revision 1.18 1998/12/11 00:03:43 peter
  3325. + globtype,tokens,version unit splitted from globals
  3326. Revision 1.17 1998/12/08 23:03:46 jonas
  3327. * allow constant offsets for go32v2 in assembler blocks
  3328. Revision 1.16 1998/12/02 16:23:36 jonas
  3329. * changed "if longintvar in set" to case or "if () or () .." statements
  3330. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  3331. Revision 1.15 1998/11/29 12:47:22 peter
  3332. * fixes for 'asm sti end;'
  3333. Revision 1.14 1998/11/16 15:38:56 peter
  3334. * fixed instruct not in table msg
  3335. Revision 1.13 1998/11/15 14:37:16 peter
  3336. + support for result in delphimode
  3337. Revision 1.12 1998/11/13 15:40:30 pierre
  3338. + added -Se in Makefile cvstest target
  3339. + lexlevel cleanup
  3340. normal_function_level main_program_level and unit_init_level defined
  3341. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  3342. (test added in code !)
  3343. * -Un option was wrong
  3344. * _FAIL and _SELF only keyword inside
  3345. constructors and methods respectively
  3346. Revision 1.11 1998/11/13 10:12:11 peter
  3347. * constant fixes
  3348. Revision 1.10 1998/11/05 23:48:27 peter
  3349. * recordtype.field support in constant expressions
  3350. * fixed imul for oa_imm8 which was not allowed
  3351. * fixed reading of local typed constants
  3352. * fixed comment reading which is not any longer a separator
  3353. Revision 1.9 1998/10/13 16:50:17 pierre
  3354. * undid some changes of Peter that made the compiler wrong
  3355. for m68k (I had to reinsert some ifdefs)
  3356. * removed several memory leaks under m68k
  3357. * removed the meory leaks for assembler readers
  3358. * cross compiling shoud work again better
  3359. ( crosscompiling sysamiga works
  3360. but as68k still complain about some code !)
  3361. Revision 1.8 1998/10/07 04:29:44 carl
  3362. * Concatlabel now gives output on error
  3363. * in/out bugfix (still ins/outs left to fix)
  3364. Revision 1.7 1998/09/02 01:23:40 carl
  3365. * bugfix of operand overrides, VERY stupid bugfix BTW...
  3366. Revision 1.6 1998/08/27 00:42:17 carl
  3367. * bugfix of leal problem
  3368. * bugfix of using overrides with record offsets
  3369. * bugfix if using records to load values
  3370. Revision 1.5 1998/08/21 08:45:53 pierre
  3371. * better line info for asm statements
  3372. Revision 1.4 1998/07/14 14:47:00 peter
  3373. * released NEWINPUT
  3374. Revision 1.3 1998/07/07 11:20:09 peter
  3375. + NEWINPUT for a better inputfile and scanner object
  3376. Revision 1.2 1998/06/24 14:06:38 peter
  3377. * fixed the name changes
  3378. Revision 1.1 1998/06/23 14:00:18 peter
  3379. * renamed RA* units
  3380. Revision 1.11 1998/06/16 08:56:28 peter
  3381. + targetcpu
  3382. * cleaner pmodules for newppu
  3383. Revision 1.10 1998/06/12 10:32:33 pierre
  3384. * column problem hopefully solved
  3385. + C vars declaration changed
  3386. Revision 1.9 1998/05/31 14:13:32 peter
  3387. * fixed call bugs with assembler readers
  3388. + OPR_SYMBOL to hold a symbol in the asm parser
  3389. * fixed staticsymtable vars which were acessed through %ebp instead of
  3390. name
  3391. Revision 1.8 1998/05/30 14:31:07 peter
  3392. + $ASMMODE
  3393. Revision 1.7 1998/05/28 16:32:05 carl
  3394. * bugfix with operands main branch version (patched manually)
  3395. Revision 1.6 1998/05/23 01:21:26 peter
  3396. + aktasmmode, aktoptprocessor, aktoutputformat
  3397. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  3398. + $LIBNAME to set the library name where the unit will be put in
  3399. * splitted cgi386 a bit (codeseg to large for bp7)
  3400. * nasm, tasm works again. nasm moved to ag386nsm.pas
  3401. Revision 1.5 1998/05/20 09:42:36 pierre
  3402. + UseTokenInfo now default
  3403. * unit in interface uses and implementation uses gives error now
  3404. * only one error for unknown symbol (uses lastsymknown boolean)
  3405. the problem came from the label code !
  3406. + first inlined procedures and function work
  3407. (warning there might be allowed cases were the result is still wrong !!)
  3408. * UseBrower updated gives a global list of all position of all used symbols
  3409. with switch -gb
  3410. Revision 1.4 1998/04/29 10:34:03 pierre
  3411. + added some code for ansistring (not complete nor working yet)
  3412. * corrected operator overloading
  3413. * corrected nasm output
  3414. + started inline procedures
  3415. + added starstarn : use ** for exponentiation (^ gave problems)
  3416. + started UseTokenInfo cond to get accurate positions
  3417. Revision 1.3 1998/04/08 16:58:06 pierre
  3418. * several bugfixes
  3419. ADD ADC and AND are also sign extended
  3420. nasm output OK (program still crashes at end
  3421. and creates wrong assembler files !!)
  3422. procsym types sym in tdef removed !!
  3423. }