rax86int.pas 95 KB

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