ra386int.pas 135 KB

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