rax86int.pas 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing process for the intel styled inline assembler.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. Unit Rax86int;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. cpubase,
  23. globtype,
  24. aasmbase,
  25. rasm,
  26. rax86;
  27. type
  28. tasmtoken = (
  29. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  30. AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  31. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  32. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  33. {------------------ Assembler directives --------------------}
  34. AS_ALIGN,AS_DB,AS_DW,AS_DD,AS_DQ,AS_PUBLIC,AS_END,
  35. {------------------ Assembler Operators --------------------}
  36. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_NEAR,AS_FAR,
  37. AS_HIGH,AS_LOW,AS_OFFSET,AS_SIZEOF,AS_VMTOFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  38. AS_AND,AS_OR,AS_XOR,AS_WRT,AS___GOTPCREL);
  39. type
  40. tx86intreader = class(tasmreader)
  41. actasmpattern_origcase : string;
  42. actasmtoken : tasmtoken;
  43. prevasmtoken : tasmtoken;
  44. ActOpsize : topsize;
  45. inexpression : boolean;
  46. constructor create;override;
  47. function is_asmopcode(const s: string):boolean;
  48. function is_asmoperator(const s: string):boolean;
  49. function is_asmdirective(const s: string):boolean;
  50. function is_register(const s:string):boolean;
  51. function is_locallabel(const s:string):boolean;
  52. function Assemble: tlinkedlist;override;
  53. procedure GetToken;
  54. function consume(t : tasmtoken):boolean;
  55. procedure RecoverConsume(allowcomma:boolean);
  56. procedure BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  57. procedure BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype;out isseg,is_farproc_entry:boolean);
  58. function BuildConstExpression:aint;
  59. function BuildRefConstExpression(startingminus:boolean=false):aint;
  60. procedure BuildReference(oper : tx86operand);
  61. procedure BuildOperand(oper: tx86operand;istypecast:boolean);
  62. procedure BuildConstantOperand(oper: tx86operand);
  63. procedure BuildOpCode(instr : tx86instruction);
  64. procedure BuildConstant(constsize: byte);
  65. end;
  66. implementation
  67. uses
  68. { common }
  69. cutils,
  70. { global }
  71. globals,verbose,
  72. systems,
  73. { aasm }
  74. aasmdata,aasmcpu,
  75. {$ifdef i8086}
  76. aasmtai,
  77. {$endif i8086}
  78. { symtable }
  79. symconst,symbase,symtype,symsym,symdef,
  80. {$ifdef i8086}
  81. symcpu,
  82. {$endif i8086}
  83. { parser }
  84. scanner,pbase,
  85. { register allocator }
  86. rautils,itx86int,
  87. { codegen }
  88. cgbase,procinfo,paramgr
  89. ;
  90. type
  91. tasmkeyword = string[9];
  92. const
  93. { These tokens should be modified accordingly to the modifications }
  94. { in the different enumerations. }
  95. firstdirective = AS_ALIGN;
  96. lastdirective = AS_END;
  97. firstoperator = AS_BYTE;
  98. lastoperator = AS___GOTPCREL;
  99. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  100. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  101. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  102. ('ALIGN','DB','DW','DD','DQ','PUBLIC','END');
  103. { problems with shl,shr,not,and,or and xor, they are }
  104. { context sensitive. }
  105. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  106. 'BYTE','WORD','DWORD','QWORD','TBYTE','DQWORD','OWORD','XMMWORD','YWORD','YMMWORD','NEAR','FAR','HIGH',
  107. 'LOW','OFFSET','SIZEOF','VMTOFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  108. 'OR','XOR','WRT','GOTPCREL');
  109. token2str : array[tasmtoken] of string[10] = (
  110. '','Label','LLabel','String','Integer',
  111. ',','[',']','(',
  112. ')',':','.','+','-','*',
  113. ';','identifier','register','opcode','/',
  114. '','','','','','','END',
  115. '','','','','','','','','','','','',
  116. '','','','sizeof','vmtoffset','','type','ptr','mod','shl','shr','not',
  117. 'and','or','xor','wrt','..gotpcrel'
  118. );
  119. constructor tx86intreader.create;
  120. var
  121. i : tasmop;
  122. Begin
  123. inherited create;
  124. iasmops:=TFPHashList.create;
  125. for i:=firstop to lastop do
  126. iasmops.Add(upper(std_op2str[i]),Pointer(PtrInt(i)));
  127. end;
  128. {---------------------------------------------------------------------}
  129. { Routines for the tokenizing }
  130. {---------------------------------------------------------------------}
  131. function tx86intreader.is_asmopcode(const s: string):boolean;
  132. var
  133. cond : string[4];
  134. cnd : tasmcond;
  135. j: longint;
  136. Begin
  137. is_asmopcode:=FALSE;
  138. actopcode:=A_None;
  139. actcondition:=C_None;
  140. actopsize:=S_NO;
  141. { Search opcodes }
  142. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  143. if actopcode<>A_NONE then
  144. begin
  145. actasmtoken:=AS_OPCODE;
  146. result:=TRUE;
  147. exit;
  148. end;
  149. { not found yet, check condition opcodes }
  150. j:=0;
  151. while (j<CondAsmOps) do
  152. begin
  153. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  154. begin
  155. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  156. if cond<>'' then
  157. begin
  158. for cnd:=low(TasmCond) to high(TasmCond) do
  159. if Cond=Upper(cond2str[cnd]) then
  160. begin
  161. actopcode:=CondASmOp[j];
  162. actcondition:=cnd;
  163. is_asmopcode:=TRUE;
  164. actasmtoken:=AS_OPCODE;
  165. exit
  166. end;
  167. end;
  168. end;
  169. inc(j);
  170. end;
  171. end;
  172. function tx86intreader.is_asmoperator(const s: string):boolean;
  173. var
  174. i : longint;
  175. Begin
  176. for i:=0 to _count_asmoperators do
  177. if s=_asmoperators[i] then
  178. begin
  179. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  180. is_asmoperator:=true;
  181. exit;
  182. end;
  183. is_asmoperator:=false;
  184. end;
  185. Function tx86intreader.is_asmdirective(const s: string):boolean;
  186. var
  187. i : longint;
  188. Begin
  189. for i:=0 to _count_asmdirectives do
  190. if s=_asmdirectives[i] then
  191. begin
  192. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  193. is_asmdirective:=true;
  194. exit;
  195. end;
  196. is_asmdirective:=false;
  197. end;
  198. function tx86intreader.is_register(const s:string):boolean;
  199. begin
  200. is_register:=false;
  201. actasmregister:=masm_regnum_search(lower(s));
  202. { don't acceps "flags" as register name in an instruction }
  203. if (getsupreg(actasmregister)=RS_DEFAULTFLAGS) and (getregtype(actasmregister)=getregtype(NR_DEFAULTFLAGS)) then
  204. actasmregister:=NR_NO;
  205. if actasmregister<>NR_NO then
  206. begin
  207. is_register:=true;
  208. actasmtoken:=AS_REGISTER;
  209. end;
  210. end;
  211. function tx86intreader.is_locallabel(const s:string):boolean;
  212. begin
  213. is_locallabel:=(length(s)>1) and (s[1]='@');
  214. end;
  215. Procedure tx86intreader.GetToken;
  216. var
  217. len : longint;
  218. forcelabel : boolean;
  219. srsym : tsym;
  220. srsymtable : TSymtable;
  221. begin
  222. c:=scanner.c;
  223. { save old token and reset new token }
  224. prevasmtoken:=actasmtoken;
  225. actasmtoken:=AS_NONE;
  226. { reset }
  227. forcelabel:=FALSE;
  228. actasmpattern:='';
  229. { while space and tab , continue scan... }
  230. while (c in [' ',#9]) do
  231. c:=current_scanner.asmgetchar;
  232. { get token pos }
  233. if not (c in [#10,#13,'{',';','(','/']) then
  234. current_scanner.gettokenpos;
  235. { Local Label, Label, Directive, Prefix or Opcode }
  236. if firsttoken and not (c in [#10,#13,'{',';','(','/']) then
  237. begin
  238. firsttoken:=FALSE;
  239. len:=0;
  240. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  241. begin
  242. { if there is an at_sign, then this must absolutely be a label }
  243. if c = '@' then
  244. forcelabel:=TRUE;
  245. inc(len);
  246. actasmpattern[len]:=c;
  247. c:=current_scanner.asmgetchar;
  248. end;
  249. actasmpattern[0]:=chr(len);
  250. actasmpattern_origcase:=actasmpattern;
  251. uppervar(actasmpattern);
  252. { allow spaces }
  253. while (c in [' ',#9]) do
  254. c:=current_scanner.asmgetchar;
  255. { label ? }
  256. if c = ':' then
  257. begin
  258. if actasmpattern[1]='@' then
  259. actasmtoken:=AS_LLABEL
  260. else
  261. actasmtoken:=AS_LABEL;
  262. { let us point to the next character }
  263. c:=current_scanner.asmgetchar;
  264. firsttoken:=true;
  265. exit;
  266. end;
  267. { Are we trying to create an identifier with }
  268. { an at-sign...? }
  269. if forcelabel then
  270. Message(asmr_e_none_label_contain_at);
  271. { opcode ? }
  272. If is_asmopcode(actasmpattern) then
  273. Begin
  274. { check if we are in an expression }
  275. { then continue with asm directives }
  276. if not inexpression then
  277. exit;
  278. end;
  279. if is_asmdirective(actasmpattern) then
  280. exit;
  281. message1(asmr_e_unknown_opcode,actasmpattern);
  282. actasmtoken:=AS_NONE;
  283. exit;
  284. end
  285. else { else firsttoken }
  286. begin
  287. case c of
  288. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  289. { - @Result, @Code or @Data special variables. }
  290. begin
  291. actasmpattern:=c;
  292. c:=current_scanner.asmgetchar;
  293. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  294. begin
  295. actasmpattern:=actasmpattern + c;
  296. c:=current_scanner.asmgetchar;
  297. end;
  298. actasmpattern_origcase:=actasmpattern;
  299. uppervar(actasmpattern);
  300. actasmtoken:=AS_ID;
  301. exit;
  302. end;
  303. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  304. begin
  305. actasmpattern:=c;
  306. c:=current_scanner.asmgetchar;
  307. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  308. begin
  309. actasmpattern:=actasmpattern + c;
  310. c:=current_scanner.asmgetchar;
  311. end;
  312. actasmpattern_origcase:=actasmpattern;
  313. uppervar(actasmpattern);
  314. { after prefix (or segment override) we allow also a new opcode }
  315. If (is_prefix(actopcode) or is_override(actopcode)) and is_asmopcode(actasmpattern) then
  316. Begin
  317. { if we are not in a constant }
  318. { expression than this is an }
  319. { opcode. }
  320. if not inexpression then
  321. exit;
  322. end;
  323. { support st(X) for fpu registers }
  324. if (actasmpattern = 'ST') and (c='(') then
  325. Begin
  326. actasmpattern:=actasmpattern+c;
  327. c:=current_scanner.asmgetchar;
  328. { allow spaces }
  329. while (c in [' ',#9]) do
  330. c:=current_scanner.asmgetchar;
  331. if c in ['0'..'7'] then
  332. actasmpattern:=actasmpattern + c
  333. else
  334. Message(asmr_e_invalid_fpu_register);
  335. c:=current_scanner.asmgetchar;
  336. { allow spaces }
  337. while (c in [' ',#9]) do
  338. c:=current_scanner.asmgetchar;
  339. if c <> ')' then
  340. Message(asmr_e_invalid_fpu_register)
  341. else
  342. Begin
  343. actasmpattern:=actasmpattern + c;
  344. c:=current_scanner.asmgetchar;
  345. end;
  346. end;
  347. if is_asmdirective(actasmpattern) then
  348. exit;
  349. if is_asmoperator(actasmpattern) then
  350. exit;
  351. if is_register(actasmpattern) then
  352. exit;
  353. { allow spaces }
  354. while (c in [' ',#9]) do
  355. c:=current_scanner.asmgetchar;
  356. { if next is a '.' and this is a unitsym then we also need to
  357. parse the identifier }
  358. if (c='.') then
  359. begin
  360. asmsearchsym(actasmpattern,srsym,srsymtable);
  361. if assigned(srsym) and
  362. (srsym.typ=unitsym) and
  363. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  364. srsym.owner.iscurrentunit then
  365. begin
  366. { Add . to create System.Identifier }
  367. actasmpattern:=actasmpattern+c;
  368. c:=current_scanner.asmgetchar;
  369. { Delphi allows System.@Halt, just ignore the @ }
  370. if c='@' then
  371. c:=current_scanner.asmgetchar;
  372. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  373. begin
  374. actasmpattern:=actasmpattern + upcase(c);
  375. c:=current_scanner.asmgetchar;
  376. end;
  377. end;
  378. end;
  379. actasmtoken:=AS_ID;
  380. exit;
  381. end;
  382. '''' : { string or character }
  383. begin
  384. actasmpattern:='';
  385. repeat
  386. if c = '''' then
  387. begin
  388. c:=current_scanner.asmgetchar;
  389. if c in [#10,#13] then
  390. begin
  391. Message(scan_f_string_exceeds_line);
  392. break;
  393. end;
  394. repeat
  395. if c='''' then
  396. begin
  397. c:=current_scanner.asmgetchar;
  398. if c='''' then
  399. begin
  400. actasmpattern:=actasmpattern+'''';
  401. c:=current_scanner.asmgetchar;
  402. if c in [#10,#13] then
  403. begin
  404. Message(scan_f_string_exceeds_line);
  405. break;
  406. end;
  407. end
  408. else
  409. break;
  410. end
  411. else
  412. begin
  413. actasmpattern:=actasmpattern+c;
  414. c:=current_scanner.asmgetchar;
  415. if c in [#10,#13] then
  416. begin
  417. Message(scan_f_string_exceeds_line);
  418. break
  419. end;
  420. end;
  421. until false; { end repeat }
  422. end
  423. else
  424. break; { end if }
  425. until false;
  426. actasmtoken:=AS_STRING;
  427. exit;
  428. end;
  429. '"' : { string or character }
  430. begin
  431. actasmpattern:='';
  432. repeat
  433. if c = '"' then
  434. begin
  435. c:=current_scanner.asmgetchar;
  436. if c in [#10,#13] then
  437. begin
  438. Message(scan_f_string_exceeds_line);
  439. break;
  440. end;
  441. repeat
  442. if c='"' then
  443. begin
  444. c:=current_scanner.asmgetchar;
  445. if c='"' then
  446. begin
  447. actasmpattern:=actasmpattern+'"';
  448. c:=current_scanner.asmgetchar;
  449. if c in [#10,#13] then
  450. begin
  451. Message(scan_f_string_exceeds_line);
  452. break;
  453. end;
  454. end
  455. else
  456. break;
  457. end
  458. else
  459. begin
  460. actasmpattern:=actasmpattern+c;
  461. c:=current_scanner.asmgetchar;
  462. if c in [#10,#13] then
  463. begin
  464. Message(scan_f_string_exceeds_line);
  465. break
  466. end;
  467. end;
  468. until false; { end repeat }
  469. end
  470. else
  471. break; { end if }
  472. until false;
  473. actasmtoken:=AS_STRING;
  474. exit;
  475. end;
  476. '$' :
  477. begin
  478. c:=current_scanner.asmgetchar;
  479. while c in ['0'..'9','A'..'F','a'..'f'] do
  480. begin
  481. actasmpattern:=actasmpattern + c;
  482. c:=current_scanner.asmgetchar;
  483. end;
  484. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  485. actasmtoken:=AS_INTNUM;
  486. exit;
  487. end;
  488. '&' : { identifier }
  489. begin
  490. actasmpattern:='';
  491. c:=current_scanner.asmgetchar;
  492. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  493. begin
  494. actasmpattern:=actasmpattern + c;
  495. c:=current_scanner.asmgetchar;
  496. end;
  497. actasmpattern_origcase:=actasmpattern;
  498. uppervar(actasmpattern);
  499. actasmtoken:=AS_ID;
  500. exit;
  501. end;
  502. ',' :
  503. begin
  504. actasmtoken:=AS_COMMA;
  505. c:=current_scanner.asmgetchar;
  506. exit;
  507. end;
  508. '[' :
  509. begin
  510. actasmtoken:=AS_LBRACKET;
  511. c:=current_scanner.asmgetchar;
  512. exit;
  513. end;
  514. ']' :
  515. begin
  516. actasmtoken:=AS_RBRACKET;
  517. c:=current_scanner.asmgetchar;
  518. exit;
  519. end;
  520. '(' :
  521. begin
  522. c:=current_scanner.asmgetchar;
  523. if c='*' then
  524. begin
  525. current_scanner.skipoldtpcomment(true);
  526. GetToken;
  527. end
  528. else
  529. actasmtoken:=AS_LPAREN;
  530. exit;
  531. end;
  532. ')' :
  533. begin
  534. actasmtoken:=AS_RPAREN;
  535. c:=current_scanner.asmgetchar;
  536. exit;
  537. end;
  538. ':' :
  539. begin
  540. actasmtoken:=AS_COLON;
  541. c:=current_scanner.asmgetchar;
  542. exit;
  543. end;
  544. '.' :
  545. begin
  546. c:=current_scanner.asmgetchar;
  547. {$ifdef x86_64}
  548. if c='.' then
  549. begin
  550. actasmpattern:='..';
  551. c:=current_scanner.asmgetchar;
  552. repeat
  553. actasmpattern:=actasmpattern+c;
  554. c:=current_scanner.asmgetchar;
  555. until not(c in ['A'..'Z','a'..'z','0'..'9','_']);
  556. if upper(actasmpattern)<>'..GOTPCREL' then
  557. begin
  558. actasmtoken:=AS_ID;
  559. consume(AS___GOTPCREL);
  560. end;
  561. actasmtoken:=AS___GOTPCREL
  562. end
  563. else
  564. {$endif x86_64}
  565. actasmtoken:=AS_DOT;
  566. exit;
  567. end;
  568. '+' :
  569. begin
  570. actasmtoken:=AS_PLUS;
  571. c:=current_scanner.asmgetchar;
  572. exit;
  573. end;
  574. '-' :
  575. begin
  576. actasmtoken:=AS_MINUS;
  577. c:=current_scanner.asmgetchar;
  578. exit;
  579. end;
  580. '*' :
  581. begin
  582. actasmtoken:=AS_STAR;
  583. c:=current_scanner.asmgetchar;
  584. exit;
  585. end;
  586. '/' :
  587. begin
  588. c:=current_scanner.asmgetchar;
  589. if c='/' then
  590. begin
  591. current_scanner.skipdelphicomment;
  592. GetToken;
  593. end
  594. else
  595. actasmtoken:=AS_SLASH;
  596. exit;
  597. end;
  598. '0'..'9':
  599. begin
  600. actasmpattern:=c;
  601. c:=current_scanner.asmgetchar;
  602. { Get the possible characters }
  603. while c in ['0'..'9','A'..'F','a'..'f'] do
  604. begin
  605. actasmpattern:=actasmpattern + c;
  606. c:=current_scanner.asmgetchar;
  607. end;
  608. { Get ending character }
  609. actasmpattern_origcase:=actasmpattern;
  610. uppervar(actasmpattern);
  611. c:=upcase(c);
  612. { possibly a binary number. }
  613. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  614. Begin
  615. { Delete the last binary specifier }
  616. delete(actasmpattern,length(actasmpattern),1);
  617. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  618. actasmtoken:=AS_INTNUM;
  619. exit;
  620. end
  621. else
  622. Begin
  623. case c of
  624. 'O' :
  625. Begin
  626. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  627. actasmtoken:=AS_INTNUM;
  628. c:=current_scanner.asmgetchar;
  629. exit;
  630. end;
  631. 'H' :
  632. Begin
  633. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  634. actasmtoken:=AS_INTNUM;
  635. c:=current_scanner.asmgetchar;
  636. exit;
  637. end;
  638. else { must be an integer number }
  639. begin
  640. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  641. actasmtoken:=AS_INTNUM;
  642. exit;
  643. end;
  644. end;
  645. end;
  646. end;
  647. #13,#10:
  648. begin
  649. current_scanner.linebreak;
  650. c:=current_scanner.asmgetchar;
  651. firsttoken:=TRUE;
  652. actasmtoken:=AS_SEPARATOR;
  653. exit;
  654. end;
  655. ';':
  656. begin
  657. c:=current_scanner.asmgetchar;
  658. firsttoken:=TRUE;
  659. actasmtoken:=AS_SEPARATOR;
  660. exit;
  661. end;
  662. '{':
  663. begin
  664. current_scanner.skipcomment(true);
  665. GetToken;
  666. end;
  667. else
  668. current_scanner.illegal_char(c);
  669. end;
  670. end;
  671. end;
  672. function tx86intreader.consume(t : tasmtoken):boolean;
  673. begin
  674. Consume:=true;
  675. if t<>actasmtoken then
  676. begin
  677. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  678. Consume:=false;
  679. end;
  680. repeat
  681. gettoken;
  682. until actasmtoken<>AS_NONE;
  683. end;
  684. procedure tx86intreader.RecoverConsume(allowcomma:boolean);
  685. begin
  686. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  687. begin
  688. if allowcomma and (actasmtoken=AS_COMMA) then
  689. break;
  690. Consume(actasmtoken);
  691. end;
  692. end;
  693. {*****************************************************************************
  694. Parsing Helpers
  695. *****************************************************************************}
  696. { This routine builds up a record offset after a AS_DOT
  697. token is encountered.
  698. On entry actasmtoken should be equal to AS_DOT }
  699. Procedure tx86intreader.BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  700. var
  701. s: string;
  702. Begin
  703. offset:=0;
  704. size:=0;
  705. hastypecast:=false;
  706. s:=expr;
  707. while (actasmtoken=AS_DOT) do
  708. begin
  709. Consume(AS_DOT);
  710. if actasmtoken in [AS_BYTE,AS_ID,AS_WORD,AS_DWORD,AS_QWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_REGISTER] then
  711. begin
  712. s:=s+'.'+actasmpattern;
  713. consume(actasmtoken);
  714. end
  715. else
  716. begin
  717. Consume(AS_ID);
  718. RecoverConsume(true);
  719. break;
  720. end;
  721. end;
  722. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  723. Message(asmr_e_building_record_offset);
  724. end;
  725. Procedure tx86intreader.BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype;out isseg,is_farproc_entry:boolean);
  726. var
  727. tempstr,expr,hs,mangledname : string;
  728. parenlevel : longint;
  729. l,k : tcgint;
  730. hasparen,
  731. errorflag,
  732. needvmtofs : boolean;
  733. prevtok : tasmtoken;
  734. hl : tasmlabel;
  735. hssymtyp : Tasmsymtype;
  736. def : tdef;
  737. sym : tsym;
  738. srsymtable : TSymtable;
  739. hastypecast : boolean;
  740. Begin
  741. { reset }
  742. value:=0;
  743. asmsym:='';
  744. asmsymtyp:=AT_DATA;
  745. isseg:=false;
  746. is_farproc_entry:=FALSE;
  747. errorflag:=FALSE;
  748. tempstr:='';
  749. expr:='';
  750. if startingminus then
  751. expr:='-';
  752. inexpression:=TRUE;
  753. parenlevel:=0;
  754. sym:=nil;
  755. needvmtofs:=FALSE;
  756. Repeat
  757. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  758. if isref and (actasmtoken=AS_LBRACKET) then
  759. break;
  760. Case actasmtoken of
  761. AS_LPAREN:
  762. Begin
  763. Consume(AS_LPAREN);
  764. expr:=expr + '(';
  765. inc(parenlevel);
  766. end;
  767. AS_RPAREN:
  768. Begin
  769. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  770. if parenlevel=0 then
  771. break;
  772. Consume(AS_RPAREN);
  773. expr:=expr + ')';
  774. dec(parenlevel);
  775. end;
  776. AS_SHL:
  777. Begin
  778. Consume(AS_SHL);
  779. expr:=expr + '<';
  780. end;
  781. AS_SHR:
  782. Begin
  783. Consume(AS_SHR);
  784. expr:=expr + '>';
  785. end;
  786. AS_SLASH:
  787. Begin
  788. Consume(AS_SLASH);
  789. expr:=expr + '/';
  790. end;
  791. AS_MOD:
  792. Begin
  793. Consume(AS_MOD);
  794. expr:=expr + '%';
  795. end;
  796. AS_STAR:
  797. Begin
  798. Consume(AS_STAR);
  799. if isref and (actasmtoken=AS_REGISTER) then
  800. break;
  801. expr:=expr + '*';
  802. end;
  803. AS_PLUS:
  804. Begin
  805. Consume(AS_PLUS);
  806. if isref and (actasmtoken=AS_REGISTER) then
  807. break;
  808. expr:=expr + '+';
  809. end;
  810. AS_MINUS:
  811. Begin
  812. Consume(AS_MINUS);
  813. expr:=expr + '-';
  814. end;
  815. AS_AND:
  816. Begin
  817. Consume(AS_AND);
  818. expr:=expr + '&';
  819. end;
  820. AS_NOT:
  821. Begin
  822. Consume(AS_NOT);
  823. expr:=expr + '~';
  824. end;
  825. AS_XOR:
  826. Begin
  827. Consume(AS_XOR);
  828. expr:=expr + '^';
  829. end;
  830. AS_OR:
  831. Begin
  832. Consume(AS_OR);
  833. expr:=expr + '|';
  834. end;
  835. AS_INTNUM:
  836. Begin
  837. expr:=expr + actasmpattern;
  838. Consume(AS_INTNUM);
  839. end;
  840. {$ifdef i8086}
  841. AS_SEG:
  842. begin
  843. isseg:=true;
  844. Consume(actasmtoken);
  845. if actasmtoken<>AS_ID then
  846. Message(asmr_e_seg_without_identifier);
  847. end;
  848. {$endif i8086}
  849. AS_VMTOFFSET,
  850. AS_OFFSET:
  851. begin
  852. if (actasmtoken = AS_OFFSET) then
  853. needofs:=true
  854. else
  855. needvmtofs:=true;
  856. Consume(actasmtoken);
  857. if actasmtoken<>AS_ID then
  858. Message(asmr_e_offset_without_identifier);
  859. end;
  860. AS_SIZEOF,
  861. AS_TYPE:
  862. begin
  863. l:=0;
  864. hasparen:=false;
  865. Consume(actasmtoken);
  866. if actasmtoken=AS_LPAREN then
  867. begin
  868. hasparen:=true;
  869. Consume(AS_LPAREN);
  870. end;
  871. if actasmtoken<>AS_ID then
  872. Message(asmr_e_type_without_identifier)
  873. else
  874. begin
  875. tempstr:=actasmpattern;
  876. Consume(AS_ID);
  877. if actasmtoken=AS_DOT then
  878. begin
  879. BuildRecordOffsetSize(tempstr,k,l,mangledname,false,hastypecast);
  880. if mangledname<>'' then
  881. { procsym }
  882. Message(asmr_e_wrong_sym_type);
  883. if hastypecast then
  884. end
  885. else
  886. begin
  887. asmsearchsym(tempstr,sym,srsymtable);
  888. if assigned(sym) then
  889. begin
  890. case sym.typ of
  891. staticvarsym,
  892. localvarsym,
  893. paravarsym :
  894. l:=tabstractvarsym(sym).getsize;
  895. typesym :
  896. l:=ttypesym(sym).typedef.size;
  897. else
  898. Message(asmr_e_wrong_sym_type);
  899. end;
  900. end
  901. else
  902. Message1(sym_e_unknown_id,tempstr);
  903. end;
  904. end;
  905. str(l, tempstr);
  906. expr:=expr + tempstr;
  907. if hasparen then
  908. Consume(AS_RPAREN);
  909. end;
  910. AS_PTR :
  911. begin
  912. { Support ugly delphi constructs like <constant> PTR [ref] }
  913. break;
  914. end;
  915. AS_STRING:
  916. begin
  917. l:=0;
  918. case Length(actasmpattern) of
  919. 1 :
  920. l:=ord(actasmpattern[1]);
  921. 2 :
  922. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  923. 3 :
  924. l:=ord(actasmpattern[3]) +
  925. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  926. 4 :
  927. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  928. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  929. else
  930. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  931. end;
  932. str(l, tempstr);
  933. expr:=expr + tempstr;
  934. Consume(AS_STRING);
  935. end;
  936. AS_ID:
  937. begin
  938. hs:='';
  939. hssymtyp:=AT_DATA;
  940. def:=nil;
  941. tempstr:=actasmpattern;
  942. prevtok:=prevasmtoken;
  943. { stop parsing a constant expression if we find an opcode after a
  944. non-operator like "db $66 mov eax,ebx" }
  945. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  946. is_asmopcode(actasmpattern) then
  947. break;
  948. consume(AS_ID);
  949. if (tempstr='@CODE') or (tempstr='@DATA') then
  950. begin
  951. if asmsym='' then
  952. begin
  953. asmsym:=tempstr;
  954. asmsymtyp:=AT_SECTION;
  955. end
  956. else
  957. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  958. end
  959. else if SearchIConstant(tempstr,l) then
  960. begin
  961. str(l, tempstr);
  962. expr:=expr + tempstr;
  963. end
  964. else
  965. begin
  966. if is_locallabel(tempstr) then
  967. begin
  968. CreateLocalLabel(tempstr,hl,false);
  969. hs:=hl.name;
  970. hssymtyp:=AT_FUNCTION;
  971. end
  972. else
  973. if SearchLabel(tempstr,hl,false) then
  974. begin
  975. hs:=hl.name;
  976. hssymtyp:=AT_FUNCTION;
  977. end
  978. else
  979. begin
  980. asmsearchsym(tempstr,sym,srsymtable);
  981. if assigned(sym) then
  982. begin
  983. case sym.typ of
  984. staticvarsym :
  985. begin
  986. hs:=tstaticvarsym(sym).mangledname;
  987. def:=tstaticvarsym(sym).vardef;
  988. end;
  989. localvarsym,
  990. paravarsym :
  991. begin
  992. Message(asmr_e_no_local_or_para_allowed);
  993. end;
  994. procsym :
  995. begin
  996. if Tprocsym(sym).ProcdefList.Count>1 then
  997. Message(asmr_w_calling_overload_func);
  998. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  999. {$ifdef i8086}
  1000. is_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  1001. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  1002. {$endif i8086}
  1003. hssymtyp:=AT_FUNCTION;
  1004. end;
  1005. typesym :
  1006. begin
  1007. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1008. Message(asmr_e_wrong_sym_type);
  1009. end;
  1010. fieldvarsym :
  1011. begin
  1012. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  1013. end;
  1014. else
  1015. Message(asmr_e_wrong_sym_type);
  1016. end;
  1017. end
  1018. else
  1019. Message1(sym_e_unknown_id,tempstr);
  1020. end;
  1021. { symbol found? }
  1022. if hs<>'' then
  1023. begin
  1024. if asmsym='' then
  1025. begin
  1026. asmsym:=hs;
  1027. asmsymtyp:=hssymtyp;
  1028. end
  1029. else
  1030. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1031. if (expr='') or (expr[length(expr)]='+') then
  1032. begin
  1033. { don't remove the + if there could be a record field }
  1034. if actasmtoken<>AS_DOT then
  1035. delete(expr,length(expr),1);
  1036. end
  1037. else
  1038. if needofs then
  1039. begin
  1040. if (prevtok<>AS_OFFSET) then
  1041. Message(asmr_e_need_offset);
  1042. end
  1043. else
  1044. Message(asmr_e_only_add_relocatable_symbol);
  1045. end;
  1046. if (actasmtoken=AS_DOT) or
  1047. (assigned(sym) and
  1048. (sym.typ = fieldvarsym) and
  1049. not(sp_static in sym.symoptions)) then
  1050. begin
  1051. BuildRecordOffsetSize(tempstr,l,k,hs,needvmtofs,hastypecast);
  1052. if hs <> '' then
  1053. hssymtyp:=AT_FUNCTION
  1054. else
  1055. begin
  1056. str(l, tempstr);
  1057. expr:=expr + tempstr;
  1058. end
  1059. end
  1060. else
  1061. begin
  1062. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1063. delete(expr,length(expr),1);
  1064. end;
  1065. if (actasmtoken=AS_LBRACKET) and
  1066. assigned(def) and
  1067. (def.typ=arraydef) then
  1068. begin
  1069. consume(AS_LBRACKET);
  1070. l:=BuildConstExpression;
  1071. if l<tarraydef(def).lowrange then
  1072. begin
  1073. Message(asmr_e_constant_out_of_bounds);
  1074. l:=0;
  1075. end
  1076. else
  1077. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1078. str(l, tempstr);
  1079. expr:=expr + '+' + tempstr;
  1080. consume(AS_RBRACKET);
  1081. end;
  1082. end;
  1083. { check if there are wrong operator used like / or mod etc. }
  1084. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1085. Message(asmr_e_only_add_relocatable_symbol);
  1086. end;
  1087. AS_ALIGN,
  1088. AS_DB,
  1089. AS_DW,
  1090. AS_DD,
  1091. AS_DQ,
  1092. AS_END,
  1093. AS_RBRACKET,
  1094. AS_SEPARATOR,
  1095. AS_COMMA,
  1096. AS_COLON:
  1097. break;
  1098. else
  1099. begin
  1100. { write error only once. }
  1101. if not errorflag then
  1102. Message(asmr_e_invalid_constant_expression);
  1103. { consume tokens until we find COMMA or SEPARATOR }
  1104. Consume(actasmtoken);
  1105. errorflag:=TRUE;
  1106. end;
  1107. end;
  1108. Until false;
  1109. { calculate expression }
  1110. if not ErrorFlag then
  1111. value:=CalculateExpression(expr)
  1112. else
  1113. value:=0;
  1114. { no longer in an expression }
  1115. inexpression:=FALSE;
  1116. end;
  1117. Function tx86intreader.BuildConstExpression:aint;
  1118. var
  1119. l : tcgint;
  1120. hs : string;
  1121. hssymtyp : TAsmsymtype;
  1122. isseg : boolean;
  1123. is_farproc_entry : boolean;
  1124. begin
  1125. BuildConstSymbolExpression(false,false,false,l,hs,hssymtyp,isseg,is_farproc_entry);
  1126. if hs<>'' then
  1127. Message(asmr_e_relocatable_symbol_not_allowed);
  1128. BuildConstExpression:=l;
  1129. end;
  1130. Function tx86intreader.BuildRefConstExpression(startingminus:boolean):aint;
  1131. var
  1132. l : tcgint;
  1133. hs : string;
  1134. hssymtyp : TAsmsymtype;
  1135. isseg : boolean;
  1136. is_farproc_entry : boolean;
  1137. begin
  1138. BuildConstSymbolExpression(false,true,startingminus,l,hs,hssymtyp,isseg,is_farproc_entry);
  1139. if hs<>'' then
  1140. Message(asmr_e_relocatable_symbol_not_allowed);
  1141. BuildRefConstExpression:=l;
  1142. end;
  1143. procedure tx86intreader.BuildReference(oper : tx86operand);
  1144. var
  1145. scale : byte;
  1146. k,l : tcgint;
  1147. tempstr,hs : string;
  1148. tempsymtyp : tasmsymtype;
  1149. code : integer;
  1150. hreg : tregister;
  1151. GotStar,GotOffset,HadVar,
  1152. GotPlus,Negative : boolean;
  1153. hl : tasmlabel;
  1154. isseg: boolean;
  1155. is_farproc_entry,
  1156. hastypecast: boolean;
  1157. Begin
  1158. Consume(AS_LBRACKET);
  1159. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1160. oper.InitRef;
  1161. GotStar:=false;
  1162. GotPlus:=true;
  1163. GotOffset:=false;
  1164. Negative:=false;
  1165. Scale:=0;
  1166. repeat
  1167. if GotOffset and (actasmtoken<>AS_ID) then
  1168. Message(asmr_e_invalid_reference_syntax);
  1169. Case actasmtoken of
  1170. AS_ID, { Constant reference expression OR variable reference expression }
  1171. AS_VMTOFFSET:
  1172. Begin
  1173. if not GotPlus then
  1174. Message(asmr_e_invalid_reference_syntax);
  1175. GotStar:=false;
  1176. GotPlus:=false;
  1177. if (actasmtoken = AS_VMTOFFSET) or
  1178. (SearchIConstant(actasmpattern,l) or
  1179. SearchRecordType(actasmpattern)) then
  1180. begin
  1181. l:=BuildRefConstExpression(negative);
  1182. negative:=false; { "l" was negated if necessary }
  1183. GotPlus:=(prevasmtoken=AS_PLUS);
  1184. GotStar:=(prevasmtoken=AS_STAR);
  1185. case oper.opr.typ of
  1186. OPR_LOCAL :
  1187. begin
  1188. if GotStar then
  1189. Message(asmr_e_invalid_reference_syntax);
  1190. Inc(oper.opr.localsymofs,l);
  1191. end;
  1192. OPR_REFERENCE :
  1193. begin
  1194. if GotStar then
  1195. oper.opr.ref.scalefactor:=l
  1196. else
  1197. Inc(oper.opr.ref.offset,l);
  1198. end;
  1199. end;
  1200. end
  1201. else
  1202. Begin
  1203. if negative and not oper.hasvar then
  1204. Message(asmr_e_only_add_relocatable_symbol)
  1205. else if oper.hasvar and not GotOffset and
  1206. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1207. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1208. HadVar:=oper.hasvar and GotOffset;
  1209. tempstr:=actasmpattern;
  1210. Consume(AS_ID);
  1211. { typecasting? }
  1212. if (actasmtoken=AS_LPAREN) and
  1213. SearchType(tempstr,l) then
  1214. begin
  1215. oper.hastype:=true;
  1216. oper.typesize:=l;
  1217. Consume(AS_LPAREN);
  1218. BuildOperand(oper,true);
  1219. Consume(AS_RPAREN);
  1220. end
  1221. else
  1222. if is_locallabel(tempstr) then
  1223. begin
  1224. CreateLocalLabel(tempstr,hl,false);
  1225. oper.InitRef;
  1226. oper.haslabelref:=true;
  1227. if not negative then
  1228. begin
  1229. oper.opr.ref.symbol:=hl;
  1230. oper.hasvar:=true;
  1231. end
  1232. else
  1233. oper.opr.ref.relsymbol:=hl;
  1234. {$ifdef i8086}
  1235. if oper.opr.ref.segment=NR_NO then
  1236. oper.opr.ref.segment:=NR_CS;
  1237. {$endif i8086}
  1238. end
  1239. else
  1240. if oper.SetupVar(tempstr,GotOffset) then
  1241. begin
  1242. { convert OPR_LOCAL register para into a reference base }
  1243. if (oper.opr.typ=OPR_LOCAL) and
  1244. AsmRegisterPara(oper.opr.localsym) then
  1245. oper.InitRefConvertLocal
  1246. else
  1247. begin
  1248. {$ifdef x86_64}
  1249. if actasmtoken=AS_WRT then
  1250. begin
  1251. if (oper.opr.typ=OPR_REFERENCE) then
  1252. begin
  1253. Consume(AS_WRT);
  1254. Consume(AS___GOTPCREL);
  1255. if (oper.opr.ref.base<>NR_NO) or
  1256. (oper.opr.ref.index<>NR_NO) or
  1257. (oper.opr.ref.offset<>0) then
  1258. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1259. if tf_no_pic_supported in target_info.flags then
  1260. Message(asmr_e_no_gotpcrel_support);
  1261. oper.opr.ref.refaddr:=addr_pic;
  1262. oper.opr.ref.base:=NR_RIP;
  1263. end
  1264. else
  1265. message(asmr_e_invalid_reference_syntax);
  1266. end;
  1267. {$endif x86_64}
  1268. end;
  1269. end
  1270. else
  1271. Message1(sym_e_unknown_id,tempstr);
  1272. { record.field ? }
  1273. if actasmtoken=AS_DOT then
  1274. begin
  1275. BuildRecordOffsetSize(tempstr,l,k,hs,false,hastypecast);
  1276. if (hs<>'') then
  1277. Message(asmr_e_invalid_symbol_ref);
  1278. case oper.opr.typ of
  1279. OPR_LOCAL :
  1280. inc(oper.opr.localsymofs,l);
  1281. OPR_REFERENCE :
  1282. inc(oper.opr.ref.offset,l);
  1283. end;
  1284. if hastypecast then
  1285. oper.hastype:=true;
  1286. oper.SetSize(k,false);
  1287. end;
  1288. if GotOffset then
  1289. begin
  1290. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1291. begin
  1292. if (oper.opr.typ=OPR_REFERENCE) then
  1293. oper.opr.ref.base:=NR_NO;
  1294. oper.hasvar:=hadvar;
  1295. end
  1296. else
  1297. begin
  1298. if oper.hasvar and hadvar then
  1299. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1300. { should we allow ?? }
  1301. end;
  1302. end;
  1303. end;
  1304. GotOffset:=false;
  1305. end;
  1306. AS_PLUS :
  1307. Begin
  1308. Consume(AS_PLUS);
  1309. Negative:=false;
  1310. GotPlus:=true;
  1311. GotStar:=false;
  1312. Scale:=0;
  1313. end;
  1314. AS_DOT :
  1315. Begin
  1316. { Handle like a + }
  1317. Consume(AS_DOT);
  1318. Negative:=false;
  1319. GotPlus:=true;
  1320. GotStar:=false;
  1321. Scale:=0;
  1322. end;
  1323. AS_MINUS :
  1324. begin
  1325. Consume(AS_MINUS);
  1326. Negative:=true;
  1327. GotPlus:=true;
  1328. GotStar:=false;
  1329. Scale:=0;
  1330. end;
  1331. AS_STAR : { Scaling, with eax*4 order }
  1332. begin
  1333. Consume(AS_STAR);
  1334. hs:='';
  1335. l:=0;
  1336. case actasmtoken of
  1337. AS_ID,
  1338. AS_LPAREN :
  1339. l:=BuildConstExpression;
  1340. AS_INTNUM:
  1341. Begin
  1342. hs:=actasmpattern;
  1343. Consume(AS_INTNUM);
  1344. end;
  1345. AS_REGISTER :
  1346. begin
  1347. case oper.opr.typ of
  1348. OPR_REFERENCE :
  1349. begin
  1350. if oper.opr.ref.scalefactor=0 then
  1351. begin
  1352. if scale<>0 then
  1353. begin
  1354. oper.opr.ref.scalefactor:=scale;
  1355. scale:=0;
  1356. end
  1357. else
  1358. Message(asmr_e_wrong_scale_factor);
  1359. end
  1360. else
  1361. Message(asmr_e_invalid_reference_syntax);
  1362. end;
  1363. OPR_LOCAL :
  1364. begin
  1365. if oper.opr.localscale=0 then
  1366. begin
  1367. if scale<>0 then
  1368. begin
  1369. oper.opr.localscale:=scale;
  1370. scale:=0;
  1371. end
  1372. else
  1373. Message(asmr_e_wrong_scale_factor);
  1374. end
  1375. else
  1376. Message(asmr_e_invalid_reference_syntax);
  1377. end;
  1378. end;
  1379. end;
  1380. else
  1381. Message(asmr_e_invalid_reference_syntax);
  1382. end;
  1383. if actasmtoken<>AS_REGISTER then
  1384. begin
  1385. if hs<>'' then
  1386. val(hs,l,code);
  1387. case oper.opr.typ of
  1388. OPR_REFERENCE :
  1389. oper.opr.ref.scalefactor:=l;
  1390. OPR_LOCAL :
  1391. oper.opr.localscale:=l;
  1392. end;
  1393. if l>9 then
  1394. Message(asmr_e_wrong_scale_factor);
  1395. end;
  1396. GotPlus:=false;
  1397. GotStar:=false;
  1398. end;
  1399. AS_REGISTER :
  1400. begin
  1401. if not((GotPlus and (not Negative)) or
  1402. GotStar) then
  1403. Message(asmr_e_invalid_reference_syntax);
  1404. hreg:=actasmregister;
  1405. Consume(AS_REGISTER);
  1406. { this register will be the index:
  1407. 1. just read a *
  1408. 2. next token is a *
  1409. 3. base register is already used }
  1410. case oper.opr.typ of
  1411. OPR_LOCAL :
  1412. begin
  1413. if (oper.opr.localindexreg<>NR_NO) then
  1414. Message(asmr_e_multiple_index);
  1415. {$ifdef x86_64}
  1416. { Locals/parameters cannot be accessed RIP-relative. Need a dedicated error message here? }
  1417. if (hreg=NR_RIP) then
  1418. Message(asmr_e_no_local_or_para_allowed);
  1419. {$endif x86_64}
  1420. oper.opr.localindexreg:=hreg;
  1421. if scale<>0 then
  1422. begin
  1423. oper.opr.localscale:=scale;
  1424. scale:=0;
  1425. end;
  1426. end;
  1427. OPR_REFERENCE :
  1428. begin
  1429. if (GotStar) or
  1430. (actasmtoken=AS_STAR) or
  1431. (oper.opr.ref.base<>NR_NO) then
  1432. begin
  1433. if (oper.opr.ref.index<>NR_NO) then
  1434. Message(asmr_e_multiple_index);
  1435. oper.opr.ref.index:=hreg;
  1436. if scale<>0 then
  1437. begin
  1438. oper.opr.ref.scalefactor:=scale;
  1439. scale:=0;
  1440. end;
  1441. end
  1442. else
  1443. begin
  1444. oper.opr.ref.base:=hreg;
  1445. {$ifdef x86_64}
  1446. { non-GOT based RIP-relative accesses are also position-independent }
  1447. if (oper.opr.ref.base=NR_RIP) and
  1448. (oper.opr.ref.refaddr<>addr_pic) then
  1449. oper.opr.ref.refaddr:=addr_pic_no_got;
  1450. {$endif x86_64}
  1451. end;
  1452. end;
  1453. end;
  1454. GotPlus:=false;
  1455. GotStar:=false;
  1456. end;
  1457. AS_OFFSET :
  1458. begin
  1459. Consume(AS_OFFSET);
  1460. GotOffset:=true;
  1461. end;
  1462. AS_TYPE,
  1463. AS_NOT,
  1464. AS_STRING,
  1465. AS_INTNUM,
  1466. AS_LPAREN : { Constant reference expression }
  1467. begin
  1468. if not GotPlus and not GotStar then
  1469. Message(asmr_e_invalid_reference_syntax);
  1470. BuildConstSymbolExpression(true,true,GotPlus and negative,l,tempstr,tempsymtyp,isseg,is_farproc_entry);
  1471. { already handled by BuildConstSymbolExpression(); must be
  1472. handled there to avoid [reg-1+1] being interpreted as
  1473. [reg-(1+1)] }
  1474. negative:=false;
  1475. if tempstr<>'' then
  1476. begin
  1477. if GotStar then
  1478. Message(asmr_e_only_add_relocatable_symbol);
  1479. if not assigned(oper.opr.ref.symbol) then
  1480. begin
  1481. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1482. {$ifdef i8086}
  1483. if isseg then
  1484. begin
  1485. if not (oper.opr.ref.refaddr in [addr_fardataseg,addr_dgroup]) then
  1486. oper.opr.ref.refaddr:=addr_seg;
  1487. end
  1488. else if (tempsymtyp=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  1489. oper.opr.ref.segment:=NR_CS;
  1490. {$endif i8086}
  1491. end
  1492. else
  1493. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1494. end;
  1495. case oper.opr.typ of
  1496. OPR_REFERENCE :
  1497. begin
  1498. if GotStar then
  1499. oper.opr.ref.scalefactor:=l
  1500. else if (prevasmtoken = AS_STAR) then
  1501. begin
  1502. if scale<>0 then
  1503. scale:=l*scale
  1504. else
  1505. scale:=l;
  1506. end
  1507. else
  1508. begin
  1509. Inc(oper.opr.ref.offset,l);
  1510. Inc(oper.opr.constoffset,l);
  1511. end;
  1512. end;
  1513. OPR_LOCAL :
  1514. begin
  1515. if GotStar then
  1516. oper.opr.localscale:=l
  1517. else if (prevasmtoken = AS_STAR) then
  1518. begin
  1519. if scale<>0 then
  1520. scale:=l*scale
  1521. else
  1522. scale:=l;
  1523. end
  1524. else
  1525. Inc(oper.opr.localsymofs,l);
  1526. end;
  1527. end;
  1528. GotPlus:=(prevasmtoken=AS_PLUS) or
  1529. (prevasmtoken=AS_MINUS);
  1530. if GotPlus then
  1531. negative := prevasmtoken = AS_MINUS;
  1532. GotStar:=(prevasmtoken=AS_STAR);
  1533. end;
  1534. AS_RBRACKET :
  1535. begin
  1536. if GotPlus or GotStar then
  1537. Message(asmr_e_invalid_reference_syntax);
  1538. Consume(AS_RBRACKET);
  1539. break;
  1540. end;
  1541. else
  1542. Begin
  1543. Message(asmr_e_invalid_reference_syntax);
  1544. RecoverConsume(true);
  1545. break;
  1546. end;
  1547. end;
  1548. until false;
  1549. end;
  1550. Procedure tx86intreader.BuildConstantOperand(oper: tx86operand);
  1551. var
  1552. l : tcgint;
  1553. tempstr : string;
  1554. tempsymtyp : tasmsymtype;
  1555. isseg: boolean;
  1556. is_farproc_entry : boolean;
  1557. begin
  1558. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1559. Message(asmr_e_invalid_operand_type);
  1560. BuildConstSymbolExpression(true,false,false,l,tempstr,tempsymtyp,isseg,is_farproc_entry);
  1561. {$ifdef i8086}
  1562. if tempstr='@DATA' then
  1563. begin
  1564. if not isseg then
  1565. Message(asmr_e_CODE_or_DATA_without_SEG);
  1566. oper.SetupData;
  1567. end
  1568. else if tempstr='@CODE' then
  1569. begin
  1570. if not isseg then
  1571. Message(asmr_e_CODE_or_DATA_without_SEG);
  1572. oper.SetupCode;
  1573. end
  1574. else
  1575. {$endif i8086}
  1576. if tempstr<>'' then
  1577. begin
  1578. oper.opr.typ:=OPR_SYMBOL;
  1579. oper.opr.symofs:=l;
  1580. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1581. oper.opr.symseg:=isseg;
  1582. oper.opr.sym_farproc_entry:=is_farproc_entry;
  1583. end
  1584. else
  1585. if oper.opr.typ=OPR_NONE then
  1586. begin
  1587. oper.opr.typ:=OPR_CONSTANT;
  1588. oper.opr.val:=l;
  1589. end
  1590. else
  1591. inc(oper.opr.val,l);
  1592. end;
  1593. Procedure tx86intreader.BuildOperand(oper: tx86operand;istypecast:boolean);
  1594. procedure AddLabelOperand(hl:tasmlabel);
  1595. begin
  1596. if (oper.opr.typ=OPR_NONE) and
  1597. is_calljmp(actopcode) then
  1598. begin
  1599. oper.opr.typ:=OPR_SYMBOL;
  1600. oper.opr.symbol:=hl;
  1601. end
  1602. else
  1603. begin
  1604. oper.InitRef;
  1605. oper.opr.ref.symbol:=hl;
  1606. oper.haslabelref:=true;
  1607. {$ifdef i8086}
  1608. if oper.opr.ref.segment=NR_NO then
  1609. oper.opr.ref.segment:=NR_CS;
  1610. {$endif i8086}
  1611. end;
  1612. end;
  1613. var
  1614. expr,
  1615. hs : string;
  1616. tempreg : tregister;
  1617. l : tcgint;
  1618. hl : tasmlabel;
  1619. toffset,
  1620. tsize : tcgint;
  1621. hastypecast: boolean;
  1622. begin
  1623. expr:='';
  1624. repeat
  1625. if actasmtoken=AS_DOT then
  1626. begin
  1627. if expr<>'' then
  1628. begin
  1629. BuildRecordOffsetSize(expr,toffset,tsize,hs,false,hastypecast);
  1630. if (oper.opr.typ<>OPR_NONE) and
  1631. (hs<>'') then
  1632. Message(asmr_e_wrong_sym_type);
  1633. oper.SetSize(tsize,true);
  1634. if hastypecast then
  1635. oper.hastype:=true;
  1636. { we have used the size of a field. Reset the typesize of the record }
  1637. oper.typesize:=0;
  1638. case oper.opr.typ of
  1639. OPR_LOCAL :
  1640. begin
  1641. { don't allow direct access to fields of parameters, because that
  1642. will generate buggy code. Allow it only for explicit typecasting
  1643. and when the parameter is in a register (delphi compatible) }
  1644. if (not oper.hastype) and
  1645. (oper.opr.localsym.typ=paravarsym) and
  1646. (not(po_assembler in current_procinfo.procdef.procoptions) or
  1647. (tparavarsym(oper.opr.localsym).paraloc[calleeside].location^.loc<>LOC_REGISTER) or
  1648. (not is_implicit_pointer_object_type(oper.opr.localsym.vardef) and
  1649. not paramanager.push_addr_param(oper.opr.localsym.varspez,oper.opr.localsym.vardef,current_procinfo.procdef.proccalloption))) then
  1650. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1651. oper.opr.localforceref:=true;
  1652. inc(oper.opr.localsymofs,toffset);
  1653. oper.opr.localvarsize := tsize;
  1654. end;
  1655. OPR_CONSTANT :
  1656. inc(oper.opr.val,toffset);
  1657. OPR_REFERENCE :
  1658. begin
  1659. inc(oper.opr.ref.offset,toffset);
  1660. oper.opr.varsize := tsize;
  1661. end;
  1662. OPR_NONE :
  1663. begin
  1664. if (hs <> '') then
  1665. begin
  1666. oper.opr.typ:=OPR_SYMBOL;
  1667. oper.opr.symbol:=current_asmdata.RefAsmSymbol(hs,AT_FUNCTION);
  1668. end
  1669. else
  1670. begin
  1671. oper.opr.typ:=OPR_CONSTANT;
  1672. oper.opr.val:=toffset;
  1673. end;
  1674. end;
  1675. OPR_REGISTER :
  1676. Message(asmr_e_invalid_reference_syntax);
  1677. OPR_SYMBOL:
  1678. Message(asmr_e_invalid_symbol_ref);
  1679. else
  1680. internalerror(200309222);
  1681. end;
  1682. expr:='';
  1683. end
  1684. else
  1685. begin
  1686. { See it as a separator }
  1687. Consume(AS_DOT);
  1688. end;
  1689. end;
  1690. case actasmtoken of
  1691. {$ifndef i8086}
  1692. AS_SEG :
  1693. Begin
  1694. Message(asmr_e_seg_not_supported);
  1695. Consume(actasmtoken);
  1696. end;
  1697. {$else not i8086}
  1698. AS_SEG,
  1699. {$endif not i8086}
  1700. AS_OFFSET,
  1701. AS_SIZEOF,
  1702. AS_VMTOFFSET,
  1703. AS_TYPE,
  1704. AS_NOT,
  1705. AS_STRING,
  1706. AS_PLUS,
  1707. AS_MINUS,
  1708. AS_LPAREN,
  1709. AS_INTNUM :
  1710. begin
  1711. case oper.opr.typ of
  1712. OPR_REFERENCE :
  1713. {$ifndef x86_64}
  1714. { this is for the i386 scenario where you have
  1715. <load got into ebx>
  1716. mov eax, [ebx].offset globalvar
  1717. x86-64 uses RIP-based addresses (both for GOT and non-GOT
  1718. relative accesses)
  1719. }
  1720. if (actasmtoken=AS_OFFSET) and
  1721. (cs_create_pic in current_settings.moduleswitches) then
  1722. begin
  1723. Consume(AS_OFFSET);
  1724. oper.opr.ref.refaddr:=addr_pic;
  1725. BuildOperand(oper,false);
  1726. end
  1727. else
  1728. {$endif x86_64}
  1729. begin
  1730. l := BuildRefConstExpression;
  1731. inc(oper.opr.ref.offset,l);
  1732. inc(oper.opr.constoffset,l);
  1733. end;
  1734. OPR_LOCAL :
  1735. begin
  1736. l := BuildConstExpression;
  1737. inc(oper.opr.localsymofs,l);
  1738. inc(oper.opr.localconstoffset,l);
  1739. end;
  1740. OPR_NONE,
  1741. OPR_CONSTANT :
  1742. BuildConstantOperand(oper);
  1743. else
  1744. Message(asmr_e_invalid_operand_type);
  1745. end;
  1746. end;
  1747. AS_PTR :
  1748. begin
  1749. if not oper.hastype then
  1750. begin
  1751. if (oper.opr.typ=OPR_CONSTANT) then
  1752. begin
  1753. oper.typesize:=oper.opr.val;
  1754. { reset constant value of operand }
  1755. oper.opr.typ:=OPR_NONE;
  1756. oper.opr.val:=0;
  1757. end
  1758. else
  1759. Message(asmr_e_syn_operand);
  1760. end;
  1761. Consume(AS_PTR);
  1762. { in delphi mode, allow e.g. call dword ptr eax,
  1763. see also webtbs/tw18225.pp }
  1764. if not(m_delphi in current_settings.modeswitches) then
  1765. oper.InitRef;
  1766. { if the operand subscripts a record, the typesize will be
  1767. rest -> save it here and restore it afterwards }
  1768. l:=oper.typesize;
  1769. BuildOperand(oper,false);
  1770. oper.setsize(l,true);
  1771. end;
  1772. AS_ID : { A constant expression, or a Variable ref. }
  1773. Begin
  1774. { Label or Special symbol reference? }
  1775. if actasmpattern[1] = '@' then
  1776. Begin
  1777. if actasmpattern = '@RESULT' then
  1778. Begin
  1779. oper.SetupResult;
  1780. Consume(AS_ID);
  1781. expr:='result';
  1782. end
  1783. else
  1784. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1785. begin
  1786. {$ifdef i8086}
  1787. Message(asmr_e_CODE_or_DATA_without_SEG);
  1788. {$else i8086}
  1789. Message(asmr_w_CODE_and_DATA_not_supported);
  1790. {$endif i8086}
  1791. Consume(AS_ID);
  1792. end
  1793. else
  1794. { Local Label }
  1795. begin
  1796. CreateLocalLabel(actasmpattern,hl,false);
  1797. Consume(AS_ID);
  1798. AddLabelOperand(hl);
  1799. end;
  1800. end
  1801. else
  1802. { support result for delphi modes }
  1803. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  1804. begin
  1805. oper.SetUpResult;
  1806. Consume(AS_ID);
  1807. expr:='result';
  1808. end
  1809. { probably a variable or normal expression }
  1810. { or a procedure (such as in CALL ID) }
  1811. else
  1812. Begin
  1813. { is it a constant ? }
  1814. if SearchIConstant(actasmpattern,l) then
  1815. Begin
  1816. case oper.opr.typ of
  1817. OPR_REFERENCE :
  1818. begin
  1819. l := BuildRefConstExpression;
  1820. inc(oper.opr.ref.offset,l);
  1821. inc(oper.opr.constoffset,l);
  1822. end;
  1823. OPR_LOCAL :
  1824. begin
  1825. l := BuildRefConstExpression;
  1826. inc(oper.opr.localsymofs,l);
  1827. inc(oper.opr.localconstoffset,l);
  1828. end;
  1829. OPR_NONE,
  1830. OPR_CONSTANT :
  1831. BuildConstantOperand(oper);
  1832. else
  1833. Message(asmr_e_invalid_operand_type);
  1834. end;
  1835. end
  1836. else
  1837. { Check for pascal label }
  1838. if SearchLabel(actasmpattern,hl,false) then
  1839. begin
  1840. Consume(AS_ID);
  1841. AddLabelOperand(hl);
  1842. end
  1843. else
  1844. { is it a normal variable ? }
  1845. Begin
  1846. expr:=actasmpattern;
  1847. Consume(AS_ID);
  1848. { typecasting? }
  1849. if SearchType(expr,l) then
  1850. begin
  1851. oper.hastype:=true;
  1852. oper.typesize:=l;
  1853. case actasmtoken of
  1854. AS_LPAREN :
  1855. begin
  1856. { Support Type([Reference]) }
  1857. Consume(AS_LPAREN);
  1858. BuildOperand(oper,true);
  1859. { Delphi also supports Type(Register) and
  1860. interprets it the same as Type([Register]). }
  1861. if (oper.opr.typ = OPR_REGISTER) then
  1862. { This also sets base to the register. }
  1863. oper.InitRef;
  1864. Consume(AS_RPAREN);
  1865. end;
  1866. AS_LBRACKET :
  1867. begin
  1868. { Support Var.Type[Index] }
  1869. { Convert @label.Byte[1] to reference }
  1870. if oper.opr.typ=OPR_SYMBOL then
  1871. oper.initref;
  1872. end;
  1873. end;
  1874. end
  1875. else
  1876. begin
  1877. if not oper.SetupVar(expr,false) then
  1878. Begin
  1879. { not a variable, check special variables.. }
  1880. if expr = 'SELF' then
  1881. begin
  1882. oper.SetupSelf;
  1883. expr:='self';
  1884. end
  1885. else
  1886. begin
  1887. Message1(sym_e_unknown_id,expr);
  1888. expr:='';
  1889. end;
  1890. end;
  1891. { indexed access to variable? }
  1892. if actasmtoken=AS_LBRACKET then
  1893. begin
  1894. { ... then the operand size is not known anymore }
  1895. oper.size:=OS_NO;
  1896. BuildReference(oper);
  1897. end;
  1898. end;
  1899. end;
  1900. end;
  1901. end;
  1902. AS_REGISTER : { Register, a variable reference or a constant reference }
  1903. begin
  1904. { save the type of register used. }
  1905. tempreg:=actasmregister;
  1906. Consume(AS_REGISTER);
  1907. if actasmtoken = AS_COLON then
  1908. Begin
  1909. Consume(AS_COLON);
  1910. oper.InitRef;
  1911. oper.opr.ref.segment:=tempreg;
  1912. BuildReference(oper);
  1913. end
  1914. else
  1915. { Simple register }
  1916. begin
  1917. if (oper.opr.typ <> OPR_NONE) then
  1918. Message(asmr_e_syn_operand);
  1919. oper.opr.typ:=OPR_REGISTER;
  1920. oper.opr.reg:=tempreg;
  1921. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  1922. end;
  1923. end;
  1924. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1925. Begin
  1926. BuildReference(oper);
  1927. end;
  1928. AS_DWORD,
  1929. AS_BYTE,
  1930. AS_WORD,
  1931. AS_TBYTE,
  1932. AS_DQWORD,
  1933. AS_QWORD,
  1934. AS_OWORD,
  1935. AS_XMMWORD,
  1936. AS_YWORD,
  1937. AS_YMMWORD:
  1938. begin
  1939. { Type specifier }
  1940. oper.hastype:=true;
  1941. oper.typesize:=0;
  1942. case actasmtoken of
  1943. AS_DWORD : oper.typesize:=4;
  1944. AS_WORD : oper.typesize:=2;
  1945. AS_BYTE : oper.typesize:=1;
  1946. AS_QWORD : oper.typesize:=8;
  1947. AS_DQWORD : oper.typesize:=16;
  1948. AS_TBYTE : oper.typesize:=10;
  1949. AS_OWORD,
  1950. AS_XMMWORD: oper.typesize:=16;
  1951. AS_YWORD,
  1952. AS_YMMWORD: oper.typesize:=32;
  1953. else
  1954. internalerror(2010061101);
  1955. end;
  1956. Consume(actasmtoken);
  1957. if (actasmtoken=AS_LPAREN) then
  1958. begin
  1959. { Support "xxx ptr [Reference]" }
  1960. { in case the expression subscripts a record, the typesize
  1961. is reset, so save the explicit size we set above }
  1962. l:=oper.typesize;
  1963. Consume(AS_LPAREN);
  1964. BuildOperand(oper,true);
  1965. Consume(AS_RPAREN);
  1966. oper.setsize(l,true);
  1967. end;
  1968. end;
  1969. AS_SEPARATOR,
  1970. AS_END,
  1971. AS_COMMA,
  1972. AS_COLON:
  1973. begin
  1974. break;
  1975. end;
  1976. AS_RPAREN:
  1977. begin
  1978. if not istypecast then
  1979. begin
  1980. Message(asmr_e_syn_operand);
  1981. Consume(AS_RPAREN);
  1982. end
  1983. else
  1984. break;
  1985. end;
  1986. else
  1987. begin
  1988. Message(asmr_e_syn_operand);
  1989. RecoverConsume(true);
  1990. break;
  1991. end;
  1992. end;
  1993. until false;
  1994. { End of operand, update size if a typecast is forced }
  1995. if (oper.typesize<>0) and
  1996. (oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL]) then
  1997. oper.SetSize(oper.typesize,true);
  1998. {$ifdef i8086}
  1999. { references to a procedure/function entry, without an explicit segment
  2000. override, are added an CS: override by default (this is Turbo Pascal 7
  2001. compatible) }
  2002. if (oper.opr.typ=OPR_REFERENCE) and assigned(oper.opr.ref.symbol) and
  2003. (oper.opr.ref.symbol.typ=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  2004. oper.opr.ref.segment:=NR_CS;
  2005. {$endif i8086}
  2006. end;
  2007. Procedure tx86intreader.BuildOpCode(instr : tx86instruction);
  2008. var
  2009. PrefixOp,OverrideOp: tasmop;
  2010. operandnum : longint;
  2011. t: TRegister;
  2012. is_far_const:boolean;
  2013. i:byte;
  2014. tmp: toperand;
  2015. di_param, si_param: ShortInt;
  2016. {$ifdef i8086}
  2017. hsymbol: TAsmSymbol;
  2018. hoffset: ASizeInt;
  2019. href_farproc_entry: Boolean;
  2020. {$endif i8086}
  2021. begin
  2022. PrefixOp:=A_None;
  2023. OverrideOp:=A_None;
  2024. is_far_const:=false;
  2025. { prefix seg opcode / prefix opcode }
  2026. repeat
  2027. if is_prefix(actopcode) then
  2028. with instr do
  2029. begin
  2030. PrefixOp:=ActOpcode;
  2031. opcode:=ActOpcode;
  2032. condition:=ActCondition;
  2033. opsize:=ActOpsize;
  2034. ConcatInstruction(curlist);
  2035. consume(AS_OPCODE);
  2036. end
  2037. else
  2038. if is_override(actopcode) then
  2039. with instr do
  2040. begin
  2041. OverrideOp:=ActOpcode;
  2042. opcode:=ActOpcode;
  2043. condition:=ActCondition;
  2044. opsize:=ActOpsize;
  2045. ConcatInstruction(curlist);
  2046. consume(AS_OPCODE);
  2047. end
  2048. else
  2049. break;
  2050. { allow for newline after prefix or override }
  2051. while actasmtoken=AS_SEPARATOR do
  2052. consume(AS_SEPARATOR);
  2053. until (actasmtoken<>AS_OPCODE);
  2054. { opcode }
  2055. if (actasmtoken <> AS_OPCODE) then
  2056. begin
  2057. Message(asmr_e_invalid_or_missing_opcode);
  2058. RecoverConsume(false);
  2059. exit;
  2060. end;
  2061. { Fill the instr object with the current state }
  2062. with instr do
  2063. begin
  2064. Opcode:=ActOpcode;
  2065. condition:=ActCondition;
  2066. opsize:=ActOpsize;
  2067. { Valid combination of prefix/override and instruction ? }
  2068. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  2069. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  2070. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  2071. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  2072. end;
  2073. { pushf/popf/pusha/popa have to default to 16 bit in Intel mode
  2074. (Intel manual and Delphi-compatbile) -- setting the opsize for
  2075. these instructions doesn't change anything in the internal assember,
  2076. so change the opcode }
  2077. if (instr.opcode=A_POPF) then
  2078. instr.opcode:=A_POPFW
  2079. else if (instr.opcode=A_PUSHF) then
  2080. instr.opcode:=A_PUSHFW
  2081. {$ifndef x86_64}
  2082. else if (instr.opcode=A_PUSHA) then
  2083. instr.opcode:=A_PUSHAW
  2084. else if (instr.opcode=A_POPA) then
  2085. instr.opcode:=A_POPAW
  2086. {$endif x86_64}
  2087. {$ifdef i8086}
  2088. { ret is converted to retn or retf, depending on the call model of the
  2089. current procedure (BP7 compatible) }
  2090. else if (instr.opcode=A_RET) then
  2091. begin
  2092. if is_proc_far(current_procinfo.procdef) and
  2093. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2094. instr.opcode:=A_RETF
  2095. else
  2096. instr.opcode:=A_RETN;
  2097. end
  2098. {$endif i8086}
  2099. ;
  2100. { We are reading operands, so opcode will be an AS_ID }
  2101. { process operands backwards to get them in AT&T order }
  2102. operandnum:=max_operands;
  2103. is_far_const:=false;
  2104. Consume(AS_OPCODE);
  2105. { Zero operand opcode ? }
  2106. if actasmtoken in [AS_SEPARATOR,AS_END] then
  2107. exit;
  2108. { Read Operands }
  2109. repeat
  2110. case actasmtoken of
  2111. { End of asm operands for this opcode }
  2112. AS_END,
  2113. AS_SEPARATOR :
  2114. break;
  2115. { Operand delimiter }
  2116. AS_COMMA :
  2117. begin
  2118. { should have something before the comma }
  2119. if instr.operands[operandnum].opr.typ=OPR_NONE then
  2120. Message(asmr_e_syntax_error);
  2121. if operandnum <= 1 then
  2122. Message(asmr_e_too_many_operands)
  2123. else
  2124. Dec(operandnum);
  2125. Consume(AS_COMMA);
  2126. end;
  2127. {Far constant, i.e. jmp $0000:$11111111.}
  2128. AS_COLON:
  2129. begin
  2130. is_far_const:=true;
  2131. if operandnum<max_operands then
  2132. message(asmr_e_too_many_operands)
  2133. else
  2134. dec(operandnum);
  2135. consume(AS_COLON);
  2136. end;
  2137. { Type specifier }
  2138. AS_NEAR,
  2139. AS_FAR :
  2140. begin
  2141. if actasmtoken = AS_NEAR then
  2142. begin
  2143. {$ifndef i8086}
  2144. Message(asmr_w_near_ignored);
  2145. {$endif not i8086}
  2146. instr.opsize:=S_NEAR;
  2147. end
  2148. else
  2149. begin
  2150. {$ifndef i8086}
  2151. Message(asmr_w_far_ignored);
  2152. {$endif not i8086}
  2153. instr.opsize:=S_FAR;
  2154. end;
  2155. Consume(actasmtoken);
  2156. if actasmtoken=AS_PTR then
  2157. begin
  2158. Consume(AS_PTR);
  2159. instr.Operands[operandnum].InitRef;
  2160. end;
  2161. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2162. end;
  2163. else
  2164. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2165. end; { end case }
  2166. until false;
  2167. { shift operands to start from 1, exchange to make sure they are destroyed correctly }
  2168. for i:=operandnum to max_operands do
  2169. begin
  2170. tmp:=instr.operands[i+1-operandnum];
  2171. instr.operands[i+1-operandnum]:=instr.operands[i];
  2172. instr.operands[i]:=tmp;
  2173. end;
  2174. operandnum:=(max_operands+1)-operandnum;
  2175. instr.ops:=operandnum;
  2176. { Check operands }
  2177. for i:=1 to operandnum do
  2178. begin
  2179. if is_far_const and
  2180. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  2181. message(asmr_e_expr_illegal)
  2182. else
  2183. if instr.operands[i].opr.typ=OPR_NONE then
  2184. Message(asmr_e_syntax_error);
  2185. end;
  2186. { Check for invalid ES: overrides }
  2187. if is_x86_parameterized_string_op(instr.opcode) then
  2188. begin
  2189. si_param:=get_x86_string_op_si_param(instr.opcode);
  2190. if si_param<>-1 then
  2191. si_param:=x86_parameterized_string_op_param_count(instr.opcode)-si_param;
  2192. di_param:=get_x86_string_op_di_param(instr.opcode);
  2193. if di_param<>-1 then
  2194. begin
  2195. di_param:=x86_parameterized_string_op_param_count(instr.opcode)-di_param;
  2196. if di_param<=operandnum then
  2197. with instr.operands[di_param] do
  2198. if (opr.typ=OPR_REFERENCE) and
  2199. (opr.ref.segment<>NR_NO) and
  2200. (opr.ref.segment<>NR_ES) then
  2201. Message(asmr_e_cannot_override_es_segment);
  2202. end;
  2203. { if two memory parameters, check whether their address sizes are equal }
  2204. if (si_param<>-1) and (di_param<>-1) and
  2205. (si_param<=operandnum) and (di_param<=operandnum) and
  2206. (instr.operands[si_param].opr.typ=OPR_REFERENCE) and
  2207. (instr.operands[di_param].opr.typ=OPR_REFERENCE) then
  2208. begin
  2209. if get_ref_address_size(instr.operands[si_param].opr.ref)<>
  2210. get_ref_address_size(instr.operands[di_param].opr.ref) then
  2211. Message(asmr_e_address_sizes_do_not_match);
  2212. end;
  2213. end;
  2214. { e.g. for "push dword 1", "push word 6" }
  2215. if (instr.ops=1) and
  2216. (instr.operands[1].typesize<>0) then
  2217. instr.operands[1].setsize(instr.operands[1].typesize,false);
  2218. {$ifdef i8086}
  2219. for i:=1 to operandnum do
  2220. with instr.operands[i].opr do
  2221. begin
  2222. { convert 'call/jmp [proc/label]' to 'call/jmp proc/label'. Ugly,
  2223. but Turbo Pascal 7 compatible. }
  2224. if (instr.opcode in [A_CALL,A_JMP]) and
  2225. (instr.operands[i].haslabelref or instr.operands[i].hasproc)
  2226. and (typ=OPR_REFERENCE) and
  2227. assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL,AT_ADDR]) and
  2228. (ref.base=NR_NO) and (ref.index=NR_NO) then
  2229. begin
  2230. hsymbol:=ref.symbol;
  2231. hoffset:=ref.offset;
  2232. href_farproc_entry:=ref_farproc_entry;
  2233. typ:=OPR_SYMBOL;
  2234. symbol:=hsymbol;
  2235. symofs:=hoffset;
  2236. symseg:=False;
  2237. sym_farproc_entry:=href_farproc_entry;
  2238. end;
  2239. { convert 'call/jmp symbol' to 'call/jmp far symbol' for symbols that are an entry point of a far procedure }
  2240. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2241. (typ=OPR_SYMBOL) and sym_farproc_entry then
  2242. instr.opsize:=S_FAR;
  2243. { convert 'call/jmp dword [something]' to 'call/jmp far [something]' (BP7 compatibility) }
  2244. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2245. (typ in [OPR_LOCAL,OPR_REFERENCE]) and (instr.operands[i].size=OS_32) then
  2246. instr.opsize:=S_FAR;
  2247. end;
  2248. {$endif i8086}
  2249. if (MemRefInfo(instr.opcode).ExistsSSEAVX) and
  2250. (MemRefInfo(instr.opcode).MemRefSize in MemRefSizeInfoVMems) then
  2251. begin
  2252. for i:=1 to operandnum do
  2253. begin
  2254. if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2255. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2256. (instr.operands[i].opr.ref.index = NR_NO) then
  2257. begin
  2258. instr.operands[i].opr.ref.index := instr.operands[i].opr.ref.base;
  2259. instr.operands[i].opr.ref.base := NR_NO;
  2260. end
  2261. else if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2262. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2263. (getregtype(instr.operands[i].opr.ref.index) = R_INTREGISTER) and
  2264. (getsubreg(instr.operands[i].opr.ref.index) = R_SUBADDR) then
  2265. begin
  2266. // exchange base- and index-register
  2267. // e.g. VGATHERDPD XMM0, [XMM1 + RAX], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1], XMM2
  2268. // e.g. VGATHERDPD XMM0, [XMM1 + RAX * 2], XMM2 =>> not supported
  2269. // e.g. VGATHERDPD XMM0, [XMM1 + RAX + 16], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1 + 16]
  2270. if instr.operands[i].opr.ref.scalefactor > 1 then Message(asmr_e_invalid_reference_syntax)
  2271. else
  2272. begin
  2273. t := instr.operands[i].opr.ref.base;
  2274. instr.operands[i].opr.ref.base := instr.operands[i].opr.ref.index;
  2275. instr.operands[i].opr.ref.index := t;
  2276. end;
  2277. end;
  2278. end;
  2279. end;
  2280. end;
  2281. Procedure tx86intreader.BuildConstant(constsize: byte);
  2282. var
  2283. asmsymtyp : tasmsymtype;
  2284. asmsym,
  2285. expr: string;
  2286. value : tcgint;
  2287. isseg: boolean;
  2288. is_farproc_entry : boolean;
  2289. Begin
  2290. Repeat
  2291. Case actasmtoken of
  2292. AS_STRING:
  2293. Begin
  2294. { DD and DW cases }
  2295. if constsize <> 1 then
  2296. Begin
  2297. if Not PadZero(actasmpattern,constsize) then
  2298. Message(scan_f_string_exceeds_line);
  2299. end;
  2300. expr:=actasmpattern;
  2301. Consume(AS_STRING);
  2302. Case actasmtoken of
  2303. AS_COMMA:
  2304. Consume(AS_COMMA);
  2305. AS_END,
  2306. AS_SEPARATOR: ;
  2307. else
  2308. Message(asmr_e_invalid_string_expression);
  2309. end;
  2310. ConcatString(curlist,expr);
  2311. end;
  2312. AS_PLUS,
  2313. AS_MINUS,
  2314. AS_LPAREN,
  2315. AS_NOT,
  2316. AS_INTNUM,
  2317. {$ifdef i8086}
  2318. AS_SEG,
  2319. {$endif i8086}
  2320. AS_ID :
  2321. Begin
  2322. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp,isseg,is_farproc_entry);
  2323. if asmsym<>'' then
  2324. begin
  2325. if (not isseg) and (constsize<>sizeof(pint)) then
  2326. Message1(asmr_w_const32bit_for_address,asmsym);
  2327. {$ifdef i8086}
  2328. if asmsym='@DATA' then
  2329. begin
  2330. if not isseg then
  2331. Message(asmr_e_CODE_or_DATA_without_SEG);
  2332. if constsize<2 then
  2333. Message1(asmr_e_const16bit_for_segment,asmsym);
  2334. if current_settings.x86memorymodel=mm_huge then
  2335. curlist.concat(Tai_const.Create_fardataseg)
  2336. else
  2337. curlist.concat(Tai_const.Create_dgroup);
  2338. if constsize>2 then
  2339. ConcatConstant(curlist,0,constsize-2);
  2340. end
  2341. else if asmsym='@CODE' then
  2342. begin
  2343. if not isseg then
  2344. Message(asmr_e_CODE_or_DATA_without_SEG);
  2345. if constsize<2 then
  2346. Message1(asmr_e_const16bit_for_segment,asmsym);
  2347. curlist.concat(Tai_const.Create_seg_name(current_procinfo.procdef.mangledname));
  2348. if constsize>2 then
  2349. ConcatConstant(curlist,0,constsize-2);
  2350. end
  2351. else if isseg then
  2352. begin
  2353. if constsize<2 then
  2354. Message1(asmr_e_const16bit_for_segment,asmsym);
  2355. curlist.concat(Tai_const.Create_seg_name(asmsym));
  2356. if constsize>2 then
  2357. ConcatConstant(curlist,0,constsize-2);
  2358. end
  2359. else
  2360. {$endif i8086}
  2361. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value);
  2362. end
  2363. else
  2364. ConcatConstant(curlist,value,constsize);
  2365. end;
  2366. AS_COMMA:
  2367. begin
  2368. Consume(AS_COMMA);
  2369. end;
  2370. AS_ALIGN,
  2371. AS_DB,
  2372. AS_DW,
  2373. AS_DD,
  2374. AS_DQ,
  2375. AS_OPCODE,
  2376. AS_END,
  2377. AS_SEPARATOR:
  2378. break;
  2379. else
  2380. begin
  2381. Message(asmr_e_syn_constant);
  2382. RecoverConsume(false);
  2383. end
  2384. end;
  2385. Until false;
  2386. end;
  2387. function tx86intreader.Assemble: tlinkedlist;
  2388. Var
  2389. hl : tasmlabel;
  2390. instr : Tx86Instruction;
  2391. tmpsym: tsym;
  2392. tmpsrsymtable: TSymtable;
  2393. Begin
  2394. Message1(asmr_d_start_reading,'intel');
  2395. inexpression:=FALSE;
  2396. firsttoken:=TRUE;
  2397. { sets up all opcode and register tables in uppercase
  2398. done in the construtor now
  2399. if not _asmsorted then
  2400. Begin
  2401. SetupTables;
  2402. _asmsorted:=TRUE;
  2403. end;
  2404. }
  2405. curlist:=TAsmList.Create;
  2406. { we might need to know which parameters are passed in registers }
  2407. if not parse_generic then
  2408. current_procinfo.generate_parameter_info;
  2409. { start tokenizer }
  2410. gettoken;
  2411. { main loop }
  2412. repeat
  2413. case actasmtoken of
  2414. AS_LLABEL:
  2415. Begin
  2416. if CreateLocalLabel(actasmpattern,hl,true) then
  2417. ConcatLabel(curlist,hl);
  2418. Consume(AS_LLABEL);
  2419. end;
  2420. AS_LABEL:
  2421. Begin
  2422. if SearchLabel(upper(actasmpattern),hl,true) then
  2423. begin
  2424. if hl.is_public then
  2425. ConcatPublic(curlist,actasmpattern_origcase);
  2426. ConcatLabel(curlist,hl);
  2427. end
  2428. else
  2429. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2430. Consume(AS_LABEL);
  2431. end;
  2432. AS_DW :
  2433. Begin
  2434. inexpression:=true;
  2435. Consume(AS_DW);
  2436. BuildConstant(2);
  2437. inexpression:=false;
  2438. end;
  2439. AS_DB :
  2440. Begin
  2441. inexpression:=true;
  2442. Consume(AS_DB);
  2443. BuildConstant(1);
  2444. inexpression:=false;
  2445. end;
  2446. AS_DD :
  2447. Begin
  2448. inexpression:=true;
  2449. Consume(AS_DD);
  2450. BuildConstant(4);
  2451. inexpression:=false;
  2452. end;
  2453. {$ifdef cpu64bitaddr}
  2454. AS_DQ:
  2455. Begin
  2456. inexpression:=true;
  2457. Consume(AS_DQ);
  2458. BuildConstant(8);
  2459. inexpression:=false;
  2460. end;
  2461. {$endif cpu64bitaddr}
  2462. AS_PUBLIC:
  2463. Begin
  2464. Consume(AS_PUBLIC);
  2465. repeat
  2466. if actasmtoken=AS_ID then
  2467. begin
  2468. if (actasmpattern<>'') and (actasmpattern[1]='@') then
  2469. Message1(asmr_e_local_label_cannot_be_declared_public,actasmpattern)
  2470. else if SearchLabel(upper(actasmpattern),hl,false) then
  2471. begin
  2472. if not hl.is_public then
  2473. begin
  2474. hl.is_public:=true;
  2475. asmsearchsym(upper(actasmpattern),tmpsym,tmpsrsymtable);
  2476. if tlabelsym(tmpsym).defined then
  2477. Message1(asmr_e_public_must_be_used_before_label_definition,actasmpattern);
  2478. end;
  2479. end
  2480. else
  2481. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2482. end;
  2483. Consume(AS_ID);
  2484. if actasmtoken=AS_COMMA then
  2485. begin
  2486. Consume(AS_COMMA);
  2487. if actasmtoken<>AS_ID then
  2488. Consume(AS_ID);
  2489. end;
  2490. until actasmtoken=AS_SEPARATOR;
  2491. end;
  2492. AS_ALIGN:
  2493. Begin
  2494. Consume(AS_ALIGN);
  2495. ConcatAlign(curlist,BuildConstExpression);
  2496. if actasmtoken<>AS_SEPARATOR then
  2497. Consume(AS_SEPARATOR);
  2498. end;
  2499. AS_OPCODE :
  2500. Begin
  2501. instr:=Tx86Instruction.Create(Tx86Operand);
  2502. BuildOpcode(instr);
  2503. with instr do
  2504. begin
  2505. CheckNonCommutativeOpcodes;
  2506. AddReferenceSizes;
  2507. SetInstructionOpsize;
  2508. CheckOperandSizes;
  2509. ConcatInstruction(curlist);
  2510. end;
  2511. instr.Free;
  2512. end;
  2513. AS_SEPARATOR :
  2514. Begin
  2515. Consume(AS_SEPARATOR);
  2516. end;
  2517. AS_END :
  2518. break; { end assembly block }
  2519. else
  2520. Begin
  2521. Message(asmr_e_syntax_error);
  2522. RecoverConsume(false);
  2523. end;
  2524. end; { end case }
  2525. until false;
  2526. { check that all referenced local labels are defined }
  2527. checklocallabels;
  2528. { Return the list in an asmnode }
  2529. assemble:=curlist;
  2530. Message1(asmr_d_finish_reading,'intel');
  2531. end;
  2532. end.