ra386int.pas 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610
  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^) <> 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. ConcatLabel(p,instr.getinstruction, 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.isintvalue := 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^)+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. if (operands[1].operandtype = OPR_REFERENCE) and
  1168. (assigned(operands[1].ref.symbol)) then
  1169. Freemem(operands[1].ref.symbol,length(operands[1].ref.symbol^)+1);
  1170. operands[1].operandtype := OPR_NONE;
  1171. numops := 0;
  1172. { always a byte for XLAT }
  1173. instr.stropsize := S_B;
  1174. end;
  1175. end;
  1176. end; { case }
  1177. { we have to start a new case because INS etc are already handled before
  1178. as well (JM) }
  1179. Case instruc Of
  1180. A_INS,A_MOVS,A_OUTS:
  1181. Begin
  1182. if numops =2 then
  1183. Begin
  1184. if (operands[1].operandtype = OPR_REFERENCE) and
  1185. (assigned(operands[1].ref.symbol)) then
  1186. Freemem(operands[1].ref.symbol,length(operands[1].ref.symbol^)+1);
  1187. if (operands[2].operandtype = OPR_REFERENCE) and
  1188. (assigned(operands[2].ref.symbol)) then
  1189. Freemem(operands[2].ref.symbol,length(operands[1].ref.symbol^)+1);
  1190. operands[1].operandtype := OPR_NONE;
  1191. operands[2].operandtype := OPR_NONE;
  1192. numops := 0;
  1193. end;
  1194. end;
  1195. end;
  1196. { swap the destination and source }
  1197. { to put in AT&T style direction }
  1198. { only if there are 2/3 operand }
  1199. { numbers. }
  1200. if (instruc <> A_ENTER) then
  1201. SwapOperands(instr);
  1202. { copy them to local variables }
  1203. { for faster access }
  1204. optyp1:=operands[1].opinfo;
  1205. optyp2:=operands[2].opinfo;
  1206. optyp3:=operands[3].opinfo;
  1207. end; { end with }
  1208. { after reading the operands }
  1209. { search the instruction }
  1210. { setup startvalue from cache }
  1211. if itcache^[instruc]<>-1 then
  1212. i:=itcache^[instruc]
  1213. else
  1214. i:=0;
  1215. { this makes cpu.pp uncompilable, but i think this code should be }
  1216. { inserted in the system unit anyways. }
  1217. if (instruc > lastop_ittable) then
  1218. begin
  1219. Message1(assem_w_opcode_not_in_table,upper(int_op2str[instruc]));
  1220. fits:=true;
  1221. end
  1222. else while not(fits) do
  1223. begin
  1224. { set the instruction cache, if the instruction }
  1225. { occurs the first time }
  1226. if (it[i].i=instruc) and (itcache^[instruc]=-1) then
  1227. itcache^[instruc]:=i;
  1228. if (it[i].i=instruc) and (instr.numops=it[i].ops) then
  1229. begin
  1230. { first fit }
  1231. case instr.numops of
  1232. 0 : begin
  1233. fits:=true;
  1234. break;
  1235. end;
  1236. 1 :
  1237. Begin
  1238. if (optyp1 and it[i].o1)<>0 then
  1239. Begin
  1240. fits:=true;
  1241. break;
  1242. end;
  1243. { I consider sign-extended 8bit value to }
  1244. { be equal to immediate 8bit therefore }
  1245. { convert... }
  1246. if (optyp1 = ao_imm8) then
  1247. Begin
  1248. { check if this is a simple sign extend. }
  1249. if (it[i].o1<>ao_imm8s) then
  1250. Begin
  1251. fits:=true;
  1252. break;
  1253. end;
  1254. end;
  1255. end;
  1256. 2 : if ((optyp1 and it[i].o1)<>0) and
  1257. ((optyp2 and it[i].o2)<>0) then
  1258. Begin
  1259. fits:=true;
  1260. break;
  1261. end
  1262. { if the operands can be swaped }
  1263. { then swap them }
  1264. else if ((it[i].m and af_d)<>0) and
  1265. ((optyp1 and it[i].o2)<>0) and
  1266. ((optyp2 and it[i].o1)<>0) then
  1267. begin
  1268. { swap the destination and source }
  1269. { to put in AT&T style direction }
  1270. { What does this mean !!!! ???????????????????????? }
  1271. { if (output_format in [of_o,of_att]) then }
  1272. { ???????????? }
  1273. { SwapOperands(instr); }
  1274. fits:=true;
  1275. break;
  1276. end;
  1277. 3 : if ((optyp1 and it[i].o1)<>0) and
  1278. ((optyp2 and it[i].o2)<>0) and
  1279. ((optyp3 and it[i].o3)<>0) then
  1280. Begin
  1281. fits:=true;
  1282. break;
  1283. end;
  1284. end; { end case }
  1285. end; { endif }
  1286. if it[i].i=A_NONE then
  1287. begin
  1288. { NO MATCH! }
  1289. Message(assem_e_invalid_opcode_and_operand);
  1290. exit;
  1291. end;
  1292. inc(i);
  1293. end; { end while }
  1294. { We add the opcode to the opcode linked list }
  1295. if fits then
  1296. Begin
  1297. if instr.getprefix <> A_NONE then
  1298. Begin
  1299. p^.concat(new(pai386,op_none(instr.getprefix,S_NO)));
  1300. end;
  1301. case instr.numops of
  1302. 0:
  1303. if instr.stropsize <> S_NO then
  1304. { is this a string operation opcode or xlat then check }
  1305. { the size of the operation. }
  1306. p^.concat(new(pai386,op_none(instruc,instr.stropsize)))
  1307. else
  1308. p^.concat(new(pai386,op_none(instruc,S_NO)));
  1309. 1: Begin
  1310. case instr.operands[1].operandtype of
  1311. { all one operand opcodes with constant have no defined sizes }
  1312. { at least that is what it seems in the tasm 2.0 manual. }
  1313. OPR_CONSTANT: p^.concat(new(pai386,op_const(instruc,
  1314. S_NO, instr.operands[1].val)));
  1315. OPR_REGISTER: Case Instruc Of
  1316. A_INC,A_DEC, A_NEG,A_NOT:
  1317. Begin
  1318. p^.concat(new(pai386,op_reg(instruc,
  1319. instr.operands[1].size,instr.operands[1].reg)));
  1320. end
  1321. else
  1322. p^.concat(new(pai386,op_reg(instruc,
  1323. S_NO,instr.operands[1].reg)));
  1324. end;
  1325. { this is where it gets a bit more complicated... }
  1326. OPR_REFERENCE:
  1327. if instr.operands[1].size <> S_NO then
  1328. Begin
  1329. p^.concat(new(pai386,op_ref(instruc,
  1330. instr.operands[1].size,newreference(instr.operands[1].ref))));
  1331. end
  1332. else
  1333. Begin
  1334. { special jmp and call case with }
  1335. { symbolic references. }
  1336. case instruc of
  1337. A_CALL,A_JMP:
  1338. Begin
  1339. p^.concat(new(pai386,op_ref(instruc,
  1340. S_NO,newreference(instr.operands[1].ref))));
  1341. end
  1342. else
  1343. Message(assem_e_invalid_opcode_and_operand);
  1344. end;
  1345. End;
  1346. OPR_SYMBOL: Begin
  1347. p^.concat(new(pai386,op_csymbol(instruc,
  1348. instr.stropsize, newcsymbol(instr.operands[1].symbol^,0))));
  1349. End;
  1350. OPR_NONE: Begin
  1351. Message(assem_f_internal_error_in_concatopcode);
  1352. end;
  1353. else
  1354. Begin
  1355. Message(assem_f_internal_error_in_concatopcode);
  1356. end;
  1357. end;
  1358. end;
  1359. 2:
  1360. Begin
  1361. Case Instruc Of
  1362. A_MOVSX,A_MOVZX,A_MOVSB,A_MOVSBL,A_MOVSBW,
  1363. A_MOVSWL,A_MOVZB,A_MOVZWL:
  1364. { movzx and movsx }
  1365. HandleExtend(instr);
  1366. else
  1367. { other instructions }
  1368. Begin
  1369. With instr do
  1370. Begin
  1371. { source }
  1372. opsize := operands[1].size;
  1373. case operands[1].operandtype of
  1374. { reg,reg }
  1375. { reg,ref }
  1376. OPR_REGISTER:
  1377. Begin
  1378. case operands[2].operandtype of
  1379. OPR_REGISTER:
  1380. Begin
  1381. { see info in ratti386.pas, about the problem }
  1382. { which can cause gas here. }
  1383. if (opsize = operands[2].size) then
  1384. begin
  1385. p^.concat(new(pai386,op_reg_reg(instruc,
  1386. opsize,operands[1].reg,operands[2].reg)));
  1387. end
  1388. else
  1389. Case instruc Of
  1390. A_IN:
  1391. p^.concat(new(pai386,op_reg_reg(instruc,
  1392. operands[2].size,operands[1].reg,operands[2].reg)));
  1393. A_OUT:
  1394. p^.concat(new(pai386,op_reg_reg(instruc,
  1395. operands[1].size,operands[1].reg,operands[2].reg)));
  1396. { these do not require any size specification. }
  1397. A_SAL,A_SAR,A_SHL,A_SHR,A_ROL,A_ROR,A_RCR,
  1398. A_RCL:
  1399. { outs and ins are already taken care by }
  1400. { the first pass. }
  1401. p^.concat(new(pai386,op_reg_reg(instruc,
  1402. S_NO,operands[1].reg,operands[2].reg)))
  1403. else
  1404. Message(assem_e_invalid_opcode_and_operand);
  1405. end;
  1406. end; {case}
  1407. OPR_REFERENCE:
  1408. { variable name. }
  1409. { here we must check the instruction type }
  1410. { before deciding if to use and compare }
  1411. { any sizes. }
  1412. if assigned(operands[2].ref.symbol) then
  1413. Begin
  1414. if (opsize = operands[2].size) then
  1415. p^.concat(new(pai386,op_reg_ref(instruc,
  1416. opsize,operands[1].reg,newreference(operands[2].ref))))
  1417. Else
  1418. Case instruc Of
  1419. A_RCL,A_RCR,A_ROL,A_ROR,A_SAL,A_SAR,A_SHR,
  1420. A_SHL:
  1421. p^.concat(new(pai386,op_reg_ref(instruc,
  1422. opsize,operands[1].reg,newreference(operands[2].ref))))
  1423. else
  1424. Message(assem_e_invalid_size_in_ref);
  1425. End; {case}
  1426. end
  1427. else
  1428. Begin
  1429. { register reference }
  1430. { possiblities:1) local variable which }
  1431. { has been replaced by bp and offset }
  1432. { in this case size should be valid }
  1433. { 2) Indirect register }
  1434. { adressing, 1st operand determines }
  1435. { size. }
  1436. if (opsize = operands[2].size) or (operands[2].size = S_NO) then
  1437. p^.concat(new(pai386,op_reg_ref(instruc,
  1438. opsize,operands[1].reg,newreference(operands[2].ref))))
  1439. else
  1440. Message(assem_e_invalid_size_in_ref);
  1441. end;
  1442. OPR_CONSTANT: { const,reg }
  1443. Begin { OUT const,reg }
  1444. if (instruc = A_OUT) and (opsize = S_B) then
  1445. p^.concat(new(pai386,op_reg_const(instruc,
  1446. opsize,operands[1].reg,operands[2].val)))
  1447. else
  1448. Message(assem_e_invalid_size_in_ref);
  1449. end;
  1450. else { else case }
  1451. Begin
  1452. Message(assem_f_internal_error_in_concatopcode);
  1453. end;
  1454. end; { end inner case }
  1455. end;
  1456. { const,reg }
  1457. { const,const }
  1458. { const,ref }
  1459. OPR_CONSTANT:
  1460. case instr.operands[2].operandtype of
  1461. { constant, constant does not have a specific size. }
  1462. OPR_CONSTANT:
  1463. p^.concat(new(pai386,op_const_const(instruc,
  1464. S_NO,operands[1].val,operands[2].val)));
  1465. OPR_REFERENCE:
  1466. Begin
  1467. if (operands[1].val <= $ff) and
  1468. (operands[2].size in [S_B,S_W,S_L,
  1469. S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1470. p^.concat(new(pai386,op_const_ref(instruc,
  1471. operands[2].size,operands[1].val,
  1472. newreference(operands[2].ref))))
  1473. else
  1474. if (operands[1].val <= $ffff) and
  1475. (operands[2].size in [S_W,S_L,
  1476. S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1477. p^.concat(new(pai386,op_const_ref(instruc,
  1478. operands[2].size,operands[1].val,
  1479. newreference(operands[2].ref))))
  1480. else
  1481. if (operands[1].val <= $7fffffff) and
  1482. (operands[2].size in [S_L,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1483. p^.concat(new(pai386,op_const_ref(instruc,
  1484. operands[2].size,operands[1].val,
  1485. newreference(operands[2].ref))))
  1486. else
  1487. Message(assem_e_invalid_size_in_ref);
  1488. end;
  1489. OPR_REGISTER:
  1490. Begin
  1491. { size of opcode determined by register }
  1492. if (operands[1].val <= $ff) and
  1493. (operands[2].size in [S_B,S_W,S_L,S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1494. p^.concat(new(pai386,op_const_reg(instruc,
  1495. operands[2].size,operands[1].val,
  1496. operands[2].reg)))
  1497. else
  1498. if (operands[1].val <= $ffff) and
  1499. (operands[2].size in [S_W,S_L,S_IS,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1500. p^.concat(new(pai386,op_const_reg(instruc,
  1501. operands[2].size,operands[1].val,
  1502. operands[2].reg)))
  1503. else
  1504. if (operands[1].val <= $7fffffff) and
  1505. (operands[2].size in [S_L,S_IL,S_IQ,S_FS,S_FL,S_FX]) then
  1506. p^.concat(new(pai386,op_const_reg(instruc,
  1507. operands[2].size,operands[1].val,
  1508. operands[2].reg)))
  1509. else
  1510. Message(assem_e_invalid_opcode_size);
  1511. end;
  1512. else
  1513. Begin
  1514. Message(assem_f_internal_error_in_concatopcode);
  1515. end;
  1516. end; { end case }
  1517. { ref,reg }
  1518. { ref,ref }
  1519. OPR_REFERENCE:
  1520. case instr.operands[2].operandtype of
  1521. OPR_REGISTER:
  1522. if assigned(operands[1].ref.symbol) then
  1523. { global variable }
  1524. Begin
  1525. Case instruc Of
  1526. A_LEA,A_LDS,A_LES,A_LFS,A_LGS,A_LSS:
  1527. p^.concat(new(pai386,op_ref_reg(instruc,
  1528. S_NO,newreference(operands[1].ref),
  1529. operands[2].reg)))
  1530. else
  1531. if (opsize = operands[2].size) then
  1532. p^.concat(new(pai386,op_ref_reg(instruc,
  1533. opsize,newreference(operands[1].ref),
  1534. operands[2].reg)))
  1535. else
  1536. Begin
  1537. Message(assem_e_invalid_opcode_and_operand);
  1538. end;
  1539. end; { case }
  1540. end
  1541. else
  1542. Begin
  1543. { register reference }
  1544. { possiblities:1) local variable which }
  1545. { has been replaced by bp and offset }
  1546. { in this case size should be valid }
  1547. { 2) Indirect register }
  1548. { adressing, 2nd operand determines }
  1549. { size. }
  1550. if (opsize = operands[2].size) or (opsize = S_NO) then
  1551. Begin
  1552. p^.concat(new(pai386,op_ref_reg(instruc,
  1553. operands[2].size,newreference(operands[1].ref),
  1554. operands[2].reg)));
  1555. end
  1556. else
  1557. Message(assem_e_invalid_size_in_ref);
  1558. end;
  1559. OPR_REFERENCE: { special opcodes }
  1560. p^.concat(new(pai386,op_ref_ref(instruc,
  1561. opsize,newreference(operands[1].ref),
  1562. newreference(operands[2].ref))));
  1563. else
  1564. Begin
  1565. Message(assem_f_internal_error_in_concatopcode);
  1566. end;
  1567. end; { end inner case }
  1568. end; { end case }
  1569. end; { end with }
  1570. end; {end else of case... }
  1571. end; { end case }
  1572. end;
  1573. 3: Begin
  1574. { only imul, shld and shrd }
  1575. { middle must be a register }
  1576. if ((instruc = A_SHLD) or (instruc = A_SHRD)) and (instr.operands[2].operandtype =
  1577. OPR_REGISTER) then
  1578. Begin
  1579. case instr.operands[2].size of
  1580. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  1581. Begin
  1582. if instr.operands[1].val <= $ff then
  1583. Begin
  1584. if instr.operands[3].size in [S_W] then
  1585. Begin
  1586. case instr.operands[3].operandtype of
  1587. OPR_REFERENCE: { MISSING !!!! } ;
  1588. OPR_REGISTER: p^.concat(new(pai386,
  1589. op_const_reg_reg(instruc, S_W,
  1590. instr.operands[1].val, instr.operands[2].reg,
  1591. instr.operands[3].reg)));
  1592. else
  1593. Message(assem_e_invalid_opcode_and_operand);
  1594. Message(assem_e_invalid_opcode_and_operand);
  1595. end;
  1596. end
  1597. else
  1598. Message(assem_e_invalid_opcode_and_operand);
  1599. end;
  1600. end
  1601. else
  1602. Message(assem_e_invalid_opcode_and_operand);
  1603. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  1604. Begin
  1605. if instr.operands[1].val <= $ff then
  1606. Begin
  1607. if instr.operands[3].size in [S_L] then
  1608. Begin
  1609. case instr.operands[3].operandtype of
  1610. OPR_REFERENCE: { MISSING !!!! } ;
  1611. OPR_REGISTER: p^.concat(new(pai386,
  1612. op_const_reg_reg(instruc, S_L,
  1613. instr.operands[1].val, instr.operands[2].reg,
  1614. instr.operands[3].reg)));
  1615. else
  1616. Message(assem_e_invalid_opcode_and_operand);
  1617. end;
  1618. end
  1619. else
  1620. Message(assem_e_invalid_opcode_and_operand);
  1621. end;
  1622. end
  1623. else
  1624. Message(assem_e_invalid_opcode_and_operand);
  1625. else
  1626. Message(assem_e_invalid_opcode_and_operand);
  1627. end; { end case }
  1628. end
  1629. else
  1630. if (instruc = A_IMUL) and
  1631. (instr.operands[3].operandtype = OPR_REGISTER) then
  1632. Begin
  1633. case instr.operands[3].size of
  1634. S_W: if instr.operands[1].operandtype = OPR_CONSTANT then
  1635. Begin
  1636. if instr.operands[1].val <= $ffff then
  1637. Begin
  1638. if instr.operands[2].size in [S_W] then
  1639. Begin
  1640. case instr.operands[2].operandtype of
  1641. OPR_REFERENCE: { MISSING !!!! } ;
  1642. OPR_REGISTER: p^.concat(new(pai386,
  1643. op_const_reg_reg(instruc, S_W,
  1644. instr.operands[1].val, instr.operands[2].reg,
  1645. instr.operands[3].reg)));
  1646. else
  1647. Message(assem_e_invalid_opcode_and_operand);
  1648. end; { end case }
  1649. end
  1650. else
  1651. Message(assem_e_invalid_opcode_and_operand);
  1652. end;
  1653. end
  1654. else
  1655. Message(assem_e_invalid_opcode_and_operand);
  1656. S_L: if instr.operands[1].operandtype = OPR_CONSTANT then
  1657. Begin
  1658. if instr.operands[1].val <= $7fffffff then
  1659. Begin
  1660. if instr.operands[2].size in [S_L] then
  1661. Begin
  1662. case instr.operands[2].operandtype of
  1663. OPR_REFERENCE: { MISSING !!!! } ;
  1664. OPR_REGISTER: p^.concat(new(pai386,
  1665. op_const_reg_reg(instruc, S_L,
  1666. instr.operands[1].val, instr.operands[2].reg,
  1667. instr.operands[3].reg)));
  1668. else
  1669. Message(assem_e_invalid_opcode_and_operand);
  1670. end; { end case }
  1671. end
  1672. else
  1673. Message(assem_e_invalid_opcode_and_operand);
  1674. end;
  1675. end
  1676. else
  1677. Message(assem_e_invalid_opcode_and_operand);
  1678. else
  1679. Message(assem_e_invalid_middle_sized_operand);
  1680. end; { end case }
  1681. end { endif }
  1682. else
  1683. Message(assem_e_invalid_three_operand_opcode);
  1684. end;
  1685. end; { end case }
  1686. end; { end "if fits then" ... }
  1687. end;
  1688. {---------------------------------------------------------------------}
  1689. { Routines for the parsing }
  1690. {---------------------------------------------------------------------}
  1691. procedure consume(t : tinteltoken);
  1692. begin
  1693. if t<>actasmtoken then
  1694. Message(assem_e_syntax_error);
  1695. actasmtoken:=gettoken;
  1696. { if the token must be ignored, then }
  1697. { get another token to parse. }
  1698. if actasmtoken = AS_NONE then
  1699. actasmtoken := gettoken;
  1700. end;
  1701. function findregister(const s : string): tregister;
  1702. {*********************************************************************}
  1703. { FUNCTION findregister(s: string):tasmop; }
  1704. { Description: Determines if the s string is a valid register, }
  1705. { if so returns correct tregister token, or R_NO if not found. }
  1706. {*********************************************************************}
  1707. var
  1708. i: tregister;
  1709. begin
  1710. findregister := R_NO;
  1711. for i:=firstreg to lastreg do
  1712. if s = iasmregs[i] then
  1713. Begin
  1714. findregister := i;
  1715. exit;
  1716. end;
  1717. end;
  1718. function findoverride(const s: string; var reg:tregister): boolean;
  1719. var
  1720. i: byte;
  1721. begin
  1722. findoverride := FALSE;
  1723. reg := R_NO;
  1724. for i:=0 to _count_asmoverrides do
  1725. Begin
  1726. if s = _asmoverrides[i] then
  1727. begin
  1728. reg := _overridetokens[i];
  1729. findoverride := TRUE;
  1730. exit;
  1731. end;
  1732. end;
  1733. end;
  1734. function findprefix(const s: string; var token: tasmop): boolean;
  1735. var i: byte;
  1736. Begin
  1737. findprefix := FALSE;
  1738. for i:=0 to _count_asmprefixes do
  1739. Begin
  1740. if s = _asmprefixes[i] then
  1741. begin
  1742. token := _prefixtokens[i];
  1743. findprefix := TRUE;
  1744. exit;
  1745. end;
  1746. end;
  1747. end;
  1748. function findsegment(const s:string): tregister;
  1749. {*********************************************************************}
  1750. { FUNCTION findsegment(s: string):tasmop; }
  1751. { Description: Determines if the s string is a valid segment register}
  1752. { if so returns correct tregister token, or R_NO if not found. }
  1753. {*********************************************************************}
  1754. var
  1755. i: tregister;
  1756. Begin
  1757. findsegment := R_DEFAULT_SEG;
  1758. for i:=firstsreg to lastsreg do
  1759. if s = iasmregs[i] then
  1760. Begin
  1761. findsegment := i;
  1762. exit;
  1763. end;
  1764. end;
  1765. function findopcode(const s: string): tasmop;
  1766. {*********************************************************************}
  1767. { FUNCTION findopcode(s: string): tasmop; }
  1768. { Description: Determines if the s string is a valid opcode }
  1769. { if so returns correct tasmop token. }
  1770. {*********************************************************************}
  1771. var
  1772. i: tasmop;
  1773. j: byte;
  1774. Begin
  1775. findopcode := A_NONE;
  1776. for i:=firstop to lastop do
  1777. if s = iasmops^[i] then
  1778. begin
  1779. findopcode:=i;
  1780. exit;
  1781. end;
  1782. { not found yet, search for extended opcodes }
  1783. { now, in this case, we must use the suffix }
  1784. { to determine the size of the instruction }
  1785. for j:=0 to _count_asmspecialops do
  1786. Begin
  1787. if s = _specialops[j] then
  1788. Begin
  1789. findopcode := _specialopstokens[j];
  1790. { set the size }
  1791. case s[length(s)] of
  1792. 'B': instr.stropsize := S_B;
  1793. 'D': instr.stropsize := S_L;
  1794. 'W': instr.stropsize := S_W;
  1795. end;
  1796. exit;
  1797. end;
  1798. end;
  1799. end;
  1800. Function CheckPrefix(prefix: tasmop; opcode:tasmop): Boolean;
  1801. { Checks if the prefix is valid with the following instruction }
  1802. { return false if not, otherwise true }
  1803. Begin
  1804. CheckPrefix := TRUE;
  1805. Case prefix of
  1806. A_REP,A_REPNE,A_REPE:
  1807. Case opcode Of
  1808. A_SCAS,A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  1809. Else
  1810. Begin
  1811. CheckPrefix := FALSE;
  1812. exit;
  1813. end;
  1814. end; { case }
  1815. A_LOCK:
  1816. Case opcode Of
  1817. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  1818. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  1819. Else
  1820. Begin
  1821. CheckPrefix := FALSE;
  1822. Exit;
  1823. end;
  1824. end; { case }
  1825. A_NONE: exit; { no prefix here }
  1826. else
  1827. CheckPrefix := FALSE;
  1828. end; { end case }
  1829. end;
  1830. Procedure InitAsmRef(var instr: TInstruction);
  1831. {*********************************************************************}
  1832. { Description: This routine first check if the instruction is of }
  1833. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  1834. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  1835. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  1836. { to point to the default segment. }
  1837. {*********************************************************************}
  1838. Begin
  1839. With instr do
  1840. Begin
  1841. case operands[operandnum].operandtype of
  1842. OPR_REFERENCE: exit;
  1843. OPR_NONE: ;
  1844. else
  1845. Message(assem_e_invalid_operand_type);
  1846. end;
  1847. operands[operandnum].operandtype := OPR_REFERENCE;
  1848. operands[operandnum].ref.segment := R_DEFAULT_SEG;
  1849. end;
  1850. end;
  1851. Function CheckOverride(segreg: tregister; var instr: TInstruction): Boolean;
  1852. { Check if the override is valid, and if so then }
  1853. { update the instr variable accordingly. }
  1854. Begin
  1855. CheckOverride := FALSE;
  1856. Case instr.getinstruction of
  1857. A_MOVS,A_XLAT,A_CMPS:
  1858. Begin
  1859. CheckOverride := TRUE;
  1860. Message(assem_e_segment_override_not_supported);
  1861. end
  1862. end;
  1863. End;
  1864. Procedure GetRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  1865. {*********************************************************************}
  1866. { PROCEDURE GetRecordOffsetSize }
  1867. { Description: This routine builds up a record offset after a AS_DOT }
  1868. { token is encountered. }
  1869. { On entry actasmtoken should be equal to AS_DOT }
  1870. {*********************************************************************}
  1871. { EXIT CONDITION: On exit the routine should point to either the }
  1872. { ERROR RECOVER: read until AS_COMMA or AS_SEPARATOR token. }
  1873. { Warning: This is called recursively. }
  1874. {*********************************************************************}
  1875. var
  1876. toffset,tsize : longint;
  1877. Begin
  1878. offset:=0;
  1879. size:=0;
  1880. Consume(AS_DOT);
  1881. if actasmtoken = AS_ID then
  1882. Begin
  1883. if not GetTypeOffsetSize(expr,actasmpattern,toffset,tsize) and
  1884. not GetVarOffsetSize(expr,actasmpattern,toffset,tsize) then
  1885. begin
  1886. Message(assem_e_syntax_error);
  1887. toffset:=0;
  1888. tsize:=0;
  1889. end;
  1890. inc(offset,toffset);
  1891. size:=tsize;
  1892. Consume(AS_ID);
  1893. if actasmtoken=AS_DOT then
  1894. begin
  1895. GetRecordOffsetSize(expr,toffset,tsize);
  1896. inc(offset,toffset);
  1897. size:=tsize;
  1898. end;
  1899. end
  1900. else
  1901. Begin
  1902. Message(assem_e_syntax_error);
  1903. repeat
  1904. consume(actasmtoken)
  1905. until (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA);
  1906. end;
  1907. end;
  1908. Function BuildRefExpression: longint;
  1909. {*********************************************************************}
  1910. { FUNCTION BuildExpression: longint }
  1911. { Description: This routine calculates a constant expression to }
  1912. { a given value. The return value is the value calculated from }
  1913. { the expression. }
  1914. { The following tokens (not strings) are recognized: }
  1915. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  1916. {*********************************************************************}
  1917. { ENTRY: On entry the token should be any valid expression token. }
  1918. { EXIT: On Exit the token points to any token after the closing }
  1919. { RBRACKET }
  1920. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  1921. { invalid tokens. }
  1922. {*********************************************************************}
  1923. var
  1924. tempstr,expr : string;
  1925. l,k : longint;
  1926. errorflag : boolean;
  1927. Begin
  1928. errorflag := FALSE;
  1929. tempstr := '';
  1930. expr := '';
  1931. { tell tokenizer that we are in }
  1932. { an expression. }
  1933. inexpression := TRUE;
  1934. Repeat
  1935. Case actasmtoken of
  1936. AS_LPAREN:
  1937. Begin
  1938. Consume(AS_LPAREN);
  1939. expr := expr + '(';
  1940. end;
  1941. AS_RPAREN:
  1942. Begin
  1943. Consume(AS_RPAREN);
  1944. expr := expr + ')';
  1945. end;
  1946. AS_SHL:
  1947. Begin
  1948. Consume(AS_SHL);
  1949. expr := expr + '<';
  1950. end;
  1951. AS_SHR:
  1952. Begin
  1953. Consume(AS_SHR);
  1954. expr := expr + '>';
  1955. end;
  1956. AS_SLASH:
  1957. Begin
  1958. Consume(AS_SLASH);
  1959. expr := expr + '/';
  1960. end;
  1961. AS_MOD:
  1962. Begin
  1963. Consume(AS_MOD);
  1964. expr := expr + '%';
  1965. end;
  1966. AS_STAR:
  1967. Begin
  1968. Consume(AS_STAR);
  1969. expr := expr + '*';
  1970. end;
  1971. AS_PLUS:
  1972. Begin
  1973. Consume(AS_PLUS);
  1974. expr := expr + '+';
  1975. end;
  1976. AS_MINUS:
  1977. Begin
  1978. Consume(AS_MINUS);
  1979. expr := expr + '-';
  1980. end;
  1981. AS_AND:
  1982. Begin
  1983. Consume(AS_AND);
  1984. expr := expr + '&';
  1985. end;
  1986. AS_NOT:
  1987. Begin
  1988. Consume(AS_NOT);
  1989. expr := expr + '~';
  1990. end;
  1991. AS_XOR:
  1992. Begin
  1993. Consume(AS_XOR);
  1994. expr := expr + '^';
  1995. end;
  1996. AS_OR:
  1997. Begin
  1998. Consume(AS_OR);
  1999. expr := expr + '|';
  2000. end;
  2001. AS_INTNUM:
  2002. Begin
  2003. expr := expr + actasmpattern;
  2004. Consume(AS_INTNUM);
  2005. end;
  2006. AS_BINNUM:
  2007. Begin
  2008. expr:=expr+BinaryToDec(actasmpattern);
  2009. Consume(AS_BINNUM);
  2010. end;
  2011. AS_HEXNUM:
  2012. Begin
  2013. expr:=expr+HexToDec(actasmpattern);
  2014. Consume(AS_HEXNUM);
  2015. end;
  2016. AS_OCTALNUM:
  2017. Begin
  2018. expr:=expr+OctalToDec(actasmpattern);
  2019. Consume(AS_OCTALNUM);
  2020. end;
  2021. AS_ID:
  2022. Begin
  2023. tempstr:=actasmpattern;
  2024. consume(AS_ID);
  2025. if actasmtoken=AS_DOT then
  2026. begin
  2027. GetRecordOffsetSize(tempstr,l,k);
  2028. str(l, tempstr);
  2029. expr := expr + tempstr;
  2030. end
  2031. else
  2032. begin
  2033. if SearchIConstant(tempstr,l) then
  2034. begin
  2035. str(l, tempstr);
  2036. expr := expr + tempstr;
  2037. end
  2038. else
  2039. Message1(assem_e_invalid_const_symbol,tempstr);
  2040. end;
  2041. end;
  2042. AS_RBRACKET: { End of reference }
  2043. Begin
  2044. if not ErrorFlag then
  2045. BuildRefExpression := CalculateExpression(expr)
  2046. else
  2047. BuildRefExpression := 0;
  2048. Consume(AS_RBRACKET);
  2049. { no longer in an expression }
  2050. inexpression := FALSE;
  2051. exit;
  2052. end;
  2053. else
  2054. Begin
  2055. { write error only once. }
  2056. if not errorflag then
  2057. Message(assem_e_invalid_constant_expression);
  2058. BuildRefExpression := 0;
  2059. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then
  2060. exit;
  2061. { consume tokens until we find COMMA or SEPARATOR }
  2062. Consume(actasmtoken);
  2063. errorflag := TRUE;
  2064. end;
  2065. end;
  2066. Until false;
  2067. end;
  2068. Procedure BuildRecordOffset(var instr: TInstruction; varname: string);
  2069. {*********************************************************************}
  2070. { PROCEDURE BuildRecordOffset(var Instr: TInstruction) }
  2071. { Description: This routine takes care of field specifiers of records }
  2072. { and/or variables in asm operands. It updates the offset accordingly}
  2073. {*********************************************************************}
  2074. { ENTRY: On entry the token should be DOT. }
  2075. { name: should be the name of the variable to be expanded. '' if }
  2076. { no variabled specified. }
  2077. { EXIT: On Exit the token points to SEPARATOR or COMMA. }
  2078. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2079. { invalid tokens. }
  2080. {*********************************************************************}
  2081. var
  2082. firstpass: boolean;
  2083. offset: longint;
  2084. tsize,toffset : longint;
  2085. basetypename : string;
  2086. Begin
  2087. basetypename := '';
  2088. firstpass := TRUE;
  2089. { .ID[REG].ID ... }
  2090. { .ID.ID... }
  2091. Consume(AS_DOT);
  2092. Repeat
  2093. case actasmtoken of
  2094. AS_ID: Begin
  2095. { we must reset the operand size - since only the last field }
  2096. { will give us the size of the operand. }
  2097. { instr.opsize := S_NO;}
  2098. InitAsmRef(instr);
  2099. { var_name.typefield.typefield }
  2100. if (varname <> '') then
  2101. Begin
  2102. if GetVarOffsetSize(varname,actasmpattern,toffset,tsize) then
  2103. Begin
  2104. Inc(instr.operands[operandnum].ref.offset,tOffset);
  2105. SetOperandSize(instr,operandnum,tsize);
  2106. end
  2107. else
  2108. Message1(assem_e_unknown_id,actasmpattern);
  2109. end
  2110. else
  2111. { [ref].var_name.typefield.typefield ... }
  2112. { [ref].var_name[reg] }
  2113. if not assigned(instr.operands[operandnum].ref.symbol) and
  2114. firstpass then
  2115. Begin
  2116. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2117. Begin
  2118. { type field ? }
  2119. basetypename := actasmpattern;
  2120. end
  2121. else
  2122. varname := actasmpattern;
  2123. end
  2124. else
  2125. if firstpass then
  2126. { [ref].typefield.typefield ... }
  2127. { where the first typefield must specifiy the base }
  2128. { object or record type. }
  2129. Begin
  2130. basetypename := actasmpattern;
  2131. end
  2132. else
  2133. { [ref].typefield.typefield ... }
  2134. { basetpyename is already set up... now look for fields. }
  2135. Begin
  2136. if GetTypeOffsetSize(basetypename,actasmpattern,tOffset,Tsize) then
  2137. Begin
  2138. Inc(instr.operands[operandnum].ref.offset,tOffset);
  2139. SetOperandSize(instr,operandnum,Tsize);
  2140. end
  2141. else
  2142. Message1(assem_e_unknown_id,actasmpattern);
  2143. end;
  2144. Consume(AS_ID);
  2145. { Take care of index register on this variable }
  2146. if actasmtoken = AS_LBRACKET then
  2147. Begin
  2148. Consume(AS_LBRACKET);
  2149. Case actasmtoken of
  2150. AS_REGISTER: Begin
  2151. if instr.operands[operandnum].ref.index <> R_NO then
  2152. Message(assem_e_defining_index_more_than_once);
  2153. instr.operands[operandnum].ref.index :=
  2154. findregister(actasmpattern);
  2155. Consume(AS_REGISTER);
  2156. end;
  2157. else
  2158. Begin
  2159. { add offsets , assuming these are constant expressions... }
  2160. Inc(instr.operands[operandnum].ref.offset,BuildRefExpression);
  2161. end;
  2162. end;
  2163. Consume(AS_RBRACKET);
  2164. end;
  2165. { Here we should either have AS_DOT, AS_SEPARATOR or AS_COMMA }
  2166. if actasmtoken = AS_DOT then
  2167. Consume(AS_DOT);
  2168. firstpass := FALSE;
  2169. Offset := 0;
  2170. end;
  2171. AS_SEPARATOR: exit;
  2172. AS_COMMA: exit;
  2173. else
  2174. Begin
  2175. Message(assem_e_invalid_field_specifier);
  2176. Consume(actasmtoken);
  2177. firstpass := FALSE;
  2178. end;
  2179. end; { end case }
  2180. Until (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA);
  2181. end;
  2182. Function BuildExpression: longint;
  2183. {*********************************************************************}
  2184. { FUNCTION BuildExpression: longint }
  2185. { Description: This routine calculates a constant expression to }
  2186. { a given value. The return value is the value calculated from }
  2187. { the expression. }
  2188. { The following tokens (not strings) are recognized: }
  2189. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  2190. {*********************************************************************}
  2191. { ENTRY: On entry the token should be any valid expression token. }
  2192. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  2193. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  2194. { invalid tokens. }
  2195. {*********************************************************************}
  2196. var expr: string;
  2197. tempstr: string;
  2198. l,k : longint;
  2199. errorflag: boolean;
  2200. Begin
  2201. errorflag := FALSE;
  2202. expr := '';
  2203. tempstr := '';
  2204. { tell tokenizer that we are in an expression. }
  2205. inexpression := TRUE;
  2206. Repeat
  2207. Case actasmtoken of
  2208. AS_LPAREN: Begin
  2209. Consume(AS_LPAREN);
  2210. expr := expr + '(';
  2211. end;
  2212. AS_RPAREN: Begin
  2213. Consume(AS_RPAREN);
  2214. expr := expr + ')';
  2215. end;
  2216. AS_SHL: Begin
  2217. Consume(AS_SHL);
  2218. expr := expr + '<';
  2219. end;
  2220. AS_SHR: Begin
  2221. Consume(AS_SHR);
  2222. expr := expr + '>';
  2223. end;
  2224. AS_SLASH: Begin
  2225. Consume(AS_SLASH);
  2226. expr := expr + '/';
  2227. end;
  2228. AS_MOD: Begin
  2229. Consume(AS_MOD);
  2230. expr := expr + '%';
  2231. end;
  2232. AS_STAR: Begin
  2233. Consume(AS_STAR);
  2234. expr := expr + '*';
  2235. end;
  2236. AS_PLUS: Begin
  2237. Consume(AS_PLUS);
  2238. expr := expr + '+';
  2239. end;
  2240. AS_MINUS: Begin
  2241. Consume(AS_MINUS);
  2242. expr := expr + '-';
  2243. end;
  2244. AS_AND: Begin
  2245. Consume(AS_AND);
  2246. expr := expr + '&';
  2247. end;
  2248. AS_NOT: Begin
  2249. Consume(AS_NOT);
  2250. expr := expr + '~';
  2251. end;
  2252. AS_XOR: Begin
  2253. Consume(AS_XOR);
  2254. expr := expr + '^';
  2255. end;
  2256. AS_OR: Begin
  2257. Consume(AS_OR);
  2258. expr := expr + '|';
  2259. end;
  2260. AS_ID: Begin
  2261. tempstr:=actasmpattern;
  2262. consume(AS_ID);
  2263. if actasmtoken=AS_DOT then
  2264. begin
  2265. GetRecordOffsetSize(tempstr,l,k);
  2266. str(l, tempstr);
  2267. expr := expr + tempstr;
  2268. end
  2269. else
  2270. begin
  2271. if SearchIConstant(tempstr,l) then
  2272. begin
  2273. str(l, tempstr);
  2274. expr := expr + tempstr;
  2275. end
  2276. else
  2277. Message1(assem_e_invalid_const_symbol,actasmpattern);
  2278. end;
  2279. end;
  2280. AS_INTNUM: Begin
  2281. expr := expr + actasmpattern;
  2282. Consume(AS_INTNUM);
  2283. end;
  2284. AS_BINNUM: Begin
  2285. tempstr := BinaryToDec(actasmpattern);
  2286. if tempstr = '' then
  2287. Message(assem_f_error_converting_bin);
  2288. expr:=expr+tempstr;
  2289. Consume(AS_BINNUM);
  2290. end;
  2291. AS_HEXNUM: Begin
  2292. tempstr := HexToDec(actasmpattern);
  2293. if tempstr = '' then
  2294. Message(assem_f_error_converting_hex);
  2295. expr:=expr+tempstr;
  2296. Consume(AS_HEXNUM);
  2297. end;
  2298. AS_OCTALNUM: Begin
  2299. tempstr := OctalToDec(actasmpattern);
  2300. if tempstr = '' then
  2301. Message(assem_f_error_converting_octal);
  2302. expr:=expr+tempstr;
  2303. Consume(AS_OCTALNUM);
  2304. end;
  2305. { go to next term }
  2306. AS_COMMA: Begin
  2307. if not ErrorFlag then
  2308. BuildExpression := CalculateExpression(expr)
  2309. else
  2310. BuildExpression := 0;
  2311. inexpression := FALSE;
  2312. Exit;
  2313. end;
  2314. { go to next symbol }
  2315. AS_SEPARATOR: Begin
  2316. if not ErrorFlag then
  2317. BuildExpression := CalculateExpression(expr)
  2318. else
  2319. BuildExpression := 0;
  2320. inexpression := FALSE;
  2321. Exit;
  2322. end;
  2323. else
  2324. Begin
  2325. { only write error once. }
  2326. if not errorflag then
  2327. Message(assem_e_invalid_constant_expression);
  2328. { consume tokens until we find COMMA or SEPARATOR }
  2329. Consume(actasmtoken);
  2330. errorflag := TRUE;
  2331. End;
  2332. end;
  2333. Until false;
  2334. end;
  2335. Procedure BuildScaling(Var instr: TInstruction);
  2336. {*********************************************************************}
  2337. { Takes care of parsing expression starting from the scaling value }
  2338. { up to and including possible field specifiers. }
  2339. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR }
  2340. { or AS_COMMA. On entry should point to AS_STAR token. }
  2341. {*********************************************************************}
  2342. var str:string;
  2343. l: longint;
  2344. code: integer;
  2345. Begin
  2346. Consume(AS_STAR);
  2347. if (instr.operands[operandnum].ref.scalefactor <> 0)
  2348. and (instr.operands[operandnum].ref.scalefactor <> 1) then
  2349. Begin
  2350. Message(assem_f_internal_error_in_buildscale);
  2351. end;
  2352. case actasmtoken of
  2353. AS_INTNUM: str := actasmpattern;
  2354. AS_HEXNUM: str := HexToDec(actasmpattern);
  2355. AS_BINNUM: str := BinaryToDec(actasmpattern);
  2356. AS_OCTALNUM: str := OctalToDec(actasmpattern);
  2357. else
  2358. Message(assem_e_syntax_error);
  2359. end;
  2360. val(str, l, code);
  2361. if code <> 0 then
  2362. Message(assem_e_invalid_scaling_factor);
  2363. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) and (code = 0) then
  2364. begin
  2365. instr.operands[operandnum].ref.scalefactor := l;
  2366. end
  2367. else
  2368. Begin
  2369. Message(assem_e_invalid_scaling_value);
  2370. instr.operands[operandnum].ref.scalefactor := 0;
  2371. end;
  2372. if instr.operands[operandnum].ref.index = R_NO then
  2373. Begin
  2374. Message(assem_e_scaling_value_only_allowed_with_index);
  2375. instr.operands[operandnum].ref.scalefactor := 0;
  2376. end;
  2377. { Consume the scaling number }
  2378. Consume(actasmtoken);
  2379. case actasmtoken of
  2380. { [...*SCALING-expr] ... }
  2381. AS_MINUS: Begin
  2382. if instr.operands[operandnum].ref.offset <> 0 then
  2383. Message(assem_f_internal_error_in_buildscale);
  2384. instr.operands[operandnum].ref.offset :=
  2385. BuildRefExpression;
  2386. end;
  2387. { [...*SCALING+expr] ... }
  2388. AS_PLUS: Begin
  2389. if instr.operands[operandnum].ref.offset <> 0 then
  2390. Message(assem_f_internal_error_in_buildscale);
  2391. instr.operands[operandnum].ref.offset :=
  2392. BuildRefExpression;
  2393. end;
  2394. { [...*SCALING] ... }
  2395. AS_RBRACKET: Consume(AS_RBRACKET);
  2396. else
  2397. Message(assem_e_invalid_scaling_value);
  2398. end;
  2399. { .Field.Field ... or separator/comma }
  2400. Case actasmtoken of
  2401. AS_DOT: BuildRecordOffset(instr,'');
  2402. AS_COMMA, AS_SEPARATOR: ;
  2403. else
  2404. Message(assem_e_syntax_error);
  2405. end;
  2406. end;
  2407. Procedure BuildReference(var instr: TInstruction);
  2408. {*********************************************************************}
  2409. { EXIT CONDITION: On exit the routine should point to either the }
  2410. { AS_COMMA or AS_SEPARATOR token. }
  2411. { On entry: contains the register after the opening bracket if any. }
  2412. {*********************************************************************}
  2413. var
  2414. reg:string;
  2415. segreg: boolean;
  2416. negative: boolean;
  2417. expr: string;
  2418. Begin
  2419. expr := '';
  2420. if instr.operands[operandnum].operandtype <> OPR_REFERENCE then
  2421. Begin
  2422. Message(assem_e_syn_no_ref_with_brackets);
  2423. InitAsmRef(instr);
  2424. consume(AS_REGISTER);
  2425. end
  2426. else
  2427. Begin
  2428. { save the reg }
  2429. reg := actasmpattern;
  2430. consume(AS_REGISTER);
  2431. { is the syntax of the form: [REG:REG...] }
  2432. if actasmtoken = AS_COLON then
  2433. begin
  2434. segreg := TRUE;
  2435. Message(assem_e_expression_form_not_supported);
  2436. if instr.operands[operandnum].ref.segment <> R_NO then
  2437. Message(assem_e_defining_seg_more_than_once);
  2438. instr.operands[operandnum].ref.segment := findsegment(reg);
  2439. { Here we should process the syntax of the form }
  2440. { [reg:reg...] }
  2441. end
  2442. else { SREG:[REG...] where SReg: is optional. }
  2443. Begin
  2444. if instr.operands[operandnum].ref.base <> R_NO then
  2445. Message(assem_e_defining_base_more_than_once);
  2446. instr.operands[operandnum].ref.base := findregister(reg);
  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. instr.operands[operandnum].ref.offset := BuildRefExpression;
  2533. case actasmtoken of
  2534. AS_DOT: BuildRecordOffset(instr,'');
  2535. AS_COMMA,
  2536. AS_SEPARATOR: ;
  2537. else
  2538. Message(assem_e_syntax_error);
  2539. end;
  2540. end; { end if }
  2541. end; { end this case }
  2542. AS_STAR: { REG:[REG*scaling] ... }
  2543. Begin
  2544. BuildScaling(instr);
  2545. end;
  2546. end;
  2547. end; { end outer if }
  2548. end;
  2549. Procedure BuildBracketExpression(var Instr: TInstruction; var_prefix: boolean);
  2550. {*********************************************************************}
  2551. { PROCEDURE BuildBracketExpression }
  2552. { Description: This routine builds up an expression after a LBRACKET }
  2553. { token is encountered. }
  2554. { On entry actasmtoken should be equal to AS_LBRACKET. }
  2555. { var_prefix : Should be set to true if variable identifier has }
  2556. { been defined, such as in ID[ }
  2557. {*********************************************************************}
  2558. { EXIT CONDITION: On exit the routine should point to either the }
  2559. { AS_COMMA or AS_SEPARATOR token. }
  2560. {*********************************************************************}
  2561. var
  2562. l:longint;
  2563. Begin
  2564. Consume(AS_LBRACKET);
  2565. initAsmRef(instr);
  2566. Case actasmtoken of
  2567. { Constant reference expression OR variable reference expression }
  2568. AS_ID: Begin
  2569. if actasmpattern[1] = '@' then
  2570. Message(assem_e_local_symbol_not_allowed_as_ref);
  2571. if SearchIConstant(actasmpattern,l) then
  2572. Begin
  2573. { if there was a variable prefix then }
  2574. { add to offset }
  2575. If var_prefix then
  2576. Begin
  2577. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2578. end
  2579. else
  2580. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2581. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2582. Message(assem_e_invalid_operand_in_bracket_expression);
  2583. end
  2584. else if NOT var_prefix then
  2585. Begin
  2586. InitAsmRef(instr);
  2587. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2588. Message1(assem_e_unknown_id,actasmpattern);
  2589. Consume(AS_ID);
  2590. { is there a constant expression following }
  2591. { the variable name? }
  2592. if actasmtoken <> AS_RBRACKET then
  2593. Begin
  2594. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2595. end
  2596. else
  2597. Consume(AS_RBRACKET);
  2598. end
  2599. else
  2600. Message1(assem_e_invalid_symbol_name,actasmpattern);
  2601. end;
  2602. { Here we handle the special case in tp where }
  2603. { the + operator is allowed with reg and var }
  2604. { references, such as in mov al, byte ptr [+bx] }
  2605. AS_PLUS: Begin
  2606. Consume(AS_PLUS);
  2607. Case actasmtoken of
  2608. AS_REGISTER: Begin
  2609. BuildReference(instr);
  2610. end;
  2611. AS_ID: Begin
  2612. if actasmpattern[1] = '@' then
  2613. Message(assem_e_local_symbol_not_allowed_as_ref);
  2614. if SearchIConstant(actasmpattern,l) then
  2615. Begin
  2616. { if there was a variable prefix then }
  2617. { add to offset }
  2618. If var_prefix then
  2619. Begin
  2620. Inc(instr.operands[operandnum].ref.offset,
  2621. BuildRefExpression);
  2622. end
  2623. else
  2624. instr.operands[operandnum].ref.offset :=
  2625. BuildRefExpression;
  2626. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2627. Message(assem_e_invalid_operand_in_bracket_expression);
  2628. end
  2629. else if NOT var_prefix then
  2630. Begin
  2631. InitAsmRef(instr);
  2632. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2633. Message1(assem_e_unknown_id,actasmpattern);
  2634. Consume(AS_ID);
  2635. { is there a constant expression following }
  2636. { the variable name? }
  2637. if actasmtoken <> AS_RBRACKET then
  2638. Begin
  2639. Inc(instr.operands[operandnum].ref.offset,
  2640. BuildRefExpression);
  2641. end
  2642. else
  2643. Consume(AS_RBRACKET);
  2644. end
  2645. else
  2646. Message1(assem_e_invalid_symbol_name,actasmpattern);
  2647. end;
  2648. { Constant reference expression // }
  2649. AS_INTNUM,AS_BINNUM,AS_OCTALNUM,
  2650. AS_HEXNUM: Begin
  2651. { if there was a variable prefix then }
  2652. { add to offset instead. }
  2653. If var_prefix then
  2654. Begin
  2655. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2656. end
  2657. else
  2658. Begin
  2659. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2660. end;
  2661. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2662. Message(assem_e_invalid_operand_in_bracket_expression);
  2663. end;
  2664. else
  2665. Message(assem_e_syntax_error);
  2666. end;
  2667. end;
  2668. { Constant reference expression // }
  2669. AS_MINUS,AS_NOT,AS_LPAREN:
  2670. Begin
  2671. { if there was a variable prefix then }
  2672. { add to offset instead. }
  2673. If var_prefix then
  2674. Begin
  2675. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2676. end
  2677. else
  2678. Begin
  2679. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2680. end;
  2681. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2682. Message(assem_e_invalid_operand_in_bracket_expression);
  2683. end;
  2684. { Constant reference expression // }
  2685. AS_INTNUM,AS_OCTALNUM,AS_BINNUM,AS_HEXNUM: Begin
  2686. { if there was a variable prefix then }
  2687. { add to offset instead. }
  2688. If var_prefix then
  2689. Begin
  2690. Inc(instr.operands[operandnum].ref.offset, BuildRefExpression);
  2691. end
  2692. else
  2693. Begin
  2694. instr.operands[operandnum].ref.offset :=BuildRefExpression;
  2695. end;
  2696. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2697. Message(assem_e_invalid_operand_in_bracket_expression);
  2698. end;
  2699. { Variable reference expression }
  2700. AS_REGISTER: BuildReference(instr);
  2701. else
  2702. Begin
  2703. Message(assem_e_invalid_reference_syntax);
  2704. while (actasmtoken <> AS_SEPARATOR) do
  2705. Consume(actasmtoken);
  2706. end;
  2707. end; { end case }
  2708. end;
  2709. Procedure BuildOperand(var instr: TInstruction);
  2710. {*********************************************************************}
  2711. { EXIT CONDITION: On exit the routine should point to either the }
  2712. { AS_COMMA or AS_SEPARATOR token. }
  2713. {*********************************************************************}
  2714. var
  2715. tempstr: string;
  2716. expr: string;
  2717. lab: Pasmlabel;
  2718. l : longint;
  2719. hl: plabel;
  2720. Begin
  2721. tempstr := '';
  2722. expr := '';
  2723. case actasmtoken of
  2724. { Constant expression // }
  2725. AS_PLUS,AS_MINUS,AS_NOT,AS_LPAREN:
  2726. Begin
  2727. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2728. Message(assem_e_invalid_operand_type);
  2729. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2730. instr.operands[operandnum].val :=BuildExpression;
  2731. end;
  2732. { Constant expression // }
  2733. AS_STRING: Begin
  2734. if not (instr.operands[operandnum].operandtype in [OPR_NONE]) then
  2735. Message(assem_e_invalid_operand_type);
  2736. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2737. if not PadZero(actasmpattern,4) then
  2738. Message1(assem_e_invalid_string_as_opcode_operand,actasmpattern);
  2739. instr.operands[operandnum].val :=
  2740. ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  2741. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1])
  2742. shl 24;
  2743. Consume(AS_STRING);
  2744. Case actasmtoken of
  2745. AS_COMMA, AS_SEPARATOR: ;
  2746. else
  2747. Message(assem_e_invalid_string_expression);
  2748. end; { end case }
  2749. end;
  2750. { Constant expression // }
  2751. AS_INTNUM,AS_BINNUM,
  2752. AS_OCTALNUM,
  2753. AS_HEXNUM: Begin
  2754. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2755. Message(assem_e_invalid_operand_type);
  2756. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2757. instr.operands[operandnum].val :=BuildExpression;
  2758. end;
  2759. { A constant expression, or a Variable ref. }
  2760. AS_ID: Begin
  2761. if actasmpattern[1] = '@' then
  2762. { Label or Special symbol reference }
  2763. Begin
  2764. if actasmpattern = '@RESULT' then
  2765. Begin
  2766. InitAsmRef(instr);
  2767. SetUpResult(instr,operandnum);
  2768. end
  2769. else
  2770. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  2771. Message(assem_w_CODE_and_DATA_not_supported)
  2772. else
  2773. Begin
  2774. delete(actasmpattern,1,1);
  2775. if actasmpattern = '' then
  2776. Message(assem_e_null_label_ref_not_allowed);
  2777. lab := labellist.search(actasmpattern);
  2778. { check if the label is already defined }
  2779. { if so, we then check if the plabel is }
  2780. { non-nil, if so we add it to instruction }
  2781. if assigned(lab) then
  2782. Begin
  2783. if assigned(lab^.lab) then
  2784. Begin
  2785. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2786. instr.operands[operandnum].hl := lab^.lab;
  2787. instr.labeled := TRUE;
  2788. end;
  2789. end
  2790. else
  2791. { the label does not exist, create it }
  2792. { emit the opcode, but set that the }
  2793. { label has not been emitted }
  2794. Begin
  2795. getlabel(hl);
  2796. labellist.insert(actasmpattern,hl,FALSE);
  2797. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2798. instr.operands[operandnum].hl := hl;
  2799. instr.labeled := TRUE;
  2800. end;
  2801. end;
  2802. Consume(AS_ID);
  2803. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2804. Begin
  2805. Message(assem_e_syntax_error);
  2806. end;
  2807. end
  2808. else
  2809. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  2810. begin
  2811. InitAsmRef(instr);
  2812. SetUpResult(instr,operandnum);
  2813. Consume(AS_ID);
  2814. end
  2815. { probably a variable or normal expression }
  2816. { or a procedure (such as in CALL ID) }
  2817. else
  2818. Begin
  2819. { is it a constant ? }
  2820. if SearchIConstant(actasmpattern,l) then
  2821. Begin
  2822. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  2823. Message(assem_e_invalid_operand_type);
  2824. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  2825. instr.operands[operandnum].val :=BuildExpression;
  2826. end
  2827. else { is it a label variable ? }
  2828. Begin
  2829. { ID[ , ID.Field.Field or simple ID }
  2830. { check if this is a label, if so then }
  2831. { emit it as a label. }
  2832. if SearchLabel(actasmpattern,hl) then
  2833. Begin
  2834. instr.operands[operandnum].operandtype := OPR_LABINSTR;
  2835. instr.operands[operandnum].hl := hl;
  2836. instr.labeled := TRUE;
  2837. Consume(AS_ID);
  2838. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  2839. Message(assem_e_syntax_error);
  2840. end
  2841. else
  2842. { is it a normal variable ? }
  2843. Begin
  2844. initAsmRef(instr);
  2845. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  2846. Begin
  2847. { not a variable.. }
  2848. { check special variables.. }
  2849. if actasmpattern = 'SELF' then
  2850. { special self variable }
  2851. Begin
  2852. if assigned(procinfo._class) then
  2853. Begin
  2854. instr.operands[operandnum].ref.offset := procinfo.ESI_offset;
  2855. instr.operands[operandnum].ref.base := procinfo.framepointer;
  2856. end
  2857. else
  2858. Message(assem_e_cannot_use_SELF_outside_a_method);
  2859. end
  2860. else
  2861. Message1(assem_e_unknown_id,actasmpattern);
  2862. end;
  2863. expr := actasmpattern;
  2864. Consume(AS_ID);
  2865. case actasmtoken of
  2866. AS_LBRACKET: { indexing }
  2867. BuildBracketExpression(instr,TRUE);
  2868. AS_DOT: BuildRecordOffset(instr,expr);
  2869. AS_SEPARATOR,AS_COMMA: ;
  2870. else
  2871. Message(assem_e_syntax_error);
  2872. end;
  2873. end;
  2874. end;
  2875. end;
  2876. end;
  2877. { Register, a variable reference or a constant reference }
  2878. AS_REGISTER: Begin
  2879. { save the type of register used. }
  2880. tempstr := actasmpattern;
  2881. Consume(AS_REGISTER);
  2882. if actasmtoken = AS_COLON then
  2883. Begin
  2884. Consume(AS_COLON);
  2885. if actasmtoken <> AS_LBRACKET then
  2886. Message(assem_e_syn_start_with_bracket)
  2887. else
  2888. Begin
  2889. initAsmRef(instr);
  2890. instr.operands[operandnum].ref.segment := findsegment(tempstr);
  2891. BuildBracketExpression(instr,false);
  2892. end;
  2893. end
  2894. { Simple register }
  2895. else if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  2896. Begin
  2897. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_REGISTER]) then
  2898. Message(assem_e_invalid_operand_type);
  2899. instr.operands[operandnum].operandtype := OPR_REGISTER;
  2900. instr.operands[operandnum].reg := findregister(tempstr);
  2901. end
  2902. else
  2903. Message1(assem_e_syn_register,tempstr);
  2904. end;
  2905. { a variable reference, register ref. or a constant reference }
  2906. AS_LBRACKET: Begin
  2907. BuildBracketExpression(instr,false);
  2908. end;
  2909. { Unsupported }
  2910. AS_SEG,AS_OFFSET: Begin
  2911. Message(assem_e_SEG_and_OFFSET_not_supported);
  2912. Consume(actasmtoken);
  2913. { error recovery }
  2914. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  2915. Consume(actasmtoken);
  2916. end;
  2917. AS_SEPARATOR, AS_COMMA: ;
  2918. else
  2919. Message(assem_e_syn_opcode_operand);
  2920. end; { end case }
  2921. end;
  2922. Procedure BuildConstant(maxvalue: longint);
  2923. {*********************************************************************}
  2924. { PROCEDURE BuildConstant }
  2925. { Description: This routine takes care of parsing a DB,DD,or DW }
  2926. { line and adding those to the assembler node. Expressions, range- }
  2927. { checking are fullly taken care of. }
  2928. { maxvalue: $ff -> indicates that this is a DB node. }
  2929. { $ffff -> indicates that this is a DW node. }
  2930. { $ffffffff -> indicates that this is a DD node. }
  2931. {*********************************************************************}
  2932. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  2933. {*********************************************************************}
  2934. var
  2935. strlength: byte;
  2936. expr: string;
  2937. value : longint;
  2938. Begin
  2939. strlength := 0; { assume it is a DB }
  2940. Repeat
  2941. Case actasmtoken of
  2942. AS_STRING: Begin
  2943. if maxvalue = $ffff then
  2944. strlength := 2
  2945. else if maxvalue = $ffffffff then
  2946. strlength := 4;
  2947. if strlength <> 0 then
  2948. { DD and DW cases }
  2949. Begin
  2950. if Not PadZero(actasmpattern,strlength) then
  2951. Message(scan_f_string_exceeds_line);
  2952. end;
  2953. expr := actasmpattern;
  2954. Consume(AS_STRING);
  2955. Case actasmtoken of
  2956. AS_COMMA: Consume(AS_COMMA);
  2957. AS_SEPARATOR: ;
  2958. else
  2959. Message(assem_e_invalid_string_expression);
  2960. end; { end case }
  2961. ConcatString(p,expr);
  2962. end;
  2963. AS_INTNUM,AS_BINNUM,
  2964. AS_OCTALNUM,AS_HEXNUM:
  2965. Begin
  2966. value:=BuildExpression;
  2967. ConcatConstant(p,value,maxvalue);
  2968. end;
  2969. AS_ID:
  2970. Begin
  2971. value:=BuildExpression;
  2972. if value > maxvalue then
  2973. Begin
  2974. Message(assem_e_expression_out_of_bounds);
  2975. { assuming a value of maxvalue }
  2976. value := maxvalue;
  2977. end;
  2978. ConcatConstant(p,value,maxvalue);
  2979. end;
  2980. { These terms can start an assembler expression }
  2981. AS_PLUS,AS_MINUS,AS_LPAREN,AS_NOT: Begin
  2982. value := BuildExpression;
  2983. ConcatConstant(p,value,maxvalue);
  2984. end;
  2985. AS_COMMA: BEGIN
  2986. Consume(AS_COMMA);
  2987. END;
  2988. AS_SEPARATOR: ;
  2989. else
  2990. Begin
  2991. Message(assem_f_internal_error_in_buildconstant);
  2992. end;
  2993. end; { end case }
  2994. Until actasmtoken = AS_SEPARATOR;
  2995. end;
  2996. Procedure BuildOpCode;
  2997. {*********************************************************************}
  2998. { PROCEDURE BuildOpcode; }
  2999. { Description: Parses the intel opcode and operands, and writes it }
  3000. { in the TInstruction object. }
  3001. {*********************************************************************}
  3002. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  3003. { On ENTRY: Token should point to AS_OPCODE }
  3004. {*********************************************************************}
  3005. var
  3006. asmtok,op : tasmop;
  3007. expr : string;
  3008. segreg : tregister;
  3009. Begin
  3010. expr := '';
  3011. asmtok := A_NONE; { assmume no prefix }
  3012. segreg := R_NO; { assume no segment override }
  3013. { prefix seg opcode }
  3014. { prefix opcode }
  3015. if findprefix(actasmpattern,asmtok) then
  3016. Begin
  3017. { standard opcode prefix }
  3018. if asmtok <> A_NONE then
  3019. instr.addprefix(asmtok);
  3020. Consume(AS_OPCODE);
  3021. if findoverride(actasmpattern,segreg) then
  3022. Begin
  3023. Consume(AS_OPCODE);
  3024. Message(assem_w_repeat_prefix_and_seg_override);
  3025. end;
  3026. end
  3027. { seg prefix opcode }
  3028. { seg opcode }
  3029. else if findoverride(actasmpattern,segreg) then
  3030. Begin
  3031. Consume(AS_OPCODE);
  3032. if findprefix(actasmpattern,asmtok) then
  3033. Begin
  3034. { standard opcode prefix }
  3035. Message(assem_w_repeat_prefix_and_seg_override);
  3036. if asmtok <> A_NONE then
  3037. instr.addprefix(asmtok);
  3038. Consume(AS_OPCODE);
  3039. end;
  3040. end;
  3041. { opcode }
  3042. if (actasmtoken <> AS_OPCODE) then
  3043. Begin
  3044. Message(assem_e_invalid_or_missing_opcode);
  3045. { error recovery }
  3046. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA,AS_END]) do
  3047. Consume(actasmtoken);
  3048. exit;
  3049. end
  3050. else
  3051. Begin
  3052. op := findopcode(actasmpattern);
  3053. instr.addinstr(op);
  3054. { Valid combination of prefix and instruction ? }
  3055. if (asmtok <> A_NONE) and (NOT CheckPrefix(asmtok,op)) then
  3056. Message1(assem_e_invalid_prefix_and_opcode,actasmpattern);
  3057. { Valid combination of segment override }
  3058. if (segreg <> R_NO) and (NOT CheckOverride(segreg,instr)) then
  3059. Message1(assem_e_invalid_override_and_opcode,actasmpattern);
  3060. Consume(AS_OPCODE);
  3061. { Zero operand opcode ? }
  3062. if actasmtoken in [AS_END,AS_SEPARATOR] then
  3063. exit
  3064. else
  3065. operandnum := 1;
  3066. end;
  3067. repeat
  3068. case actasmtoken of
  3069. { End of asm operands for this opcode }
  3070. AS_END,
  3071. AS_SEPARATOR :
  3072. break;
  3073. { Operand delimiter }
  3074. AS_COMMA :
  3075. Begin
  3076. if operandnum > MaxOperands then
  3077. Message(assem_e_too_many_operands)
  3078. else
  3079. Inc(operandnum);
  3080. Consume(AS_COMMA);
  3081. end;
  3082. { Typecast, Constant Expression, Type Specifier }
  3083. AS_DWORD,
  3084. AS_BYTE,
  3085. AS_WORD,
  3086. AS_TBYTE,
  3087. AS_QWORD :
  3088. Begin
  3089. { tell that the instruction was overriden }
  3090. { so we will NEVER override the opsize }
  3091. instr.operands[operandnum].overriden := TRUE;
  3092. Case actasmtoken of
  3093. AS_DWORD : instr.operands[operandnum].size := S_L;
  3094. AS_WORD : instr.operands[operandnum].size := S_W;
  3095. AS_BYTE : instr.operands[operandnum].size := S_B;
  3096. AS_QWORD : instr.operands[operandnum].size := S_IQ;
  3097. AS_TBYTE : instr.operands[operandnum].size := S_FX;
  3098. end;
  3099. Consume(actasmtoken);
  3100. Case actasmtoken of
  3101. { Reference }
  3102. AS_PTR :
  3103. Begin
  3104. initAsmRef(instr);
  3105. Consume(AS_PTR);
  3106. BuildOperand(instr);
  3107. end;
  3108. { Possibly a typecast or a constant }
  3109. { expression. }
  3110. AS_LPAREN :
  3111. Begin
  3112. if actasmtoken = AS_ID then
  3113. Begin
  3114. { Case vartype of }
  3115. { LOCAL: Replace by offset and }
  3116. { BP in treference. }
  3117. { GLOBAL: Replace by mangledname}
  3118. { in symbol of treference }
  3119. { Check if next token = RPAREN }
  3120. { otherwise syntax error. }
  3121. initAsmRef(instr);
  3122. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  3123. Message1(assem_e_unknown_id,actasmpattern);
  3124. end
  3125. else
  3126. begin
  3127. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3128. instr.operands[operandnum].val := BuildExpression;
  3129. end;
  3130. end;
  3131. else
  3132. BuildOperand(instr);
  3133. end; { end case }
  3134. end;
  3135. { Type specifier }
  3136. AS_NEAR,
  3137. AS_FAR :
  3138. Begin
  3139. if actasmtoken = AS_NEAR then
  3140. Message(assem_w_near_ignored)
  3141. else
  3142. Message(assem_w_far_ignored);
  3143. Consume(actasmtoken);
  3144. if actasmtoken = AS_PTR then
  3145. begin
  3146. initAsmRef(instr);
  3147. Consume(AS_PTR);
  3148. end;
  3149. BuildOperand(instr);
  3150. end;
  3151. { Constant expression }
  3152. AS_LPAREN :
  3153. Begin
  3154. instr.operands[operandnum].operandtype := OPR_CONSTANT;
  3155. instr.operands[operandnum].val := BuildExpression;
  3156. end;
  3157. else
  3158. BuildOperand(instr);
  3159. end; { end case }
  3160. until false;
  3161. end;
  3162. Function Assemble: Ptree;
  3163. {*********************************************************************}
  3164. { PROCEDURE Assemble; }
  3165. { Description: Parses the intel assembler syntax, parsing is done }
  3166. { according to the rules in the Turbo Pascal manual. }
  3167. {*********************************************************************}
  3168. Var
  3169. hl : plabel;
  3170. labelptr : pasmlabel;
  3171. Begin
  3172. Message(assem_d_start_intel);
  3173. inexpression := FALSE;
  3174. firsttoken := TRUE;
  3175. operandnum := 0;
  3176. if assigned(procinfo.retdef) and
  3177. (is_fpu(procinfo.retdef) or
  3178. ret_in_acc(procinfo.retdef)) then
  3179. procinfo.funcret_is_valid:=true;
  3180. { sets up all opcode and register tables in uppercase }
  3181. if not _asmsorted then
  3182. Begin
  3183. SetupTables;
  3184. _asmsorted := TRUE;
  3185. end;
  3186. p:=new(paasmoutput,init);
  3187. { setup label linked list }
  3188. labellist.init;
  3189. c:=current_scanner^.asmgetchar;
  3190. actasmtoken:=gettoken;
  3191. repeat
  3192. case actasmtoken of
  3193. AS_LLABEL :
  3194. Begin
  3195. labelptr := labellist.search(actasmpattern);
  3196. if not assigned(labelptr) then
  3197. Begin
  3198. getlabel(hl);
  3199. labellist.insert(actasmpattern,hl,TRUE);
  3200. ConcatLabel(p,A_LABEL,hl);
  3201. end
  3202. else
  3203. { the label has already been inserted into the }
  3204. { label list, either as an intruction label (in }
  3205. { this case it has not been emitted), or as a }
  3206. { duplicate local symbol (in this case it has }
  3207. { already been emitted). }
  3208. Begin
  3209. if labelptr^.emitted then
  3210. Message1(assem_e_dup_local_sym,'@'+labelptr^.name^)
  3211. else
  3212. Begin
  3213. if assigned(labelptr^.lab) then
  3214. ConcatLabel(p,A_LABEL,labelptr^.lab);
  3215. labelptr^.emitted := TRUE;
  3216. end;
  3217. end;
  3218. Consume(AS_LLABEL);
  3219. end;
  3220. AS_LABEL :
  3221. Begin
  3222. if SearchLabel(actasmpattern,hl) then
  3223. ConcatLabel(p,A_LABEL, hl)
  3224. else
  3225. Message1(assem_e_unknown_label_identifer,actasmpattern);
  3226. Consume(AS_LABEL);
  3227. end;
  3228. AS_DW :
  3229. Begin
  3230. Consume(AS_DW);
  3231. BuildConstant($ffff);
  3232. end;
  3233. AS_DB :
  3234. Begin
  3235. Consume(AS_DB);
  3236. BuildConstant($ff);
  3237. end;
  3238. AS_DD :
  3239. Begin
  3240. Consume(AS_DD);
  3241. BuildConstant($ffffffff);
  3242. end;
  3243. AS_OPCODE :
  3244. Begin
  3245. instr.init;
  3246. BuildOpcode;
  3247. instr.numops := operandnum;
  3248. if instr.labeled then
  3249. ConcatLabeledInstr(instr)
  3250. else
  3251. ConcatOpCode(instr);
  3252. instr.done;
  3253. end;
  3254. AS_SEPARATOR :
  3255. Begin
  3256. Consume(AS_SEPARATOR);
  3257. { let us go back to the first operand }
  3258. operandnum := 0;
  3259. end;
  3260. AS_END :
  3261. break; { end assembly block }
  3262. else
  3263. Begin
  3264. Message(assem_e_assemble_node_syntax_error);
  3265. { error recovery }
  3266. Consume(actasmtoken);
  3267. end;
  3268. end; { end case }
  3269. until false;
  3270. { check if there were undefined symbols. }
  3271. { if so, then list each of those undefined }
  3272. { labels. }
  3273. if assigned(labellist.First) then
  3274. Begin
  3275. labelptr := labellist.First;
  3276. if labellist.First <> nil then
  3277. Begin
  3278. { first label }
  3279. if not labelptr^.emitted then
  3280. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  3281. { other labels ... }
  3282. While (labelptr^.Next <> nil) do
  3283. Begin
  3284. labelptr := labelptr^.Next;
  3285. if not labelptr^.emitted then
  3286. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  3287. end;
  3288. end;
  3289. end;
  3290. assemble := genasmnode(p);
  3291. labellist.done;
  3292. Message(assem_d_finish_intel);
  3293. end;
  3294. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  3295. begin
  3296. if assigned(iasmops) then
  3297. dispose(iasmops);
  3298. exitproc:=old_exit;
  3299. end;
  3300. begin
  3301. old_exit:=exitproc;
  3302. exitproc:=@ra386int_exit;
  3303. end.
  3304. {
  3305. $Log$
  3306. Revision 1.20 1999-01-10 15:37:58 peter
  3307. * moved some tables from ra386*.pas -> i386.pas
  3308. + start of coff writer
  3309. * renamed asmutils unit to rautils
  3310. Revision 1.19 1998/12/23 22:55:57 peter
  3311. + rec.field(%esi) support
  3312. + [esi+rec.field] support
  3313. Revision 1.18 1998/12/11 00:03:43 peter
  3314. + globtype,tokens,version unit splitted from globals
  3315. Revision 1.17 1998/12/08 23:03:46 jonas
  3316. * allow constant offsets for go32v2 in assembler blocks
  3317. Revision 1.16 1998/12/02 16:23:36 jonas
  3318. * changed "if longintvar in set" to case or "if () or () .." statements
  3319. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  3320. Revision 1.15 1998/11/29 12:47:22 peter
  3321. * fixes for 'asm sti end;'
  3322. Revision 1.14 1998/11/16 15:38:56 peter
  3323. * fixed instruct not in table msg
  3324. Revision 1.13 1998/11/15 14:37:16 peter
  3325. + support for result in delphimode
  3326. Revision 1.12 1998/11/13 15:40:30 pierre
  3327. + added -Se in Makefile cvstest target
  3328. + lexlevel cleanup
  3329. normal_function_level main_program_level and unit_init_level defined
  3330. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  3331. (test added in code !)
  3332. * -Un option was wrong
  3333. * _FAIL and _SELF only keyword inside
  3334. constructors and methods respectively
  3335. Revision 1.11 1998/11/13 10:12:11 peter
  3336. * constant fixes
  3337. Revision 1.10 1998/11/05 23:48:27 peter
  3338. * recordtype.field support in constant expressions
  3339. * fixed imul for oa_imm8 which was not allowed
  3340. * fixed reading of local typed constants
  3341. * fixed comment reading which is not any longer a separator
  3342. Revision 1.9 1998/10/13 16:50:17 pierre
  3343. * undid some changes of Peter that made the compiler wrong
  3344. for m68k (I had to reinsert some ifdefs)
  3345. * removed several memory leaks under m68k
  3346. * removed the meory leaks for assembler readers
  3347. * cross compiling shoud work again better
  3348. ( crosscompiling sysamiga works
  3349. but as68k still complain about some code !)
  3350. Revision 1.8 1998/10/07 04:29:44 carl
  3351. * Concatlabel now gives output on error
  3352. * in/out bugfix (still ins/outs left to fix)
  3353. Revision 1.7 1998/09/02 01:23:40 carl
  3354. * bugfix of operand overrides, VERY stupid bugfix BTW...
  3355. Revision 1.6 1998/08/27 00:42:17 carl
  3356. * bugfix of leal problem
  3357. * bugfix of using overrides with record offsets
  3358. * bugfix if using records to load values
  3359. Revision 1.5 1998/08/21 08:45:53 pierre
  3360. * better line info for asm statements
  3361. Revision 1.4 1998/07/14 14:47:00 peter
  3362. * released NEWINPUT
  3363. Revision 1.3 1998/07/07 11:20:09 peter
  3364. + NEWINPUT for a better inputfile and scanner object
  3365. Revision 1.2 1998/06/24 14:06:38 peter
  3366. * fixed the name changes
  3367. Revision 1.1 1998/06/23 14:00:18 peter
  3368. * renamed RA* units
  3369. Revision 1.11 1998/06/16 08:56:28 peter
  3370. + targetcpu
  3371. * cleaner pmodules for newppu
  3372. Revision 1.10 1998/06/12 10:32:33 pierre
  3373. * column problem hopefully solved
  3374. + C vars declaration changed
  3375. Revision 1.9 1998/05/31 14:13:32 peter
  3376. * fixed call bugs with assembler readers
  3377. + OPR_SYMBOL to hold a symbol in the asm parser
  3378. * fixed staticsymtable vars which were acessed through %ebp instead of
  3379. name
  3380. Revision 1.8 1998/05/30 14:31:07 peter
  3381. + $ASMMODE
  3382. Revision 1.7 1998/05/28 16:32:05 carl
  3383. * bugfix with operands main branch version (patched manually)
  3384. Revision 1.6 1998/05/23 01:21:26 peter
  3385. + aktasmmode, aktoptprocessor, aktoutputformat
  3386. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  3387. + $LIBNAME to set the library name where the unit will be put in
  3388. * splitted cgi386 a bit (codeseg to large for bp7)
  3389. * nasm, tasm works again. nasm moved to ag386nsm.pas
  3390. Revision 1.5 1998/05/20 09:42:36 pierre
  3391. + UseTokenInfo now default
  3392. * unit in interface uses and implementation uses gives error now
  3393. * only one error for unknown symbol (uses lastsymknown boolean)
  3394. the problem came from the label code !
  3395. + first inlined procedures and function work
  3396. (warning there might be allowed cases were the result is still wrong !!)
  3397. * UseBrower updated gives a global list of all position of all used symbols
  3398. with switch -gb
  3399. Revision 1.4 1998/04/29 10:34:03 pierre
  3400. + added some code for ansistring (not complete nor working yet)
  3401. * corrected operator overloading
  3402. * corrected nasm output
  3403. + started inline procedures
  3404. + added starstarn : use ** for exponentiation (^ gave problems)
  3405. + started UseTokenInfo cond to get accurate positions
  3406. Revision 1.3 1998/04/08 16:58:06 pierre
  3407. * several bugfixes
  3408. ADD ADC and AND are also sign extended
  3409. nasm output OK (program still crashes at end
  3410. and creates wrong assembler files !!)
  3411. procsym types sym in tdef removed !!
  3412. }