rautils.pas 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. This unit implements some support routines for assembler parsing
  4. independent of the processor
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. **********************************************************************}
  17. Unit RAUtils;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Uses
  21. cutils,cclasses,
  22. globtype,aasmbase,aasmtai,aasmdata,cpubase,cpuinfo,cgbase,cgutils,
  23. symconst,symbase,symtype,symdef,symsym,symcpu;
  24. Const
  25. RPNMax = 10; { I think you only need 4, but just to be safe }
  26. OpMax = 25;
  27. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  28. {---------------------------------------------------------------------
  29. Instruction management
  30. ---------------------------------------------------------------------}
  31. type
  32. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  33. OPR_REFERENCE,OPR_REGISTER,OPR_COND,OPR_REGSET,
  34. OPR_SHIFTEROP,OPR_MODEFLAGS,OPR_SPECIALREG,
  35. OPR_REGPAIR);
  36. TOprRec = record
  37. case typ:TOprType of
  38. OPR_NONE : ();
  39. {$if defined(AVR)}
  40. OPR_CONSTANT : (val:word);
  41. {$elseif defined(i8086)}
  42. OPR_CONSTANT : (val:longint);
  43. {$else}
  44. OPR_CONSTANT : (val:aint);
  45. {$endif}
  46. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint;symseg:boolean;sym_farproc_entry:boolean);
  47. OPR_REFERENCE : (varsize:asizeint; constoffset: asizeint;ref_farproc_entry:boolean;ref:treference);
  48. OPR_LOCAL : (localvarsize, localconstoffset: asizeint;localsym:tabstractnormalvarsym;localsymofs:aint;localindexreg:tregister;localscale:byte;localgetoffset,localforceref:boolean);
  49. OPR_REGISTER : (reg:tregister);
  50. {$ifdef m68k}
  51. OPR_REGSET : (regsetdata,regsetaddr,regsetfpu : tcpuregisterset);
  52. OPR_REGPAIR : (reghi,reglo: tregister);
  53. {$endif m68k}
  54. {$ifdef powerpc}
  55. OPR_COND : (cond : tasmcond);
  56. {$endif powerpc}
  57. {$ifdef POWERPC64}
  58. OPR_COND : (cond : tasmcond);
  59. {$endif POWERPC64}
  60. {$ifdef arm}
  61. OPR_REGSET : (regset : tcpuregisterset; regtype: tregistertype; subreg: tsubregister; usermode: boolean);
  62. OPR_SHIFTEROP : (shifterop : tshifterop);
  63. OPR_COND : (cc : tasmcond);
  64. OPR_MODEFLAGS : (flags : tcpumodeflags);
  65. OPR_SPECIALREG: (specialreg : tregister; specialregflags : tspecialregflags);
  66. {$endif arm}
  67. {$ifdef aarch64}
  68. OPR_SHIFTEROP : (shifterop : tshifterop);
  69. OPR_COND : (cc : tasmcond);
  70. {$endif aarch64}
  71. end;
  72. TOperand = class
  73. opr : TOprRec;
  74. typesize : byte;
  75. haslabelref, { if the operand has a label, used in a reference like a
  76. var (e.g. 'mov ax, word ptr [label+5]', but *not*
  77. e.g. 'jmp label') }
  78. hasproc, { if the operand has a procedure/function reference }
  79. hastype, { if the operand has typecasted variable }
  80. hasvar : boolean; { if the operand is loaded with a variable }
  81. size : TCGSize;
  82. constructor create;virtual;
  83. destructor destroy;override;
  84. Procedure SetSize(_size:longint;force:boolean);virtual;
  85. Procedure SetCorrectSize(opcode:tasmop);virtual;
  86. Function SetupResult:boolean;virtual;
  87. Function SetupSelf:boolean;
  88. Function SetupOldEBP:boolean;
  89. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  90. Function CheckOperand: boolean; virtual;
  91. Procedure InitRef;
  92. Procedure InitRefConvertLocal;
  93. protected
  94. Procedure InitRefError;
  95. end;
  96. TCOperand = class of TOperand;
  97. TInstruction = class
  98. operands : array[1..max_operands] of toperand;
  99. opcode : tasmop;
  100. condition : tasmcond;
  101. ops : byte;
  102. labeled : boolean;
  103. filepos : tfileposinfo;
  104. constructor create(optype : tcoperand);virtual;
  105. destructor destroy;override;
  106. { converts the instruction to an instruction how it's used by the assembler writer
  107. and concats it to the passed list. The newly created item is returned if the
  108. instruction was valid, otherwise nil is returned }
  109. function ConcatInstruction(p:TAsmList) : tai;virtual;
  110. end;
  111. {---------------------------------------------------------------------}
  112. { Expression parser types }
  113. {---------------------------------------------------------------------}
  114. TExprOperator = record
  115. ch: char; { operator }
  116. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  117. end;
  118. {**********************************************************************}
  119. { The following operators are supported: }
  120. { '+' : addition }
  121. { '-' : subtraction }
  122. { '*' : multiplication }
  123. { '/' : modulo division }
  124. { '^' : exclusive or }
  125. { '<' : shift left }
  126. { '>' : shift right }
  127. { '&' : bitwise and }
  128. { '|' : bitwise or }
  129. { '~' : bitwise complement }
  130. { '%' : modulo division }
  131. { nnn: longint numbers }
  132. { ( and ) parenthesis }
  133. {**********************************************************************}
  134. TExprParse = class
  135. public
  136. Constructor create;
  137. Destructor Destroy;override;
  138. Function Evaluate(Expr: String): tcgint;
  139. Function Priority(_Operator: Char): aint;
  140. private
  141. RPNStack : Array[1..RPNMax] of tcgint; { Stack For RPN calculator }
  142. RPNTop : tcgint;
  143. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  144. OpTop : tcgint;
  145. Procedure RPNPush(Num: tcgint);
  146. Function RPNPop: tcgint;
  147. Procedure RPNCalc(const token: String; prefix: boolean);
  148. Procedure OpPush(_Operator: char; prefix: boolean);
  149. { In reality returns TExprOperaotr }
  150. Procedure OpPop(var _Operator:TExprOperator);
  151. end;
  152. { Evaluate an expression string to a tcgint }
  153. Function CalculateExpression(const expression: string): tcgint;
  154. {---------------------------------------------------------------------}
  155. { String routines }
  156. {---------------------------------------------------------------------}
  157. Function ParseVal(const S:String;base:byte):tcgint;
  158. Function PadZero(Var s: String; n: byte): Boolean;
  159. Function EscapeToPascal(const s:string): string;
  160. {---------------------------------------------------------------------
  161. Symbol helper routines
  162. ---------------------------------------------------------------------}
  163. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:TSymtable);
  164. Function GetRecordOffsetSize(s:string;Var Offset: tcgint;var Size:tcgint; var mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  165. Function SearchType(const hs:string;var size:tcgint): Boolean;
  166. Function SearchRecordType(const s:string): boolean;
  167. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  168. Function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  169. {---------------------------------------------------------------------
  170. Instruction generation routines
  171. ---------------------------------------------------------------------}
  172. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  173. Procedure ConcatConstant(p : TAsmList;value: tcgint; constsize:byte);
  174. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint);
  175. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  176. Procedure ConcatString(p : TAsmList;s:string);
  177. procedure ConcatAlign(p:TAsmList;l:tcgint);
  178. Procedure ConcatPublic(p:TAsmList;const s : string);
  179. Procedure ConcatLocal(p:TAsmList;const s : string);
  180. Implementation
  181. uses
  182. SysUtils,
  183. defutil,systems,verbose,globals,
  184. symtable,paramgr,
  185. aasmcpu,
  186. procinfo;
  187. {*************************************************************************
  188. TExprParse
  189. *************************************************************************}
  190. Constructor TExprParse.create;
  191. Begin
  192. end;
  193. Procedure TExprParse.RPNPush(Num : tcgint);
  194. { Add an operand to the top of the RPN stack }
  195. begin
  196. if RPNTop < RPNMax then
  197. begin
  198. Inc(RPNTop);
  199. RPNStack[RPNTop]:=Num;
  200. end
  201. else
  202. Message(asmr_e_expr_illegal);
  203. end;
  204. Function TExprParse.RPNPop : tcgint;
  205. { Get the operand at the top of the RPN stack }
  206. begin
  207. RPNPop:=0;
  208. if RPNTop > 0 then
  209. begin
  210. RPNPop:=RPNStack[RPNTop];
  211. Dec(RPNTop);
  212. end
  213. else
  214. Message(asmr_e_expr_illegal);
  215. end;
  216. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  217. Var
  218. Temp : tcgint;
  219. n1,n2 : tcgint;
  220. LocalError : Integer;
  221. begin
  222. { Handle operators }
  223. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  224. Case Token[1] of
  225. '+' :
  226. Begin
  227. if not prefix then
  228. RPNPush(RPNPop + RPNPop);
  229. end;
  230. '-' :
  231. Begin
  232. if prefix then
  233. RPNPush(-(RPNPop))
  234. else
  235. begin
  236. n1:=RPNPop;
  237. n2:=RPNPop;
  238. RPNPush(n2 - n1);
  239. end;
  240. end;
  241. '*' : RPNPush(RPNPop * RPNPop);
  242. '&' :
  243. begin
  244. n1:=RPNPop;
  245. n2:=RPNPop;
  246. RPNPush(n2 and n1);
  247. end;
  248. '|' :
  249. begin
  250. n1:=RPNPop;
  251. n2:=RPNPop;
  252. RPNPush(n2 or n1);
  253. end;
  254. '~' : RPNPush(NOT RPNPop);
  255. '<' :
  256. begin
  257. n1:=RPNPop;
  258. n2:=RPNPop;
  259. RPNPush(n2 SHL n1);
  260. end;
  261. '>' :
  262. begin
  263. n1:=RPNPop;
  264. n2:=RPNPop;
  265. RPNPush(n2 SHR n1);
  266. end;
  267. '%' :
  268. begin
  269. Temp:=RPNPop;
  270. if Temp <> 0 then
  271. RPNPush(RPNPop mod Temp)
  272. else
  273. begin
  274. Message(asmr_e_expr_zero_divide);
  275. { push 1 for error recovery }
  276. RPNPush(1);
  277. end;
  278. end;
  279. '^' : RPNPush(RPNPop XOR RPNPop);
  280. '/' :
  281. begin
  282. Temp:=RPNPop;
  283. if Temp <> 0 then
  284. RPNPush(RPNPop div Temp)
  285. else
  286. begin
  287. Message(asmr_e_expr_zero_divide);
  288. { push 1 for error recovery }
  289. RPNPush(1);
  290. end;
  291. end;
  292. end
  293. else
  294. begin
  295. { Convert String to number and add to stack }
  296. Val(Token, Temp, LocalError);
  297. if LocalError = 0 then
  298. RPNPush(Temp)
  299. else
  300. begin
  301. Message(asmr_e_expr_illegal);
  302. { push 1 for error recovery }
  303. RPNPush(1);
  304. end;
  305. end;
  306. end;
  307. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  308. { Add an operator onto top of the stack }
  309. begin
  310. if OpTop < OpMax then
  311. begin
  312. Inc(OpTop);
  313. OpStack[OpTop].ch:=_Operator;
  314. OpStack[OpTop].is_prefix:=prefix;
  315. end
  316. else
  317. Message(asmr_e_expr_illegal);
  318. end;
  319. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  320. { Get operator at the top of the stack }
  321. begin
  322. if OpTop > 0 then
  323. begin
  324. _Operator:=OpStack[OpTop];
  325. Dec(OpTop);
  326. end
  327. else
  328. Message(asmr_e_expr_illegal);
  329. end;
  330. Function TExprParse.Priority(_Operator : Char) : aint;
  331. { Return priority of operator }
  332. { The greater the priority, the higher the precedence }
  333. begin
  334. Priority:=0;
  335. Case _Operator OF
  336. '(' :
  337. Priority:=0;
  338. '|','^','~' : // the lowest priority: OR, XOR, NOT
  339. Priority:=0;
  340. '&' : // bigger priority: AND
  341. Priority:=1;
  342. '+', '-' : // bigger priority: +, -
  343. Priority:=2;
  344. '*', '/','%','<','>' : // the highest priority: *, /, MOD, SHL, SHR
  345. Priority:=3;
  346. else
  347. Message(asmr_e_expr_illegal);
  348. end;
  349. end;
  350. Function TExprParse.Evaluate(Expr : String):tcgint;
  351. Var
  352. I : longint;
  353. Token : String;
  354. opr : TExprOperator;
  355. begin
  356. Evaluate:=0;
  357. { Reset stacks }
  358. OpTop :=0;
  359. RPNTop:=0;
  360. Token :='';
  361. { nothing to do ? }
  362. if Expr='' then
  363. exit;
  364. For I:=1 to Length(Expr) DO
  365. begin
  366. if Expr[I] in ['0'..'9'] then
  367. begin { Build multi-digit numbers }
  368. Token:=Token + Expr[I];
  369. if I = Length(Expr) then { Send last one to calculator }
  370. RPNCalc(Token,false);
  371. end
  372. else
  373. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  374. begin
  375. if Token <> '' then
  376. begin { Send last built number to calc. }
  377. RPNCalc(Token,false);
  378. Token:='';
  379. end;
  380. Case Expr[I] OF
  381. '(' : OpPush('(',false);
  382. ')' : begin
  383. While (OpTop>0) and (OpStack[OpTop].ch <> '(') DO
  384. Begin
  385. OpPop(opr);
  386. RPNCalc(opr.ch,opr.is_prefix);
  387. end;
  388. OpPop(opr); { Pop off and ignore the '(' }
  389. end;
  390. '+','-','~' : Begin
  391. { workaround for -2147483648 }
  392. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  393. begin
  394. token:='-';
  395. expr[i]:='+';
  396. end;
  397. { if start of expression then surely a prefix }
  398. { or if previous char was also an operator }
  399. if (I = 1) or (not (Expr[I-1] in ['0'..'9',')'])) then
  400. OpPush(Expr[I],true)
  401. else
  402. Begin
  403. { Evaluate all higher priority operators }
  404. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  405. Begin
  406. OpPop(opr);
  407. RPNCalc(opr.ch,opr.is_prefix);
  408. end;
  409. OpPush(Expr[I],false);
  410. End;
  411. end;
  412. '*', '/',
  413. '^','|','&',
  414. '%','<','>' : begin
  415. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  416. Begin
  417. OpPop(opr);
  418. RPNCalc(opr.ch,opr.is_prefix);
  419. end;
  420. OpPush(Expr[I],false);
  421. end;
  422. end; { Case }
  423. end
  424. else
  425. Message(asmr_e_expr_illegal); { Handle bad input error }
  426. end;
  427. { Pop off the remaining operators }
  428. While OpTop > 0 do
  429. Begin
  430. OpPop(opr);
  431. RPNCalc(opr.ch,opr.is_prefix);
  432. end;
  433. { The result is stored on the top of the stack }
  434. Evaluate:=RPNPop;
  435. end;
  436. Destructor TExprParse.Destroy;
  437. Begin
  438. end;
  439. Function CalculateExpression(const expression: string): tcgint;
  440. var
  441. expr: TExprParse;
  442. Begin
  443. expr:=TExprParse.create;
  444. CalculateExpression:=expr.Evaluate(expression);
  445. expr.Free;
  446. end;
  447. {*************************************************************************}
  448. { String conversions/utils }
  449. {*************************************************************************}
  450. Function EscapeToPascal(const s:string): string;
  451. { converts a C styled string - which contains escape }
  452. { characters to a pascal style string. }
  453. var
  454. i,len : asizeint;
  455. hs : string;
  456. temp : string;
  457. c : char;
  458. Begin
  459. hs:='';
  460. len:=0;
  461. i:=0;
  462. while (i<length(s)) and (len<255) do
  463. begin
  464. Inc(i);
  465. if (s[i]='\') and (i<length(s)) then
  466. Begin
  467. inc(i);
  468. case s[i] of
  469. '\' :
  470. c:='\';
  471. 'b':
  472. c:=#8;
  473. 'f':
  474. c:=#12;
  475. 'n':
  476. c:=#10;
  477. 'r':
  478. c:=#13;
  479. 't':
  480. c:=#9;
  481. '"':
  482. c:='"';
  483. '0'..'7':
  484. Begin
  485. temp:=s[i];
  486. temp:=temp+s[i+1];
  487. temp:=temp+s[i+2];
  488. inc(i,2);
  489. c:=chr(ParseVal(temp,8));
  490. end;
  491. 'x':
  492. Begin
  493. temp:=s[i+1];
  494. temp:=temp+s[i+2];
  495. inc(i,2);
  496. c:=chr(ParseVal(temp,16));
  497. end;
  498. else
  499. Begin
  500. Message1(asmr_e_escape_seq_ignored,s[i]);
  501. c:=s[i];
  502. end;
  503. end;
  504. end
  505. else
  506. c:=s[i];
  507. inc(len);
  508. hs[len]:=c;
  509. end;
  510. hs[0]:=chr(len);
  511. EscapeToPascal:=hs;
  512. end;
  513. Function ParseVal(const S:String;base:byte):tcgint;
  514. { Converts a decimal string to tcgint }
  515. var
  516. code : integer;
  517. errmsg : word;
  518. prefix : string[2];
  519. Begin
  520. case base of
  521. 2 :
  522. begin
  523. errmsg:=asmr_e_error_converting_binary;
  524. prefix:='%';
  525. end;
  526. 8 :
  527. begin
  528. errmsg:=asmr_e_error_converting_octal;
  529. prefix:='&';
  530. end;
  531. 10 :
  532. begin
  533. errmsg:=asmr_e_error_converting_decimal;
  534. prefix:='';
  535. end;
  536. 16 :
  537. begin
  538. errmsg:=asmr_e_error_converting_hexadecimal;
  539. prefix:='$';
  540. end;
  541. else
  542. internalerror(200501202);
  543. end;
  544. val(prefix+s,result,code);
  545. if code<>0 then
  546. begin
  547. val(prefix+s,result,code);
  548. if code<>0 then
  549. begin
  550. Message1(errmsg,s);
  551. result:=0;
  552. end;
  553. end;
  554. end;
  555. Function PadZero(Var s: String; n: byte): Boolean;
  556. Begin
  557. PadZero:=TRUE;
  558. { Do some error checking first }
  559. if Length(s) = n then
  560. exit
  561. else
  562. if Length(s) > n then
  563. Begin
  564. PadZero:=FALSE;
  565. delete(s,n+1,length(s));
  566. exit;
  567. end
  568. else
  569. PadZero:=TRUE;
  570. { Fill it up with the specified character }
  571. fillchar(s[length(s)+1],n-1,#0);
  572. s[0]:=chr(n);
  573. end;
  574. {****************************************************************************
  575. TOperand
  576. ****************************************************************************}
  577. constructor TOperand.Create;
  578. begin
  579. size:=OS_NO;
  580. hasproc:=false;
  581. hastype:=false;
  582. hasvar:=false;
  583. FillChar(Opr,sizeof(Opr),0);
  584. end;
  585. destructor TOperand.destroy;
  586. begin
  587. end;
  588. Procedure TOperand.SetSize(_size:longint;force:boolean);
  589. begin
  590. if force or
  591. ((size = OS_NO) and (_size<=16)) then
  592. Begin
  593. case _size of
  594. 1 : size:=OS_8;
  595. 2 : size:=OS_16{ could be S_IS};
  596. 4 : size:=OS_32{ could be S_IL or S_FS};
  597. 8 : size:=OS_64{ could be S_D or S_FL};
  598. 10 : size:=OS_F80;
  599. 16 : size:=OS_128;
  600. end;
  601. end;
  602. end;
  603. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  604. begin
  605. end;
  606. function TOperand.SetupResult:boolean;
  607. begin
  608. SetupResult:=false;
  609. { replace by correct offset. }
  610. with current_procinfo.procdef do
  611. if (not is_void(returndef)) then
  612. begin
  613. if (m_tp7 in current_settings.modeswitches) and
  614. not (df_generic in defoptions) and
  615. (not paramanager.ret_in_param(returndef,current_procinfo.procdef)) then
  616. begin
  617. message(asmr_e_cannot_use_RESULT_here);
  618. exit;
  619. end;
  620. SetupResult:=setupvar('result',false)
  621. end
  622. else
  623. message(asmr_e_void_function);
  624. end;
  625. Function TOperand.SetupSelf:boolean;
  626. Begin
  627. SetupSelf:=false;
  628. if assigned(current_structdef) then
  629. SetupSelf:=setupvar('self',false)
  630. else
  631. Message(asmr_e_cannot_use_SELF_outside_a_method);
  632. end;
  633. Function TOperand.SetupOldEBP:boolean;
  634. Begin
  635. SetupOldEBP:=false;
  636. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  637. SetupOldEBP:=setupvar('parentframe',false)
  638. else
  639. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  640. end;
  641. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  642. function symtable_has_localvarsyms(st:TSymtable):boolean;
  643. var
  644. sym : tsym;
  645. i : longint;
  646. begin
  647. result:=false;
  648. for i:=0 to st.SymList.Count-1 do
  649. begin
  650. sym:=tsym(st.SymList[i]);
  651. if sym.typ=localvarsym then
  652. begin
  653. result:=true;
  654. exit;
  655. end;
  656. end;
  657. end;
  658. procedure setconst(l:aint);
  659. begin
  660. { We return the address of the field, just like Delphi/TP }
  661. case opr.typ of
  662. OPR_NONE :
  663. begin
  664. opr.typ:=OPR_CONSTANT;
  665. opr.val:=l;
  666. end;
  667. OPR_CONSTANT :
  668. inc(opr.val,l);
  669. OPR_REFERENCE :
  670. inc(opr.ref.offset,l);
  671. OPR_LOCAL :
  672. inc(opr.localsymofs,l);
  673. else
  674. Message(asmr_e_invalid_operand_type);
  675. end;
  676. end;
  677. procedure setvarsize(sym: tabstractvarsym);
  678. var
  679. harrdef: tarraydef;
  680. l: asizeint;
  681. begin
  682. case sym.vardef.typ of
  683. orddef,
  684. enumdef,
  685. pointerdef,
  686. procvardef,
  687. floatdef :
  688. SetSize(sym.getsize,false);
  689. arraydef :
  690. begin
  691. { for arrays try to get the element size, take care of
  692. multiple indexes }
  693. harrdef:=tarraydef(sym.vardef);
  694. { calc array size }
  695. if is_special_array(harrdef) then
  696. l := -1
  697. else
  698. l := harrdef.size;
  699. case opr.typ of
  700. OPR_REFERENCE: opr.varsize := l;
  701. OPR_LOCAL: opr.localvarsize := l;
  702. end;
  703. while assigned(harrdef.elementdef) and
  704. (harrdef.elementdef.typ=arraydef) do
  705. harrdef:=tarraydef(harrdef.elementdef);
  706. if not is_packed_array(harrdef) then
  707. SetSize(harrdef.elesize,false)
  708. else
  709. if (harrdef.elepackedbitsize mod 8) = 0 then
  710. SetSize(harrdef.elepackedbitsize div 8,false);
  711. end;
  712. recorddef:
  713. case opr.typ of
  714. OPR_REFERENCE: opr.varsize := sym.getsize;
  715. OPR_LOCAL: opr.localvarsize := sym.getsize;
  716. end;
  717. end;
  718. end;
  719. { search and sets up the correct fields in the Instr record }
  720. { for the NON-constant identifier passed to the routine. }
  721. { if not found returns FALSE. }
  722. var
  723. sym : tsym;
  724. srsymtable : TSymtable;
  725. indexreg : tregister;
  726. plist : ppropaccesslistitem;
  727. size_set_from_absolute : boolean = false;
  728. Begin
  729. SetupVar:=false;
  730. asmsearchsym(s,sym,srsymtable);
  731. if sym = nil then
  732. exit;
  733. if sym.typ=absolutevarsym then
  734. begin
  735. case tabsolutevarsym(sym).abstyp of
  736. tovar:
  737. begin
  738. { Only support simple loads }
  739. plist:=tabsolutevarsym(sym).ref.firstsym;
  740. if assigned(plist) and
  741. (plist^.sltype=sl_load) then
  742. begin
  743. setvarsize(tabstractvarsym(sym));
  744. size_set_from_absolute:=true;
  745. sym:=plist^.sym;
  746. end
  747. else
  748. begin
  749. Message(asmr_e_unsupported_symbol_type);
  750. exit;
  751. end;
  752. end;
  753. toaddr:
  754. begin
  755. initref;
  756. opr.ref.offset:=tabsolutevarsym(sym).addroffset;
  757. setvarsize(tabstractvarsym(sym));
  758. size_set_from_absolute:=true;
  759. hasvar:=true;
  760. Result:=true;
  761. exit;
  762. end;
  763. else
  764. begin
  765. Message(asmr_e_unsupported_symbol_type);
  766. exit;
  767. end;
  768. end;
  769. end;
  770. case sym.typ of
  771. fieldvarsym :
  772. begin
  773. if not tabstractrecordsymtable(sym.owner).is_packed then
  774. setconst(tfieldvarsym(sym).fieldoffset)
  775. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  776. setconst(tfieldvarsym(sym).fieldoffset div 8)
  777. else
  778. Message(asmr_e_packed_element);
  779. if not size_set_from_absolute then
  780. setvarsize(tabstractvarsym(sym));
  781. hasvar:=true;
  782. SetupVar:=true;
  783. end;
  784. staticvarsym,
  785. localvarsym,
  786. paravarsym :
  787. begin
  788. { we always assume in asm statements that }
  789. { that the variable is valid. }
  790. tabstractvarsym(sym).varstate:=vs_readwritten;
  791. inc(tabstractvarsym(sym).refs);
  792. { variable can't be placed in a register }
  793. tabstractvarsym(sym).varregable:=vr_none;
  794. { and anything may happen with its address }
  795. tabstractvarsym(sym).addr_taken:=true;
  796. case sym.typ of
  797. staticvarsym :
  798. begin
  799. initref;
  800. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(sym).mangledname,AT_DATA);
  801. end;
  802. paravarsym,
  803. localvarsym :
  804. begin
  805. if opr.typ=OPR_REFERENCE then
  806. begin
  807. indexreg:=opr.ref.base;
  808. if opr.ref.index<>NR_NO then
  809. begin
  810. if indexreg=NR_NO then
  811. indexreg:=opr.ref.index
  812. else
  813. Message(asmr_e_multiple_index);
  814. end;
  815. end
  816. else
  817. indexreg:=NR_NO;
  818. opr.typ:=OPR_LOCAL;
  819. if assigned(current_procinfo.parent) and
  820. not(po_inline in current_procinfo.procdef.procoptions) and
  821. (sym.owner<>current_procinfo.procdef.localst) and
  822. (sym.owner<>current_procinfo.procdef.parast) and
  823. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  824. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  825. message1(asmr_e_local_para_unreachable,s);
  826. opr.localsym:=tabstractnormalvarsym(sym);
  827. opr.localsymofs:=0;
  828. opr.localindexreg:=indexreg;
  829. opr.localscale:=0;
  830. opr.localgetoffset:=GetOffset;
  831. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vardef,current_procinfo.procdef.proccalloption) then
  832. SetSize(sizeof(pint),false);
  833. end;
  834. end;
  835. if not size_set_from_absolute then
  836. setvarsize(tabstractvarsym(sym));
  837. hasvar:=true;
  838. SetupVar:=true;
  839. Exit;
  840. end;
  841. constsym :
  842. begin
  843. if tconstsym(sym).consttyp=constord then
  844. begin
  845. setconst(tconstsym(sym).value.valueord.svalue);
  846. SetupVar:=true;
  847. Exit;
  848. end;
  849. end;
  850. typesym :
  851. begin
  852. if ttypesym(sym).typedef.typ in [recorddef,objectdef] then
  853. begin
  854. setconst(0);
  855. SetupVar:=TRUE;
  856. Exit;
  857. end;
  858. end;
  859. procsym :
  860. begin
  861. if Tprocsym(sym).ProcdefList.Count>1 then
  862. Message(asmr_w_calling_overload_func);
  863. case opr.typ of
  864. OPR_REFERENCE:
  865. begin
  866. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  867. {$ifdef i8086}
  868. opr.ref_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  869. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  870. {$endif i8086}
  871. end;
  872. OPR_NONE:
  873. begin
  874. opr.typ:=OPR_SYMBOL;
  875. opr.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  876. {$ifdef i8086}
  877. opr.sym_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  878. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  879. {$endif i8086}
  880. opr.symofs:=0;
  881. end;
  882. else
  883. Message(asmr_e_invalid_operand_type);
  884. end;
  885. hasproc:=true;
  886. hasvar:=true;
  887. SetupVar:=TRUE;
  888. Exit;
  889. end;
  890. {$ifdef i8086}
  891. labelsym :
  892. begin
  893. case opr.typ of
  894. OPR_REFERENCE:
  895. begin
  896. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tlabelsym(sym).mangledname,AT_FUNCTION);
  897. if opr.ref.segment=NR_NO then
  898. opr.ref.segment:=NR_CS;
  899. end;
  900. else
  901. begin
  902. Message(asmr_e_unsupported_symbol_type);
  903. exit;
  904. end;
  905. end;
  906. haslabelref:=true;
  907. hasvar:=true;
  908. SetupVar:=TRUE;
  909. Exit;
  910. end
  911. {$endif i8086}
  912. else
  913. begin
  914. Message(asmr_e_unsupported_symbol_type);
  915. exit;
  916. end;
  917. end;
  918. end;
  919. procedure TOperand.InitRef;
  920. {*********************************************************************}
  921. { Description: This routine first check if the opcode is of }
  922. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  923. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  924. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  925. { to point to the default segment. }
  926. {*********************************************************************}
  927. var
  928. l : aint;
  929. hsymofs : aint;
  930. hsymbol : tasmsymbol;
  931. reg : tregister;
  932. hsym_farprocentry: Boolean;
  933. Begin
  934. case opr.typ of
  935. OPR_REFERENCE :
  936. exit;
  937. OPR_CONSTANT :
  938. begin
  939. l:=opr.val;
  940. opr.typ:=OPR_REFERENCE;
  941. Fillchar(opr.ref,sizeof(treference),0);
  942. opr.Ref.Offset:=l;
  943. opr.varsize:=0;
  944. opr.constoffset:=0;
  945. opr.ref_farproc_entry:=false;
  946. end;
  947. OPR_NONE :
  948. begin
  949. opr.typ:=OPR_REFERENCE;
  950. opr.varsize:=0;
  951. opr.constoffset:=0;
  952. opr.ref_farproc_entry:=false;
  953. Fillchar(opr.ref,sizeof(treference),0);
  954. end;
  955. OPR_REGISTER :
  956. begin
  957. reg:=opr.reg;
  958. opr.typ:=OPR_REFERENCE;
  959. opr.varsize:=0;
  960. opr.constoffset:=0;
  961. opr.ref_farproc_entry:=false;
  962. Fillchar(opr.ref,sizeof(treference),0);
  963. opr.Ref.base:=reg;
  964. end;
  965. OPR_SYMBOL :
  966. begin
  967. hsymbol:=opr.symbol;
  968. hsymofs:=opr.symofs;
  969. hsym_farprocentry:=opr.sym_farproc_entry;
  970. opr.typ:=OPR_REFERENCE;
  971. opr.varsize:=0;
  972. opr.constoffset:=0;
  973. Fillchar(opr.ref,sizeof(treference),0);
  974. opr.ref.symbol:=hsymbol;
  975. opr.ref.offset:=hsymofs;
  976. opr.ref_farproc_entry:=hsym_farprocentry;
  977. end;
  978. else
  979. InitRefError;
  980. end;
  981. end;
  982. procedure TOperand.InitRefConvertLocal;
  983. var
  984. localvarsize,localconstoffset: asizeint;
  985. localsym:tabstractnormalvarsym;
  986. localsymofs:aint;
  987. localindexreg:tregister;
  988. localscale:byte;
  989. begin
  990. if opr.typ=OPR_LOCAL then
  991. begin
  992. if AsmRegisterPara(opr.localsym) and
  993. not opr.localgetoffset then
  994. begin
  995. localvarsize:=opr.localvarsize;
  996. localconstoffset:=opr.localconstoffset;
  997. localsym:=opr.localsym;
  998. localsymofs:=opr.localsymofs;
  999. localindexreg:=opr.localindexreg;
  1000. localscale:=opr.localscale;;
  1001. opr.typ:=OPR_REFERENCE;
  1002. hasvar:=false;
  1003. Fillchar(opr.ref,sizeof(treference),0);
  1004. opr.varsize:=localvarsize;
  1005. opr.constoffset:=localconstoffset;
  1006. opr.ref_farproc_entry:=false;
  1007. opr.ref.base:=tparavarsym(localsym).paraloc[calleeside].Location^.register;
  1008. opr.ref.offset:=localsymofs;
  1009. opr.ref.index:=localindexreg;
  1010. opr.ref.scalefactor:=localscale;
  1011. end
  1012. else
  1013. InitRefError;
  1014. end
  1015. else
  1016. InitRef;
  1017. end;
  1018. procedure TOperand.InitRefError;
  1019. begin
  1020. Message(asmr_e_invalid_operand_type);
  1021. { Recover }
  1022. opr.typ:=OPR_REFERENCE;
  1023. opr.varsize:=0;
  1024. opr.constoffset:=0;
  1025. opr.ref_farproc_entry:=false;
  1026. Fillchar(opr.ref,sizeof(treference),0);
  1027. end;
  1028. Function TOperand.CheckOperand: boolean;
  1029. {*********************************************************************}
  1030. { Description: This routine checks if the operand is of }
  1031. { valid, and returns false if it isn't. Does nothing by default. }
  1032. {*********************************************************************}
  1033. begin
  1034. result:=true;
  1035. end;
  1036. {****************************************************************************
  1037. TInstruction
  1038. ****************************************************************************}
  1039. constructor TInstruction.create(optype : tcoperand);
  1040. var
  1041. i : longint;
  1042. Begin
  1043. { these field are set to 0 anyways by the constructor helper (FK)
  1044. Opcode:=A_NONE;
  1045. Condition:=C_NONE;
  1046. Ops:=0;
  1047. }
  1048. filepos:=current_filepos;
  1049. for i:=1 to max_operands do
  1050. Operands[i]:=optype.create;
  1051. Labeled:=false;
  1052. end;
  1053. destructor TInstruction.destroy;
  1054. var
  1055. i : longint;
  1056. Begin
  1057. for i:=1 to max_operands do
  1058. Operands[i].free;
  1059. end;
  1060. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  1061. var
  1062. ai : taicpu;
  1063. i : longint;
  1064. begin
  1065. for i:=1 to Ops do
  1066. operands[i].CheckOperand;
  1067. ai:=taicpu.op_none(opcode);
  1068. ai.fileinfo:=filepos;
  1069. ai.Ops:=Ops;
  1070. ai.Allocate_oper(Ops);
  1071. for i:=1 to Ops do
  1072. with operands[i].opr do
  1073. begin
  1074. case typ of
  1075. OPR_CONSTANT :
  1076. ai.loadconst(i-1,val);
  1077. OPR_REGISTER:
  1078. ai.loadreg(i-1,reg);
  1079. OPR_SYMBOL:
  1080. ai.loadsymbol(i-1,symbol,symofs);
  1081. OPR_LOCAL :
  1082. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1083. localscale,localgetoffset,localforceref);
  1084. OPR_REFERENCE:
  1085. ai.loadref(i-1,ref);
  1086. {$ifdef m68k}
  1087. OPR_REGSET:
  1088. ai.loadregset(i-1,regsetdata,regsetaddr,regsetfpu);
  1089. OPR_REGPAIR:
  1090. ai.loadregpair(i-1,reghi,reglo);
  1091. {$endif}
  1092. {$ifdef ARM}
  1093. OPR_REGSET:
  1094. ai.loadregset(i-1,regtype,subreg,regset,usermode);
  1095. OPR_MODEFLAGS:
  1096. ai.loadmodeflags(i-1,flags);
  1097. OPR_SPECIALREG:
  1098. ai.loadspecialreg(i-1,specialreg,specialregflags);
  1099. {$endif ARM}
  1100. {$if defined(arm) or defined(aarch64)}
  1101. OPR_SHIFTEROP:
  1102. ai.loadshifterop(i-1,shifterop);
  1103. OPR_COND:
  1104. ai.loadconditioncode(i-1,cc);
  1105. {$endif arm or aarch64}
  1106. { ignore wrong operand }
  1107. OPR_NONE:
  1108. ;
  1109. else
  1110. internalerror(200501051);
  1111. end;
  1112. end;
  1113. ai.SetCondition(condition);
  1114. { Concat the opcode or give an error }
  1115. if assigned(ai) then
  1116. p.concat(ai)
  1117. else
  1118. Message(asmr_e_invalid_opcode_and_operand);
  1119. result:=ai;
  1120. end;
  1121. {****************************************************************************
  1122. Symbol table helper routines
  1123. ****************************************************************************}
  1124. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:TSymtable);
  1125. var
  1126. i : integer;
  1127. begin
  1128. i:=pos('.',s);
  1129. { allow unit.identifier }
  1130. if i>0 then
  1131. begin
  1132. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1133. if assigned(srsym) then
  1134. begin
  1135. if (srsym.typ=unitsym) and
  1136. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1137. srsym.owner.iscurrentunit then
  1138. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1139. else
  1140. begin
  1141. srsym:=nil;
  1142. srsymtable:=nil;
  1143. end;
  1144. end;
  1145. end
  1146. else
  1147. searchsym(s,srsym,srsymtable);
  1148. end;
  1149. Function SearchType(const hs:string;var size:tcgint): Boolean;
  1150. var
  1151. srsym : tsym;
  1152. srsymtable : TSymtable;
  1153. begin
  1154. result:=false;
  1155. size:=0;
  1156. asmsearchsym(hs,srsym,srsymtable);
  1157. if assigned(srsym) and
  1158. (srsym.typ=typesym) then
  1159. begin
  1160. size:=ttypesym(srsym).typedef.size;
  1161. result:=true;
  1162. end;
  1163. end;
  1164. Function SearchRecordType(const s:string): boolean;
  1165. var
  1166. srsym : tsym;
  1167. srsymtable : TSymtable;
  1168. Begin
  1169. SearchRecordType:=false;
  1170. { Check the constants in symtable }
  1171. asmsearchsym(s,srsym,srsymtable);
  1172. if srsym <> nil then
  1173. Begin
  1174. case srsym.typ of
  1175. typesym :
  1176. begin
  1177. if ttypesym(srsym).typedef.typ in [recorddef,objectdef] then
  1178. begin
  1179. SearchRecordType:=true;
  1180. exit;
  1181. end;
  1182. end;
  1183. fieldvarsym :
  1184. begin
  1185. if (tfieldvarsym(srsym).vardef.typ in [recorddef,objectdef]) then
  1186. begin
  1187. SearchRecordType:=true;
  1188. exit;
  1189. end;
  1190. end;
  1191. end;
  1192. end;
  1193. end;
  1194. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  1195. {**********************************************************************}
  1196. { Description: Searches for a CONSTANT of name s in either the local }
  1197. { symbol list, then in the global symbol list, and returns the value }
  1198. { of that constant in l. Returns TRUE if successfull, if not found, }
  1199. { or if the constant is not of correct type, then returns FALSE }
  1200. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1201. { respectively. }
  1202. {**********************************************************************}
  1203. var
  1204. srsym : tsym;
  1205. srsymtable : TSymtable;
  1206. Begin
  1207. SearchIConstant:=false;
  1208. { check for TRUE or FALSE reserved words first }
  1209. if s = 'TRUE' then
  1210. Begin
  1211. SearchIConstant:=TRUE;
  1212. l:=1;
  1213. exit;
  1214. end;
  1215. if s = 'FALSE' then
  1216. Begin
  1217. SearchIConstant:=TRUE;
  1218. l:=0;
  1219. exit;
  1220. end;
  1221. { Check the constants in symtable }
  1222. asmsearchsym(s,srsym,srsymtable);
  1223. if srsym <> nil then
  1224. Begin
  1225. case srsym.typ of
  1226. constsym :
  1227. begin
  1228. if tconstsym(srsym).consttyp=constord then
  1229. Begin
  1230. l:=tconstsym(srsym).value.valueord.svalue;
  1231. SearchIConstant:=TRUE;
  1232. exit;
  1233. end;
  1234. end;
  1235. enumsym:
  1236. Begin
  1237. l:=tenumsym(srsym).value;
  1238. SearchIConstant:=TRUE;
  1239. exit;
  1240. end;
  1241. end;
  1242. end;
  1243. end;
  1244. function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  1245. begin
  1246. result:=
  1247. (po_assembler in current_procinfo.procdef.procoptions) and
  1248. (sym.typ=paravarsym) and
  1249. (tparavarsym(sym).paraloc[calleeside].Location^.Loc=LOC_REGISTER);
  1250. end;
  1251. Function GetRecordOffsetSize(s:string;Var Offset: tcgint;var Size:tcgint; var mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  1252. { search and returns the offset and size of records/objects of the base }
  1253. { with field name setup in field. }
  1254. { returns FALSE if not found. }
  1255. { used when base is a variable or a typed constant name. }
  1256. var
  1257. st : TSymtable;
  1258. harrdef : tarraydef;
  1259. sym : tsym;
  1260. srsymtable : TSymtable;
  1261. i : longint;
  1262. base : string;
  1263. procdef: tprocdef;
  1264. Begin
  1265. GetRecordOffsetSize:=FALSE;
  1266. Offset:=0;
  1267. Size:=0;
  1268. mangledname:='';
  1269. hastypecast:=false;
  1270. i:=pos('.',s);
  1271. if i=0 then
  1272. i:=255;
  1273. base:=Copy(s,1,i-1);
  1274. delete(s,1,i);
  1275. if base='SELF' then
  1276. st:=current_structdef.symtable
  1277. else
  1278. begin
  1279. asmsearchsym(base,sym,srsymtable);
  1280. st:=nil;
  1281. { we can start with a var,type,typedconst }
  1282. if assigned(sym) then
  1283. case sym.typ of
  1284. staticvarsym,
  1285. localvarsym,
  1286. paravarsym :
  1287. st:=Tabstractvarsym(sym).vardef.GetSymtable(gs_record);
  1288. typesym :
  1289. st:=Ttypesym(sym).typedef.GetSymtable(gs_record);
  1290. end
  1291. else
  1292. s:='';
  1293. end;
  1294. { now walk all recordsymtables }
  1295. while assigned(st) and (s<>'') do
  1296. begin
  1297. { load next field in base }
  1298. i:=pos('.',s);
  1299. if i=0 then
  1300. i:=255;
  1301. base:=Copy(s,1,i-1);
  1302. delete(s,1,i);
  1303. sym:=search_struct_member(tabstractrecorddef(st.defowner),base);
  1304. if not assigned(sym) then
  1305. begin
  1306. GetRecordOffsetSize:=false;
  1307. exit;
  1308. end;
  1309. st:=nil;
  1310. case sym.typ of
  1311. fieldvarsym :
  1312. with Tfieldvarsym(sym) do
  1313. begin
  1314. if not tabstractrecordsymtable(sym.owner).is_packed then
  1315. inc(Offset,fieldoffset)
  1316. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  1317. inc(Offset,fieldoffset div 8)
  1318. else
  1319. Message(asmr_e_packed_element);
  1320. size:=getsize;
  1321. case vardef.typ of
  1322. arraydef :
  1323. begin
  1324. { for arrays try to get the element size, take care of
  1325. multiple indexes }
  1326. harrdef:=tarraydef(vardef);
  1327. while assigned(harrdef.elementdef) and
  1328. (harrdef.elementdef.typ=arraydef) do
  1329. harrdef:=tarraydef(harrdef.elementdef);
  1330. if not is_packed_array(harrdef) then
  1331. size:=harrdef.elesize
  1332. else
  1333. begin
  1334. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1335. Message(asmr_e_packed_element);
  1336. size := (harrdef.elepackedbitsize + 7) div 8;
  1337. end;
  1338. end;
  1339. recorddef :
  1340. st:=trecorddef(vardef).symtable;
  1341. objectdef :
  1342. st:=tobjectdef(vardef).symtable;
  1343. end;
  1344. end;
  1345. procsym:
  1346. begin
  1347. st:=nil;
  1348. if Tprocsym(sym).ProcdefList.Count>1 then
  1349. Message(asmr_w_calling_overload_func);
  1350. procdef:=tprocdef(tprocsym(sym).ProcdefList[0]);
  1351. if (not needvmtofs) then
  1352. begin
  1353. mangledname:=procdef.mangledname;
  1354. end
  1355. else
  1356. begin
  1357. { can only get the vmtoffset of virtual methods }
  1358. if not(po_virtualmethod in procdef.procoptions) or
  1359. is_objectpascal_helper(procdef.struct) then
  1360. Message1(asmr_e_no_vmtoffset_possible,FullTypeName(procdef,nil))
  1361. else
  1362. begin
  1363. { size = sizeof(target_system_pointer) }
  1364. size:=sizeof(pint);
  1365. offset:=tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber)
  1366. end;
  1367. end;
  1368. { if something comes after the procsym, it's invalid assembler syntax }
  1369. GetRecordOffsetSize:=(s='');
  1370. exit;
  1371. end;
  1372. end;
  1373. end;
  1374. { Support Field.Type as typecasting }
  1375. if (st=nil) and (s<>'') then
  1376. begin
  1377. asmsearchsym(s,sym,srsymtable);
  1378. if assigned(sym) and (sym.typ=typesym) then
  1379. begin
  1380. size:=ttypesym(sym).typedef.size;
  1381. s:='';
  1382. hastypecast:=true;
  1383. end;
  1384. end;
  1385. GetRecordOffsetSize:=(s='');
  1386. end;
  1387. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1388. var
  1389. sym : tsym;
  1390. srsymtable : TSymtable;
  1391. hs : string;
  1392. Begin
  1393. hl:=nil;
  1394. SearchLabel:=false;
  1395. { Check for pascal labels, which are case insensetive }
  1396. hs:=upper(s);
  1397. asmsearchsym(hs,sym,srsymtable);
  1398. if sym=nil then
  1399. exit;
  1400. case sym.typ of
  1401. labelsym :
  1402. begin
  1403. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1404. begin
  1405. Tlabelsym(sym).nonlocal:=true;
  1406. if emit then
  1407. exclude(current_procinfo.procdef.procoptions,po_inline);
  1408. end;
  1409. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1410. if Tlabelsym(sym).nonlocal then
  1411. current_asmdata.getglobaljumplabel(tlabelsym(sym).asmblocklabel)
  1412. else
  1413. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1414. hl:=tlabelsym(sym).asmblocklabel;
  1415. if emit then
  1416. begin
  1417. if tlabelsym(sym).defined then
  1418. Message(sym_e_label_already_defined);
  1419. tlabelsym(sym).defined:=true
  1420. end
  1421. else
  1422. tlabelsym(sym).used:=true;
  1423. SearchLabel:=true;
  1424. end;
  1425. end;
  1426. end;
  1427. {*************************************************************************}
  1428. { Instruction Generation Utilities }
  1429. {*************************************************************************}
  1430. Procedure ConcatString(p : TAsmList;s:string);
  1431. {*********************************************************************}
  1432. { PROCEDURE ConcatString(s:string); }
  1433. { Description: This routine adds the character chain pointed to in }
  1434. { s to the instruction linked list. }
  1435. {*********************************************************************}
  1436. Begin
  1437. p.concat(Tai_string.Create(s));
  1438. end;
  1439. Procedure ConcatConstant(p: TAsmList; value: tcgint; constsize:byte);
  1440. {*********************************************************************}
  1441. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1442. { Description: This routine adds the value constant to the current }
  1443. { instruction linked list. }
  1444. { maxvalue -> indicates the size of the data to initialize: }
  1445. { $ff -> create a byte node. }
  1446. { $ffff -> create a word node. }
  1447. { $ffffffff -> create a dword node. }
  1448. {*********************************************************************}
  1449. var
  1450. rangelo,rangehi : int64;
  1451. Begin
  1452. case constsize of
  1453. 1 :
  1454. begin
  1455. p.concat(Tai_const.Create_8bit(byte(value)));
  1456. rangelo:=low(shortint);
  1457. rangehi:=high(byte);
  1458. end;
  1459. 2 :
  1460. begin
  1461. p.concat(Tai_const.Create_16bit(word(value)));
  1462. rangelo:=low(smallint);
  1463. rangehi:=high(word);
  1464. end;
  1465. 4 :
  1466. begin
  1467. p.concat(Tai_const.Create_32bit(longint(value)));
  1468. rangelo:=low(longint);
  1469. rangehi:=high(cardinal);
  1470. end;
  1471. 8 :
  1472. begin
  1473. p.concat(Tai_const.Create_64bit(int64(value)));
  1474. rangelo:=0;
  1475. rangehi:=0;
  1476. end;
  1477. else
  1478. internalerror(200405011);
  1479. end;
  1480. { check for out of bounds }
  1481. if (rangelo<>0) and
  1482. ((value>rangehi) or (value<rangelo)) then
  1483. Message(asmr_e_constant_out_of_bounds);
  1484. end;
  1485. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint);
  1486. begin
  1487. p.concat(Tai_const.Createname(sym,l));
  1488. end;
  1489. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1490. {***********************************************************************}
  1491. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1492. { Description: This routine adds the value constant to the current }
  1493. { instruction linked list. }
  1494. { real_typ -> indicates the type of the real data to initialize: }
  1495. { s32real -> create a single node. }
  1496. { s64real -> create a double node. }
  1497. { s80real -> create an extended node. }
  1498. { s64bit -> create a comp node. }
  1499. { f32bit -> create a fixed node. (not used normally) }
  1500. {***********************************************************************}
  1501. Begin
  1502. case real_typ of
  1503. s32real : p.concat(tai_realconst.create_s32real(value));
  1504. s64real :
  1505. {$ifdef ARM}
  1506. if is_double_hilo_swapped then
  1507. p.concat(tai_realconst.create_s64real_hiloswapped(value))
  1508. else
  1509. {$endif ARM}
  1510. p.concat(tai_realconst.create_s64real(value));
  1511. s80real : p.concat(tai_realconst.create_s80real(value,s80floattype.size));
  1512. sc80real : p.concat(tai_realconst.create_s80real(value,sc80floattype.size));
  1513. s64comp : p.concat(tai_realconst.create_s64compreal(trunc(value)));
  1514. else
  1515. internalerror(2014050608);
  1516. end;
  1517. end;
  1518. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1519. {*********************************************************************}
  1520. { PROCEDURE ConcatLabel }
  1521. { Description: This routine either emits a label or a labeled }
  1522. { instruction to the linked list of instructions. }
  1523. {*********************************************************************}
  1524. begin
  1525. p.concat(Tai_label.Create(l));
  1526. end;
  1527. procedure ConcatAlign(p:TAsmList;l:tcgint);
  1528. {*********************************************************************}
  1529. { PROCEDURE ConcatPublic }
  1530. { Description: This routine emits an global definition to the }
  1531. { linked list of instructions.(used by AT&T styled asm) }
  1532. {*********************************************************************}
  1533. begin
  1534. p.concat(Tai_align.Create(l));
  1535. end;
  1536. procedure ConcatPublic(p:TAsmList;const s : string);
  1537. {*********************************************************************}
  1538. { PROCEDURE ConcatPublic }
  1539. { Description: This routine emits an global definition to the }
  1540. { linked list of instructions.(used by AT&T styled asm) }
  1541. {*********************************************************************}
  1542. begin
  1543. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0,voidcodepointertype));
  1544. end;
  1545. procedure ConcatLocal(p:TAsmList;const s : string);
  1546. {*********************************************************************}
  1547. { PROCEDURE ConcatLocal }
  1548. { Description: This routine emits an local definition to the }
  1549. { linked list of instructions. }
  1550. {*********************************************************************}
  1551. begin
  1552. p.concat(Tai_symbol.Createname(s,AT_LABEL,0,voidcodepointertype));
  1553. end;
  1554. end.