rautils.pas 46 KB

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