rautils.pas 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  4. This unit implements some support routines for assembler parsing
  5. independent of the processor
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. **********************************************************************}
  18. Unit RAUtils;
  19. {$i fpcdefs.inc}
  20. Interface
  21. Uses
  22. cutils,cclasses,
  23. globtype,aasmbase,aasmtai,cpubase,cpuinfo,cgbase,
  24. symconst,symbase,symtype,symdef,symsym;
  25. Const
  26. RPNMax = 10; { I think you only need 4, but just to be safe }
  27. OpMax = 25;
  28. {---------------------------------------------------------------------
  29. Local Label Management
  30. ---------------------------------------------------------------------}
  31. Type
  32. { Each local label has this structure associated with it }
  33. TLocalLabel = class(TNamedIndexItem)
  34. Emitted : boolean;
  35. constructor Create(const n:string);
  36. function Gettasmlabel:tasmlabel;
  37. private
  38. lab : tasmlabel;
  39. end;
  40. TLocalLabelList = class(TDictionary)
  41. procedure CheckEmitted;
  42. end;
  43. var
  44. LocalLabelList : TLocalLabelList;
  45. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  46. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  47. {---------------------------------------------------------------------
  48. Instruction management
  49. ---------------------------------------------------------------------}
  50. type
  51. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  52. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST,OPR_COND,OPR_REGSET);
  53. TOprRec = record
  54. case typ:TOprType of
  55. OPR_NONE : ();
  56. OPR_CONSTANT : (val:longint);
  57. OPR_SYMBOL : (symbol:tasmsymbol;symofs:longint);
  58. OPR_REFERENCE : (ref:treference);
  59. OPR_LOCAL : (localsym:tvarsym;localsymofs:longint;localindexreg:tregister;localscale:byte;localgetoffset:boolean);
  60. OPR_REGISTER : (reg:tregister);
  61. {$ifdef m68k}
  62. OPR_REGLIST : (reglist:Tsupregset);
  63. {$endif m68k}
  64. {$ifdef powerpc}
  65. OPR_COND : (cond : tasmcond);
  66. {$endif powerpc}
  67. {$ifdef arm}
  68. OPR_REGSET : (regset : tcpuregisterset);
  69. {$endif arm}
  70. end;
  71. TOperand = class
  72. hastype, { if the operand has typecasted variable }
  73. hasvar : boolean; { if the operand is loaded with a variable }
  74. size : TCGSize;
  75. opr : TOprRec;
  76. constructor create;virtual;
  77. destructor destroy;override;
  78. Procedure SetSize(_size:longint;force:boolean);virtual;
  79. Procedure SetCorrectSize(opcode:tasmop);virtual;
  80. Function SetupResult:boolean;virtual;
  81. Function SetupSelf:boolean;
  82. Function SetupOldEBP:boolean;
  83. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  84. Function SetupDirectVar(const hs:string): Boolean;
  85. Procedure InitRef;
  86. end;
  87. TCOperand = class of TOperand;
  88. TInstruction = class
  89. opcode : tasmop;
  90. condition : tasmcond;
  91. ops : byte;
  92. labeled : boolean;
  93. operands : array[1..max_operands] of toperand;
  94. constructor create(optype : tcoperand);virtual;
  95. destructor destroy;override;
  96. Procedure BuildOpcode;virtual;abstract;
  97. { converts the instruction to an instruction how it's used by the assembler writer
  98. and concats it to the passed list, the newly created item is returned }
  99. function ConcatInstruction(p:TAAsmoutput) : tai;virtual;
  100. Procedure Swapoperands;
  101. end;
  102. tstr2opentry = class(Tnamedindexitem)
  103. op: TAsmOp;
  104. end;
  105. {---------------------------------------------------------------------}
  106. { Expression parser types }
  107. {---------------------------------------------------------------------}
  108. TExprOperator = record
  109. ch: char; { operator }
  110. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  111. end;
  112. String15 = String[15];
  113. {**********************************************************************}
  114. { The following operators are supported: }
  115. { '+' : addition }
  116. { '-' : subtraction }
  117. { '*' : multiplication }
  118. { '/' : modulo division }
  119. { '^' : exclusive or }
  120. { '<' : shift left }
  121. { '>' : shift right }
  122. { '&' : bitwise and }
  123. { '|' : bitwise or }
  124. { '~' : bitwise complement }
  125. { '%' : modulo division }
  126. { nnn: longint numbers }
  127. { ( and ) parenthesis }
  128. {**********************************************************************}
  129. TExprParse = class
  130. public
  131. Constructor create;
  132. Destructor Destroy;override;
  133. Function Evaluate(Expr: String): longint;
  134. Function Priority(_Operator: Char): longint;
  135. private
  136. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  137. RPNTop : longint;
  138. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  139. OpTop : longint;
  140. Procedure RPNPush(Num: Longint);
  141. Function RPNPop: Longint;
  142. Procedure RPNCalc(token: String15; prefix: boolean);
  143. Procedure OpPush(_Operator: char; prefix: boolean);
  144. { In reality returns TExprOperaotr }
  145. Procedure OpPop(var _Operator:TExprOperator);
  146. end;
  147. { Evaluate an expression string to a longint }
  148. Function CalculateExpression(const expression: string): longint;
  149. {---------------------------------------------------------------------}
  150. { String routines }
  151. {---------------------------------------------------------------------}
  152. Function ValDecimal(const S:String):longint;
  153. Function ValOctal(const S:String):longint;
  154. Function ValBinary(const S:String):longint;
  155. Function ValHexaDecimal(const S:String):longint;
  156. Function PadZero(Var s: String; n: byte): Boolean;
  157. Function EscapeToPascal(const s:string): string;
  158. {---------------------------------------------------------------------
  159. Symbol helper routines
  160. ---------------------------------------------------------------------}
  161. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  162. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  163. Function SearchType(const hs:string;var size:longint): Boolean;
  164. Function SearchRecordType(const s:string): boolean;
  165. Function SearchIConstant(const s:string; var l:longint): boolean;
  166. {---------------------------------------------------------------------
  167. Instruction generation routines
  168. ---------------------------------------------------------------------}
  169. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  170. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  171. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  172. Procedure ConcatConstant(p : TAAsmoutput;value: longint; maxvalue: longint);
  173. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  174. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  175. Procedure ConcatString(p : TAAsmoutput;s:string);
  176. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  177. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  178. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  179. Procedure ConcatGlobalBss(const s : string;size : longint);
  180. Procedure ConcatLocalBss(const s : string;size : longint);
  181. Implementation
  182. uses
  183. {$ifdef delphi}
  184. sysutils,
  185. {$else}
  186. strings,
  187. {$endif}
  188. defutil,systems,verbose,globals,
  189. symtable,paramgr,
  190. aasmcpu,
  191. procinfo;
  192. {*************************************************************************
  193. TExprParse
  194. *************************************************************************}
  195. Constructor TExprParse.create;
  196. Begin
  197. end;
  198. Procedure TExprParse.RPNPush(Num : longint);
  199. { Add an operand to the top of the RPN stack }
  200. begin
  201. if RPNTop < RPNMax then
  202. begin
  203. Inc(RPNTop);
  204. RPNStack[RPNTop]:=Num;
  205. end
  206. else
  207. Message(asmr_e_expr_illegal);
  208. end;
  209. Function TExprParse.RPNPop : longint;
  210. { Get the operand at the top of the RPN stack }
  211. begin
  212. if RPNTop > 0 then
  213. begin
  214. RPNPop:=RPNStack[RPNTop];
  215. Dec(RPNTop);
  216. end
  217. else
  218. Message(asmr_e_expr_illegal);
  219. end;
  220. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  221. Var
  222. Temp : longint;
  223. n1,n2 : longint;
  224. LocalError : Integer;
  225. begin
  226. { Handle operators }
  227. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  228. Case Token[1] of
  229. '+' :
  230. Begin
  231. if not prefix then
  232. RPNPush(RPNPop + RPNPop);
  233. end;
  234. '-' :
  235. Begin
  236. if prefix then
  237. RPNPush(-(RPNPop))
  238. else
  239. begin
  240. n1:=RPNPop;
  241. n2:=RPNPop;
  242. RPNPush(n2 - n1);
  243. end;
  244. end;
  245. '*' : RPNPush(RPNPop * RPNPop);
  246. '&' :
  247. begin
  248. n1:=RPNPop;
  249. n2:=RPNPop;
  250. RPNPush(n2 and n1);
  251. end;
  252. '|' :
  253. begin
  254. n1:=RPNPop;
  255. n2:=RPNPop;
  256. RPNPush(n2 or n1);
  257. end;
  258. '~' : RPNPush(NOT RPNPop);
  259. '<' :
  260. begin
  261. n1:=RPNPop;
  262. n2:=RPNPop;
  263. RPNPush(n2 SHL n1);
  264. end;
  265. '>' :
  266. begin
  267. n1:=RPNPop;
  268. n2:=RPNPop;
  269. RPNPush(n2 SHR n1);
  270. end;
  271. '%' :
  272. begin
  273. Temp:=RPNPop;
  274. if Temp <> 0 then
  275. RPNPush(RPNPop mod Temp)
  276. else
  277. begin
  278. Message(asmr_e_expr_zero_divide);
  279. { push 1 for error recovery }
  280. RPNPush(1);
  281. end;
  282. end;
  283. '^' : RPNPush(RPNPop XOR RPNPop);
  284. '/' :
  285. begin
  286. Temp:=RPNPop;
  287. if Temp <> 0 then
  288. RPNPush(RPNPop div Temp)
  289. else
  290. begin
  291. Message(asmr_e_expr_zero_divide);
  292. { push 1 for error recovery }
  293. RPNPush(1);
  294. end;
  295. end;
  296. end
  297. else
  298. begin
  299. { Convert String to number and add to stack }
  300. Val(Token, Temp, LocalError);
  301. if LocalError = 0 then
  302. RPNPush(Temp)
  303. else
  304. begin
  305. Message(asmr_e_expr_illegal);
  306. { push 1 for error recovery }
  307. RPNPush(1);
  308. end;
  309. end;
  310. end;
  311. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  312. { Add an operator onto top of the stack }
  313. begin
  314. if OpTop < OpMax then
  315. begin
  316. Inc(OpTop);
  317. OpStack[OpTop].ch:=_Operator;
  318. OpStack[OpTop].is_prefix:=prefix;
  319. end
  320. else
  321. Message(asmr_e_expr_illegal);
  322. end;
  323. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  324. { Get operator at the top of the stack }
  325. begin
  326. if OpTop > 0 then
  327. begin
  328. _Operator:=OpStack[OpTop];
  329. Dec(OpTop);
  330. end
  331. else
  332. Message(asmr_e_expr_illegal);
  333. end;
  334. Function TExprParse.Priority(_Operator : Char) : longint;
  335. { Return priority of operator }
  336. { The greater the priority, the higher the precedence }
  337. begin
  338. Case _Operator OF
  339. '(' :
  340. Priority:=0;
  341. '+', '-' :
  342. Priority:=1;
  343. '*', '/','%','<','>' :
  344. Priority:=2;
  345. '|','&','^','~' :
  346. Priority:=0;
  347. else
  348. Message(asmr_e_expr_illegal);
  349. end;
  350. end;
  351. Function TExprParse.Evaluate(Expr : String):longint;
  352. Var
  353. I : LongInt;
  354. Token : String15;
  355. opr : TExprOperator;
  356. begin
  357. Evaluate:=0;
  358. { Reset stacks }
  359. OpTop :=0;
  360. RPNTop:=0;
  361. Token :='';
  362. { nothing to do ? }
  363. if Expr='' then
  364. exit;
  365. For I:=1 to Length(Expr) DO
  366. begin
  367. if Expr[I] in ['0'..'9'] then
  368. begin { Build multi-digit numbers }
  369. Token:=Token + Expr[I];
  370. if I = Length(Expr) then { Send last one to calculator }
  371. RPNCalc(Token,false);
  372. end
  373. else
  374. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  375. begin
  376. if Token <> '' then
  377. begin { Send last built number to calc. }
  378. RPNCalc(Token,false);
  379. Token:='';
  380. end;
  381. Case Expr[I] OF
  382. '(' : OpPush('(',false);
  383. ')' : begin
  384. While OpStack[OpTop].ch <> '(' DO
  385. Begin
  386. OpPop(opr);
  387. RPNCalc(opr.ch,opr.is_prefix);
  388. end;
  389. OpPop(opr); { Pop off and ignore the '(' }
  390. end;
  391. '+','-','~' : Begin
  392. { workaround for -2147483648 }
  393. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  394. begin
  395. token:='-';
  396. expr[i]:='+';
  397. end;
  398. { if start of expression then surely a prefix }
  399. { or if previous char was also an operator }
  400. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  401. OpPush(Expr[I],true)
  402. else
  403. Begin
  404. { Evaluate all higher priority operators }
  405. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  406. Begin
  407. OpPop(opr);
  408. RPNCalc(opr.ch,opr.is_prefix);
  409. end;
  410. OpPush(Expr[I],false);
  411. End;
  412. end;
  413. '*', '/',
  414. '^','|','&',
  415. '%','<','>' : begin
  416. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  417. Begin
  418. OpPop(opr);
  419. RPNCalc(opr.ch,opr.is_prefix);
  420. end;
  421. OpPush(Expr[I],false);
  422. end;
  423. end; { Case }
  424. end
  425. else
  426. Message(asmr_e_expr_illegal); { Handle bad input error }
  427. end;
  428. { Pop off the remaining operators }
  429. While OpTop > 0 do
  430. Begin
  431. OpPop(opr);
  432. RPNCalc(opr.ch,opr.is_prefix);
  433. end;
  434. { The result is stored on the top of the stack }
  435. Evaluate:=RPNPop;
  436. end;
  437. Destructor TExprParse.Destroy;
  438. Begin
  439. end;
  440. Function CalculateExpression(const expression: string): longint;
  441. var
  442. expr: TExprParse;
  443. Begin
  444. expr:=TExprParse.create;
  445. CalculateExpression:=expr.Evaluate(expression);
  446. expr.Free;
  447. end;
  448. {*************************************************************************}
  449. { String conversions/utils }
  450. {*************************************************************************}
  451. Function EscapeToPascal(const s:string): string;
  452. { converts a C styled string - which contains escape }
  453. { characters to a pascal style string. }
  454. var
  455. i,len : longint;
  456. hs : string;
  457. temp : string;
  458. c : char;
  459. Begin
  460. hs:='';
  461. len:=0;
  462. i:=0;
  463. while (i<length(s)) and (len<255) do
  464. begin
  465. Inc(i);
  466. if (s[i]='\') and (i<length(s)) then
  467. Begin
  468. inc(i);
  469. case s[i] of
  470. '\' :
  471. c:='\';
  472. 'b':
  473. c:=#8;
  474. 'f':
  475. c:=#12;
  476. 'n':
  477. c:=#10;
  478. 'r':
  479. c:=#13;
  480. 't':
  481. c:=#9;
  482. '"':
  483. c:='"';
  484. '0'..'7':
  485. Begin
  486. temp:=s[i];
  487. temp:=temp+s[i+1];
  488. temp:=temp+s[i+2];
  489. inc(i,2);
  490. c:=chr(ValOctal(temp));
  491. end;
  492. 'x':
  493. Begin
  494. temp:=s[i+1];
  495. temp:=temp+s[i+2];
  496. inc(i,2);
  497. c:=chr(ValHexaDecimal(temp));
  498. end;
  499. else
  500. Begin
  501. Message1(asmr_e_escape_seq_ignored,s[i]);
  502. c:=s[i];
  503. end;
  504. end;
  505. end
  506. else
  507. c:=s[i];
  508. inc(len);
  509. hs[len]:=c;
  510. end;
  511. hs[0]:=chr(len);
  512. EscapeToPascal:=hs;
  513. end;
  514. Function ValDecimal(const S:String):longint;
  515. { Converts a decimal string to longint }
  516. var
  517. vs,c : longint;
  518. Begin
  519. vs:=0;
  520. for c:=1 to length(s) do
  521. begin
  522. vs:=vs*10;
  523. if s[c] in ['0'..'9'] then
  524. inc(vs,ord(s[c])-ord('0'))
  525. else
  526. begin
  527. Message1(asmr_e_error_converting_decimal,s);
  528. ValDecimal:=0;
  529. exit;
  530. end;
  531. end;
  532. ValDecimal:=vs;
  533. end;
  534. Function ValOctal(const S:String):longint;
  535. { Converts an octal string to longint }
  536. var
  537. vs,c : longint;
  538. Begin
  539. vs:=0;
  540. for c:=1 to length(s) do
  541. begin
  542. vs:=vs shl 3;
  543. if s[c] in ['0'..'7'] then
  544. inc(vs,ord(s[c])-ord('0'))
  545. else
  546. begin
  547. Message1(asmr_e_error_converting_octal,s);
  548. ValOctal:=0;
  549. exit;
  550. end;
  551. end;
  552. ValOctal:=vs;
  553. end;
  554. Function ValBinary(const S:String):longint;
  555. { Converts a binary string to longint }
  556. var
  557. vs,c : longint;
  558. Begin
  559. vs:=0;
  560. for c:=1 to length(s) do
  561. begin
  562. vs:=vs shl 1;
  563. if s[c] in ['0'..'1'] then
  564. inc(vs,ord(s[c])-ord('0'))
  565. else
  566. begin
  567. Message1(asmr_e_error_converting_binary,s);
  568. ValBinary:=0;
  569. exit;
  570. end;
  571. end;
  572. ValBinary:=vs;
  573. end;
  574. Function ValHexadecimal(const S:String):longint;
  575. { Converts a binary string to longint }
  576. var
  577. vs,c : longint;
  578. Begin
  579. vs:=0;
  580. for c:=1 to length(s) do
  581. begin
  582. vs:=vs shl 4;
  583. case s[c] of
  584. '0'..'9' :
  585. inc(vs,ord(s[c])-ord('0'));
  586. 'A'..'F' :
  587. inc(vs,ord(s[c])-ord('A')+10);
  588. 'a'..'f' :
  589. inc(vs,ord(s[c])-ord('a')+10);
  590. else
  591. begin
  592. Message1(asmr_e_error_converting_hexadecimal,s);
  593. ValHexadecimal:=0;
  594. exit;
  595. end;
  596. end;
  597. end;
  598. ValHexadecimal:=vs;
  599. end;
  600. Function PadZero(Var s: String; n: byte): Boolean;
  601. Begin
  602. PadZero:=TRUE;
  603. { Do some error checking first }
  604. if Length(s) = n then
  605. exit
  606. else
  607. if Length(s) > n then
  608. Begin
  609. PadZero:=FALSE;
  610. delete(s,n+1,length(s));
  611. exit;
  612. end
  613. else
  614. PadZero:=TRUE;
  615. { Fill it up with the specified character }
  616. fillchar(s[length(s)+1],n-1,#0);
  617. s[0]:=chr(n);
  618. end;
  619. {****************************************************************************
  620. TOperand
  621. ****************************************************************************}
  622. constructor TOperand.Create;
  623. begin
  624. size:=OS_NO;
  625. hastype:=false;
  626. hasvar:=false;
  627. FillChar(Opr,sizeof(Opr),0);
  628. end;
  629. destructor TOperand.destroy;
  630. begin
  631. end;
  632. Procedure TOperand.SetSize(_size:longint;force:boolean);
  633. begin
  634. if force or
  635. ((size = OS_NO) and (_size<=extended_size)) then
  636. Begin
  637. case _size of
  638. 1 : size:=OS_8;
  639. 2 : size:=OS_16{ could be S_IS};
  640. 4 : size:=OS_32{ could be S_IL or S_FS};
  641. 8 : size:=OS_64{ could be S_D or S_FL};
  642. else
  643. begin
  644. { extended_size can also be 8, resulting in a
  645. duplicate label }
  646. if _size=extended_size then
  647. size:=OS_F80;
  648. end;
  649. end;
  650. end;
  651. end;
  652. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  653. begin
  654. end;
  655. function TOperand.SetupResult:boolean;
  656. begin
  657. SetupResult:=false;
  658. { replace by correct offset. }
  659. with current_procinfo.procdef do
  660. if (not is_void(rettype.def)) then
  661. begin
  662. if (m_tp7 in aktmodeswitches) and
  663. (not paramanager.ret_in_param(rettype.def,proccalloption)) then
  664. begin
  665. message(asmr_e_cannot_use_RESULT_here);
  666. exit;
  667. end;
  668. SetupResult:=setupvar('result',false)
  669. end
  670. else
  671. message(asmr_e_void_function);
  672. end;
  673. Function TOperand.SetupSelf:boolean;
  674. Begin
  675. SetupSelf:=false;
  676. if assigned(current_procinfo.procdef._class) then
  677. SetupSelf:=setupvar('self',false)
  678. else
  679. Message(asmr_e_cannot_use_SELF_outside_a_method);
  680. end;
  681. Function TOperand.SetupOldEBP:boolean;
  682. Begin
  683. SetupOldEBP:=false;
  684. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  685. SetupOldEBP:=setupvar('parentframe',false)
  686. else
  687. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  688. end;
  689. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  690. function symtable_has_varsyms(st:tsymtable):boolean;
  691. var
  692. sym : tsym;
  693. begin
  694. result:=false;
  695. sym:=tsym(st.symindex.first);
  696. while assigned(sym) do
  697. begin
  698. if sym.typ=varsym then
  699. begin
  700. result:=true;
  701. exit;
  702. end;
  703. sym:=tsym(sym.indexnext);
  704. end;
  705. end;
  706. procedure setconst(l:longint);
  707. begin
  708. { We return the address of the field, just like Delphi/TP }
  709. case opr.typ of
  710. OPR_NONE :
  711. begin
  712. opr.typ:=OPR_CONSTANT;
  713. opr.val:=l;
  714. end;
  715. OPR_CONSTANT :
  716. inc(opr.val,l);
  717. OPR_REFERENCE :
  718. inc(opr.ref.offset,l);
  719. OPR_LOCAL :
  720. inc(opr.localsymofs,l);
  721. else
  722. Message(asmr_e_invalid_operand_type);
  723. end;
  724. end;
  725. { search and sets up the correct fields in the Instr record }
  726. { for the NON-constant identifier passed to the routine. }
  727. { if not found returns FALSE. }
  728. var
  729. sym : tsym;
  730. srsymtable : tsymtable;
  731. harrdef : tarraydef;
  732. indexreg : tregister;
  733. l : longint;
  734. plist : psymlistitem;
  735. Begin
  736. SetupVar:=false;
  737. asmsearchsym(s,sym,srsymtable);
  738. if sym = nil then
  739. exit;
  740. if sym.typ=absolutesym then
  741. begin
  742. if (tabsolutesym(sym).abstyp=tovar) then
  743. begin
  744. { Only support simple loads }
  745. plist:=tabsolutesym(sym).ref.firstsym;
  746. if assigned(plist) and
  747. (plist^.sltype=sl_load) then
  748. sym:=plist^.sym
  749. else
  750. begin
  751. Message(asmr_e_unsupported_symbol_type);
  752. exit;
  753. end;
  754. end
  755. else
  756. begin
  757. Message(asmr_e_unsupported_symbol_type);
  758. exit;
  759. end;
  760. end;
  761. case sym.typ of
  762. varsym :
  763. begin
  764. { we always assume in asm statements that }
  765. { that the variable is valid. }
  766. tvarsym(sym).varstate:=vs_used;
  767. inc(tvarsym(sym).refs);
  768. case tvarsym(sym).owner.symtabletype of
  769. objectsymtable :
  770. begin
  771. setconst(tvarsym(sym).fieldoffset);
  772. hasvar:=true;
  773. SetupVar:=true;
  774. Exit;
  775. end;
  776. globalsymtable,
  777. staticsymtable :
  778. begin
  779. initref;
  780. opr.ref.symbol:=objectlibrary.newasmsymbol(tvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  781. end;
  782. parasymtable,
  783. localsymtable :
  784. begin
  785. if (vo_is_external in tvarsym(sym).varoptions) then
  786. begin
  787. initref;
  788. opr.ref.symbol:=objectlibrary.newasmsymbol(tvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA)
  789. end
  790. else
  791. begin
  792. if opr.typ=OPR_REFERENCE then
  793. begin
  794. indexreg:=opr.ref.base;
  795. if opr.ref.index<>NR_NO then
  796. begin
  797. if indexreg=NR_NO then
  798. indexreg:=opr.ref.index
  799. else
  800. Message(asmr_e_multiple_index);
  801. end;
  802. end
  803. else
  804. indexreg:=NR_NO;
  805. opr.typ:=OPR_LOCAL;
  806. if assigned(current_procinfo.parent) and
  807. (current_procinfo.procdef.proccalloption<>pocall_inline) and
  808. (tvarsym(sym).owner<>current_procinfo.procdef.localst) and
  809. (tvarsym(sym).owner<>current_procinfo.procdef.parast) and
  810. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  811. symtable_has_varsyms(current_procinfo.procdef.localst) then
  812. message1(asmr_e_local_para_unreachable,s);
  813. opr.localsym:=tvarsym(sym);
  814. opr.localsymofs:=0;
  815. opr.localindexreg:=indexreg;
  816. opr.localscale:=0;
  817. opr.localgetoffset:=GetOffset;
  818. end;
  819. if paramanager.push_addr_param(tvarsym(sym).varspez,tvarsym(sym).vartype.def,current_procinfo.procdef.proccalloption) then
  820. SetSize(pointer_size,false);
  821. end;
  822. end;
  823. case tvarsym(sym).vartype.def.deftype of
  824. orddef,
  825. enumdef,
  826. pointerdef,
  827. floatdef :
  828. SetSize(tvarsym(sym).getsize,false);
  829. arraydef :
  830. begin
  831. { for arrays try to get the element size, take care of
  832. multiple indexes }
  833. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  834. while assigned(harrdef.elementtype.def) and
  835. (harrdef.elementtype.def.deftype=arraydef) do
  836. harrdef:=tarraydef(harrdef.elementtype.def);
  837. SetSize(harrdef.elesize,false);
  838. end;
  839. end;
  840. hasvar:=true;
  841. SetupVar:=true;
  842. Exit;
  843. end;
  844. typedconstsym :
  845. begin
  846. initref;
  847. opr.ref.symbol:=objectlibrary.newasmsymbol(ttypedconstsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  848. case ttypedconstsym(sym).typedconsttype.def.deftype of
  849. orddef,
  850. enumdef,
  851. pointerdef,
  852. floatdef :
  853. SetSize(ttypedconstsym(sym).getsize,false);
  854. arraydef :
  855. begin
  856. { for arrays try to get the element size, take care of
  857. multiple indexes }
  858. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  859. while assigned(harrdef.elementtype.def) and
  860. (harrdef.elementtype.def.deftype=arraydef) do
  861. harrdef:=tarraydef(harrdef.elementtype.def);
  862. SetSize(harrdef.elesize,false);
  863. end;
  864. end;
  865. hasvar:=true;
  866. SetupVar:=true;
  867. Exit;
  868. end;
  869. constsym :
  870. begin
  871. if tconstsym(sym).consttyp=constord then
  872. begin
  873. setconst(tconstsym(sym).value.valueord);
  874. SetupVar:=true;
  875. Exit;
  876. end;
  877. end;
  878. typesym :
  879. begin
  880. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  881. begin
  882. setconst(0);
  883. SetupVar:=TRUE;
  884. Exit;
  885. end;
  886. end;
  887. procsym :
  888. begin
  889. if opr.typ<>OPR_NONE then
  890. Message(asmr_e_invalid_operand_type);
  891. if Tprocsym(sym).procdef_count>1 then
  892. Message(asmr_w_calling_overload_func);
  893. l:=opr.ref.offset;
  894. opr.typ:=OPR_SYMBOL;
  895. opr.symbol:=objectlibrary.newasmsymbol(tprocsym(sym).first_procdef.mangledname,AB_EXTERNAL,AT_FUNCTION);
  896. opr.symofs:=l;
  897. hasvar:=true;
  898. SetupVar:=TRUE;
  899. Exit;
  900. end;
  901. else
  902. begin
  903. Message(asmr_e_unsupported_symbol_type);
  904. exit;
  905. end;
  906. end;
  907. end;
  908. { looks for internal names of variables and routines }
  909. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  910. var
  911. p : tasmsymbol;
  912. begin
  913. SetupDirectVar:=false;
  914. p:=objectlibrary.getasmsymbol(hs);
  915. if assigned(p) then
  916. begin
  917. InitRef;
  918. opr.ref.symbol:=p;
  919. hasvar:=true;
  920. SetupDirectVar:=true;
  921. end;
  922. end;
  923. procedure TOperand.InitRef;
  924. {*********************************************************************}
  925. { Description: This routine first check if the opcode is of }
  926. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  927. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  928. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  929. { to point to the default segment. }
  930. {*********************************************************************}
  931. var
  932. l : longint;
  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. end;
  944. OPR_NONE :
  945. begin
  946. opr.typ:=OPR_REFERENCE;
  947. Fillchar(opr.ref,sizeof(treference),0);
  948. end;
  949. else
  950. begin
  951. Message(asmr_e_invalid_operand_type);
  952. { Recover }
  953. opr.typ:=OPR_REFERENCE;
  954. Fillchar(opr.ref,sizeof(treference),0);
  955. end;
  956. end;
  957. end;
  958. {****************************************************************************
  959. TInstruction
  960. ****************************************************************************}
  961. constructor TInstruction.create(optype : tcoperand);
  962. var
  963. i : longint;
  964. Begin
  965. { these field are set to 0 anyways by the constructor helper (FK)
  966. Opcode:=A_NONE;
  967. Condition:=C_NONE;
  968. Ops:=0;
  969. }
  970. for i:=1 to max_operands do
  971. Operands[i]:=optype.create;
  972. Labeled:=false;
  973. end;
  974. destructor TInstruction.destroy;
  975. var
  976. i : longint;
  977. Begin
  978. for i:=1 to max_operands do
  979. Operands[i].free;
  980. end;
  981. Procedure TInstruction.Swapoperands;
  982. Var
  983. p : toperand;
  984. Begin
  985. case Ops of
  986. 2 :
  987. begin
  988. p:=Operands[1];
  989. Operands[1]:=Operands[2];
  990. Operands[2]:=p;
  991. end;
  992. 3 :
  993. begin
  994. p:=Operands[1];
  995. Operands[1]:=Operands[3];
  996. Operands[3]:=p;
  997. end;
  998. end;
  999. end;
  1000. function TInstruction.ConcatInstruction(p:TAAsmoutput) : tai;
  1001. var
  1002. ai : taicpu;
  1003. i : longint;
  1004. begin
  1005. ai:=taicpu.op_none(opcode);
  1006. ai.Ops:=Ops;
  1007. ai.Allocate_oper(Ops);
  1008. for i:=1 to Ops do
  1009. with operands[i].opr do
  1010. begin
  1011. case typ of
  1012. OPR_CONSTANT :
  1013. ai.loadconst(i-1,aword(val));
  1014. OPR_REGISTER:
  1015. ai.loadreg(i-1,reg);
  1016. OPR_SYMBOL:
  1017. ai.loadsymbol(i-1,symbol,symofs);
  1018. OPR_LOCAL :
  1019. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1020. localscale,localgetoffset);
  1021. OPR_REFERENCE:
  1022. ai.loadref(i-1,ref);
  1023. {$ifdef ARM}
  1024. OPR_REGSET:
  1025. ai.loadregset(i-1,regset);
  1026. {$endif ARM}
  1027. end;
  1028. end;
  1029. ai.SetCondition(condition);
  1030. { Concat the opcode or give an error }
  1031. if assigned(ai) then
  1032. p.concat(ai)
  1033. else
  1034. Message(asmr_e_invalid_opcode_and_operand);
  1035. result:=ai;
  1036. end;
  1037. {***************************************************************************
  1038. TLocalLabel
  1039. ***************************************************************************}
  1040. constructor TLocalLabel.create(const n:string);
  1041. begin
  1042. inherited CreateName(n);
  1043. lab:=nil;
  1044. emitted:=false;
  1045. end;
  1046. function TLocalLabel.Gettasmlabel:tasmlabel;
  1047. begin
  1048. if not assigned(lab) then
  1049. begin
  1050. objectlibrary.getlabel(lab);
  1051. { this label is forced to be used so it's always written }
  1052. lab.increfs;
  1053. end;
  1054. Gettasmlabel:=lab;
  1055. end;
  1056. {***************************************************************************
  1057. TLocalLabelList
  1058. ***************************************************************************}
  1059. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  1060. begin
  1061. if not TLocalLabel(p).emitted then
  1062. Message1(asmr_e_unknown_label_identifier,p.name);
  1063. end;
  1064. procedure TLocalLabelList.CheckEmitted;
  1065. begin
  1066. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted,nil)
  1067. end;
  1068. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1069. var
  1070. lab : TLocalLabel;
  1071. Begin
  1072. CreateLocalLabel:=true;
  1073. { Check if it already is defined }
  1074. lab:=TLocalLabel(LocalLabellist.Search(s));
  1075. if not assigned(lab) then
  1076. begin
  1077. lab:=TLocalLabel.Create(s);
  1078. LocalLabellist.Insert(lab);
  1079. end;
  1080. { set emitted flag and check for dup syms }
  1081. if emit then
  1082. begin
  1083. if lab.Emitted then
  1084. begin
  1085. Message1(asmr_e_dup_local_sym,lab.Name);
  1086. CreateLocalLabel:=false;
  1087. end;
  1088. lab.Emitted:=true;
  1089. end;
  1090. hl:=lab.Gettasmlabel;
  1091. end;
  1092. {****************************************************************************
  1093. Symbol table helper routines
  1094. ****************************************************************************}
  1095. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1096. var
  1097. i : integer;
  1098. begin
  1099. i:=pos('.',s);
  1100. { allow unit.identifier }
  1101. if i>0 then
  1102. begin
  1103. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1104. if assigned(srsym) then
  1105. begin
  1106. if (srsym.typ=unitsym) and
  1107. (srsym.owner.unitid=0) then
  1108. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1109. else
  1110. srsym:=nil;
  1111. end;
  1112. end
  1113. else
  1114. searchsym(s,srsym,srsymtable);
  1115. end;
  1116. Function SearchType(const hs:string;var size:longint): Boolean;
  1117. var
  1118. srsym : tsym;
  1119. srsymtable : tsymtable;
  1120. begin
  1121. result:=false;
  1122. size:=0;
  1123. asmsearchsym(hs,srsym,srsymtable);
  1124. if assigned(srsym) and
  1125. (srsym.typ=typesym) then
  1126. begin
  1127. size:=ttypesym(srsym).restype.def.size;
  1128. result:=true;
  1129. end;
  1130. end;
  1131. Function SearchRecordType(const s:string): boolean;
  1132. var
  1133. srsym : tsym;
  1134. srsymtable : tsymtable;
  1135. Begin
  1136. SearchRecordType:=false;
  1137. { Check the constants in symtable }
  1138. asmsearchsym(s,srsym,srsymtable);
  1139. if srsym <> nil then
  1140. Begin
  1141. case srsym.typ of
  1142. typesym :
  1143. begin
  1144. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1145. begin
  1146. SearchRecordType:=true;
  1147. exit;
  1148. end;
  1149. end;
  1150. end;
  1151. end;
  1152. end;
  1153. Function SearchIConstant(const s:string; var l:longint): boolean;
  1154. {**********************************************************************}
  1155. { Description: Searches for a CONSTANT of name s in either the local }
  1156. { symbol list, then in the global symbol list, and returns the value }
  1157. { of that constant in l. Returns TRUE if successfull, if not found, }
  1158. { or if the constant is not of correct type, then returns FALSE }
  1159. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1160. { respectively. }
  1161. {**********************************************************************}
  1162. var
  1163. srsym : tsym;
  1164. srsymtable : tsymtable;
  1165. Begin
  1166. SearchIConstant:=false;
  1167. { check for TRUE or FALSE reserved words first }
  1168. if s = 'TRUE' then
  1169. Begin
  1170. SearchIConstant:=TRUE;
  1171. l:=1;
  1172. exit;
  1173. end;
  1174. if s = 'FALSE' then
  1175. Begin
  1176. SearchIConstant:=TRUE;
  1177. l:=0;
  1178. exit;
  1179. end;
  1180. { Check the constants in symtable }
  1181. asmsearchsym(s,srsym,srsymtable);
  1182. if srsym <> nil then
  1183. Begin
  1184. case srsym.typ of
  1185. constsym :
  1186. begin
  1187. if tconstsym(srsym).consttyp=constord then
  1188. Begin
  1189. l:=tconstsym(srsym).value.valueord;
  1190. SearchIConstant:=TRUE;
  1191. exit;
  1192. end;
  1193. end;
  1194. enumsym:
  1195. Begin
  1196. l:=tenumsym(srsym).value;
  1197. SearchIConstant:=TRUE;
  1198. exit;
  1199. end;
  1200. end;
  1201. end;
  1202. end;
  1203. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1204. { search and returns the offset and size of records/objects of the base }
  1205. { with field name setup in field. }
  1206. { returns FALSE if not found. }
  1207. { used when base is a variable or a typed constant name. }
  1208. var
  1209. st : tsymtable;
  1210. harrdef : tarraydef;
  1211. sym : tsym;
  1212. srsymtable : tsymtable;
  1213. i : longint;
  1214. base : string;
  1215. Begin
  1216. GetRecordOffsetSize:=FALSE;
  1217. Offset:=0;
  1218. Size:=0;
  1219. i:=pos('.',s);
  1220. if i=0 then
  1221. i:=255;
  1222. base:=Copy(s,1,i-1);
  1223. delete(s,1,i);
  1224. if base='SELF' then
  1225. st:=current_procinfo.procdef._class.symtable
  1226. else
  1227. begin
  1228. asmsearchsym(base,sym,srsymtable);
  1229. st:=nil;
  1230. { we can start with a var,type,typedconst }
  1231. case sym.typ of
  1232. varsym :
  1233. with Tvarsym(sym).vartype do
  1234. case def.deftype of
  1235. recorddef :
  1236. st:=trecorddef(def).symtable;
  1237. objectdef :
  1238. st:=tobjectdef(def).symtable;
  1239. end;
  1240. typesym :
  1241. with Ttypesym(sym).restype do
  1242. case def.deftype of
  1243. recorddef :
  1244. st:=trecorddef(def).symtable;
  1245. objectdef :
  1246. st:=tobjectdef(def).symtable;
  1247. end;
  1248. typedconstsym :
  1249. with Ttypedconstsym(sym).typedconsttype do
  1250. case def.deftype of
  1251. recorddef :
  1252. st:=trecorddef(def).symtable;
  1253. objectdef :
  1254. st:=tobjectdef(def).symtable;
  1255. end;
  1256. end;
  1257. end;
  1258. { now walk all recordsymtables }
  1259. while assigned(st) and (s<>'') do
  1260. begin
  1261. { load next field in base }
  1262. i:=pos('.',s);
  1263. if i=0 then
  1264. i:=255;
  1265. base:=Copy(s,1,i-1);
  1266. delete(s,1,i);
  1267. if st.symtabletype=objectsymtable then
  1268. sym:=search_class_member(tobjectdef(st.defowner),base)
  1269. else
  1270. sym:=tsym(st.search(base));
  1271. if not assigned(sym) then
  1272. begin
  1273. GetRecordOffsetSize:=false;
  1274. exit;
  1275. end;
  1276. st:=nil;
  1277. case sym.typ of
  1278. varsym :
  1279. with Tvarsym(sym) do
  1280. begin
  1281. inc(Offset,fieldoffset);
  1282. size:=getsize;
  1283. with vartype do
  1284. case def.deftype of
  1285. arraydef :
  1286. begin
  1287. { for arrays try to get the element size, take care of
  1288. multiple indexes }
  1289. harrdef:=tarraydef(def);
  1290. while assigned(harrdef.elementtype.def) and
  1291. (harrdef.elementtype.def.deftype=arraydef) do
  1292. harrdef:=tarraydef(harrdef.elementtype.def);
  1293. size:=harrdef.elesize;
  1294. end;
  1295. recorddef :
  1296. st:=trecorddef(def).symtable;
  1297. objectdef :
  1298. st:=tobjectdef(def).symtable;
  1299. end;
  1300. end;
  1301. end;
  1302. end;
  1303. GetRecordOffsetSize:=(s='');
  1304. end;
  1305. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1306. var
  1307. sym : tsym;
  1308. srsymtable : tsymtable;
  1309. hs : string;
  1310. Begin
  1311. hl:=nil;
  1312. SearchLabel:=false;
  1313. { Check for pascal labels, which are case insensetive }
  1314. hs:=upper(s);
  1315. asmsearchsym(hs,sym,srsymtable);
  1316. if sym=nil then
  1317. exit;
  1318. case sym.typ of
  1319. labelsym :
  1320. begin
  1321. hl:=tlabelsym(sym).lab;
  1322. if emit then
  1323. tlabelsym(sym).defined:=true
  1324. else
  1325. tlabelsym(sym).used:=true;
  1326. SearchLabel:=true;
  1327. end;
  1328. end;
  1329. end;
  1330. {*************************************************************************}
  1331. { Instruction Generation Utilities }
  1332. {*************************************************************************}
  1333. Procedure ConcatString(p : TAAsmoutput;s:string);
  1334. {*********************************************************************}
  1335. { PROCEDURE ConcatString(s:string); }
  1336. { Description: This routine adds the character chain pointed to in }
  1337. { s to the instruction linked list. }
  1338. {*********************************************************************}
  1339. Var
  1340. pc: PChar;
  1341. Begin
  1342. getmem(pc,length(s)+1);
  1343. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1344. end;
  1345. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1346. {*********************************************************************}
  1347. { PROCEDURE ConcatPasString(s:string); }
  1348. { Description: This routine adds the character chain pointed to in }
  1349. { s to the instruction linked list, contrary to ConcatString it }
  1350. { uses a pascal style string, so it conserves null characters. }
  1351. {*********************************************************************}
  1352. Begin
  1353. p.concat(Tai_string.Create(s));
  1354. end;
  1355. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1356. {*********************************************************************}
  1357. { PROCEDURE ConcatDirect(s:string) }
  1358. { Description: This routine output the string directly to the asm }
  1359. { output, it is only sed when writing special labels in AT&T mode, }
  1360. { and should not be used without due consideration, since it may }
  1361. { cause problems. }
  1362. {*********************************************************************}
  1363. Var
  1364. pc: PChar;
  1365. Begin
  1366. getmem(pc,length(s)+1);
  1367. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1368. end;
  1369. Procedure ConcatConstant(p: TAAsmoutput; value: longint; maxvalue: longint);
  1370. {*********************************************************************}
  1371. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1372. { Description: This routine adds the value constant to the current }
  1373. { instruction linked list. }
  1374. { maxvalue -> indicates the size of the data to initialize: }
  1375. { $ff -> create a byte node. }
  1376. { $ffff -> create a word node. }
  1377. { $ffffffff -> create a dword node. }
  1378. {*********************************************************************}
  1379. Begin
  1380. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1381. Begin
  1382. Message(asmr_e_constant_out_of_bounds);
  1383. { assuming a value of maxvalue }
  1384. value:=maxvalue;
  1385. end;
  1386. if maxvalue = $ff then
  1387. p.concat(Tai_const.Create_8bit(byte(value)))
  1388. else
  1389. if maxvalue = $ffff then
  1390. p.concat(Tai_const.Create_16bit(word(value)))
  1391. else
  1392. if maxvalue = longint($ffffffff) then
  1393. p.concat(Tai_const.Create_32bit(longint(value)));
  1394. end;
  1395. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  1396. begin
  1397. p.concat(Tai_const_symbol.Createname(sym,AT_DATA,l));
  1398. end;
  1399. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1400. {***********************************************************************}
  1401. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1402. { Description: This routine adds the value constant to the current }
  1403. { instruction linked list. }
  1404. { real_typ -> indicates the type of the real data to initialize: }
  1405. { s32real -> create a single node. }
  1406. { s64real -> create a double node. }
  1407. { s80real -> create an extended node. }
  1408. { s64bit -> create a comp node. }
  1409. { f32bit -> create a fixed node. (not used normally) }
  1410. {***********************************************************************}
  1411. Begin
  1412. case real_typ of
  1413. s32real : p.concat(Tai_real_32bit.Create(value));
  1414. s64real :
  1415. {$ifdef ARM}
  1416. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  1417. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1418. else
  1419. {$endif ARM}
  1420. p.concat(Tai_real_64bit.Create(value));
  1421. s80real : p.concat(Tai_real_80bit.Create(value));
  1422. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1423. end;
  1424. end;
  1425. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1426. {*********************************************************************}
  1427. { PROCEDURE ConcatLabel }
  1428. { Description: This routine either emits a label or a labeled }
  1429. { instruction to the linked list of instructions. }
  1430. {*********************************************************************}
  1431. begin
  1432. p.concat(Tai_label.Create(l));
  1433. end;
  1434. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  1435. {*********************************************************************}
  1436. { PROCEDURE ConcatPublic }
  1437. { Description: This routine emits an global definition to the }
  1438. { linked list of instructions.(used by AT&T styled asm) }
  1439. {*********************************************************************}
  1440. begin
  1441. p.concat(Tai_align.Create(l));
  1442. end;
  1443. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1444. {*********************************************************************}
  1445. { PROCEDURE ConcatPublic }
  1446. { Description: This routine emits an global definition to the }
  1447. { linked list of instructions.(used by AT&T styled asm) }
  1448. {*********************************************************************}
  1449. begin
  1450. p.concat(Tai_symbol.Createname_global(s,AT_FUNCTION,0));
  1451. end;
  1452. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1453. {*********************************************************************}
  1454. { PROCEDURE ConcatLocal }
  1455. { Description: This routine emits an local definition to the }
  1456. { linked list of instructions. }
  1457. {*********************************************************************}
  1458. begin
  1459. p.concat(Tai_symbol.Createname(s,AT_FUNCTION,0));
  1460. end;
  1461. Procedure ConcatGlobalBss(const s : string;size : longint);
  1462. {*********************************************************************}
  1463. { PROCEDURE ConcatGlobalBss }
  1464. { Description: This routine emits an global datablock to the }
  1465. { linked list of instructions. }
  1466. {*********************************************************************}
  1467. begin
  1468. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1469. end;
  1470. Procedure ConcatLocalBss(const s : string;size : longint);
  1471. {*********************************************************************}
  1472. { PROCEDURE ConcatLocalBss }
  1473. { Description: This routine emits a local datablcok to the }
  1474. { linked list of instructions. }
  1475. {*********************************************************************}
  1476. begin
  1477. bssSegment.concat(Tai_datablock.Create(s,size));
  1478. end;
  1479. end.
  1480. {
  1481. $Log$
  1482. Revision 1.85 2004-03-23 22:34:49 peter
  1483. * constants ordinals now always have a type assigned
  1484. * integer constants have the smallest type, unsigned prefered over
  1485. signed
  1486. Revision 1.84 2004/03/18 11:43:57 olle
  1487. * change AT_FUNCTION to AT_DATA where appropriate
  1488. Revision 1.83 2004/03/17 22:27:41 florian
  1489. * fixed handling of doubles in a native arm compiler
  1490. * fixed handling of typed double constants on arm
  1491. Revision 1.82 2004/03/02 00:36:33 olle
  1492. * big transformation of Tai_[const_]Symbol.Create[data]name*
  1493. Revision 1.81 2004/02/21 21:04:09 daniel
  1494. * Micro-optimizations
  1495. Revision 1.80 2003/11/30 10:15:42 jonas
  1496. * fixed compilation for non-x86
  1497. Revision 1.79 2003/11/21 16:29:26 florian
  1498. * fixed reading of reg. sets in the arm assembler reader
  1499. Revision 1.78 2003/11/17 23:23:47 florian
  1500. + first part of arm assembler reader
  1501. Revision 1.77 2003/11/12 16:05:39 florian
  1502. * assembler readers OOPed
  1503. + typed currency constants
  1504. + typed 128 bit float constants if the CPU supports it
  1505. Revision 1.76 2003/10/30 19:59:00 peter
  1506. * support scalefactor for opr_local
  1507. * support reference with opr_local set, fixes tw2631
  1508. Revision 1.75 2003/10/29 16:47:18 peter
  1509. * fix field offset in reference
  1510. Revision 1.74 2003/10/29 15:40:20 peter
  1511. * support indexing and offset retrieval for locals
  1512. Revision 1.73 2003/10/28 15:36:01 peter
  1513. * absolute to object field supported, fixes tb0458
  1514. Revision 1.72 2003/10/24 17:39:03 peter
  1515. * more intel parser updates
  1516. Revision 1.71 2003/10/23 17:19:11 peter
  1517. * SearchType returns also the size
  1518. Revision 1.70 2003/10/08 19:39:58 peter
  1519. * allow access to parent locals when the currnet localst has no
  1520. varsyms
  1521. Revision 1.69 2003/10/01 20:34:49 peter
  1522. * procinfo unit contains tprocinfo
  1523. * cginfo renamed to cgbase
  1524. * moved cgmessage to verbose
  1525. * fixed ppc and sparc compiles
  1526. Revision 1.68 2003/09/25 14:57:36 peter
  1527. * fix check for unreachable locals
  1528. Revision 1.67 2003/09/23 17:56:06 peter
  1529. * locals and paras are allocated in the code generation
  1530. * tvarsym.localloc contains the location of para/local when
  1531. generating code for the current procedure
  1532. Revision 1.66 2003/09/16 16:17:01 peter
  1533. * varspez in calls to push_addr_param
  1534. Revision 1.65 2003/09/03 15:55:01 peter
  1535. * NEWRA branch merged
  1536. Revision 1.64.2.1 2003/08/27 19:55:54 peter
  1537. * first tregister patch
  1538. Revision 1.64 2003/06/13 21:19:31 peter
  1539. * current_procdef removed, use current_procinfo.procdef instead
  1540. Revision 1.63 2003/06/06 14:43:29 peter
  1541. * absolutesym support
  1542. Revision 1.62 2003/05/30 23:57:08 peter
  1543. * more sparc cleanup
  1544. * accumulator removed, splitted in function_return_reg (called) and
  1545. function_result_reg (caller)
  1546. Revision 1.61 2003/05/25 08:55:49 peter
  1547. * load result using hidden parameter
  1548. Revision 1.60 2003/05/15 18:58:53 peter
  1549. * removed selfpointer_offset, vmtpointer_offset
  1550. * tvarsym.adjusted_address
  1551. * address in localsymtable is now in the real direction
  1552. * removed some obsolete globals
  1553. Revision 1.59 2003/05/12 17:22:00 jonas
  1554. * fixed (last?) remaining -tvarsym(X).address to
  1555. tg.direction*tvarsym(X).address...
  1556. Revision 1.58 2003/04/27 11:21:34 peter
  1557. * aktprocdef renamed to current_procinfo.procdef
  1558. * procinfo renamed to current_procinfo
  1559. * procinfo will now be stored in current_module so it can be
  1560. cleaned up properly
  1561. * gen_main_procsym changed to create_main_proc and release_main_proc
  1562. to also generate a tprocinfo structure
  1563. * fixed unit implicit initfinal
  1564. Revision 1.57 2003/04/27 07:29:51 peter
  1565. * current_procinfo.procdef cleanup, current_procinfo.procdef is now always nil when parsing
  1566. a new procdef declaration
  1567. * aktprocsym removed
  1568. * lexlevel removed, use symtable.symtablelevel instead
  1569. * implicit init/final code uses the normal genentry/genexit
  1570. * funcret state checking updated for new funcret handling
  1571. Revision 1.56 2003/04/25 20:59:34 peter
  1572. * removed funcretn,funcretsym, function result is now in varsym
  1573. and aliases for result and function name are added using absolutesym
  1574. * vs_hidden parameter for funcret passed in parameter
  1575. * vs_hidden fixes
  1576. * writenode changed to printnode and released from extdebug
  1577. * -vp option added to generate a tree.log with the nodetree
  1578. * nicer printnode for statements, callnode
  1579. Revision 1.55 2003/04/06 21:11:23 olle
  1580. * changed newasmsymbol to newasmsymboldata for data symbols
  1581. Revision 1.54 2003/03/28 19:16:57 peter
  1582. * generic constructor working for i386
  1583. * remove fixed self register
  1584. * esi added as address register for i386
  1585. Revision 1.53 2003/02/19 22:00:14 daniel
  1586. * Code generator converted to new register notation
  1587. - Horribily outdated todo.txt removed
  1588. Revision 1.52 2003/01/08 18:43:56 daniel
  1589. * Tregister changed into a record
  1590. Revision 1.51 2002/12/14 15:02:03 carl
  1591. * maxoperands -> max_operands (for portability in rautils.pas)
  1592. * fix some range-check errors with loadconst
  1593. + add ncgadd unit to m68k
  1594. * some bugfix of a_param_reg with LOC_CREFERENCE
  1595. Revision 1.50 2002/11/25 17:43:23 peter
  1596. * splitted defbase in defutil,symutil,defcmp
  1597. * merged isconvertable and is_equal into compare_defs(_ext)
  1598. * made operator search faster by walking the list only once
  1599. Revision 1.49 2002/11/22 22:48:10 carl
  1600. * memory optimization with tconstsym (1.5%)
  1601. Revision 1.48 2002/11/18 17:31:59 peter
  1602. * pass proccalloption to ret_in_xxx and push_xxx functions
  1603. Revision 1.47 2002/11/15 16:29:31 peter
  1604. * made tasmsymbol.refs private (merged)
  1605. Revision 1.46 2002/09/03 16:26:27 daniel
  1606. * Make Tprocdef.defs protected
  1607. Revision 1.45 2002/08/25 19:25:20 peter
  1608. * sym.insert_in_data removed
  1609. * symtable.insertvardata/insertconstdata added
  1610. * removed insert_in_data call from symtable.insert, it needs to be
  1611. called separatly. This allows to deref the address calculation
  1612. * procedures now calculate the parast addresses after the procedure
  1613. directives are parsed. This fixes the cdecl parast problem
  1614. * push_addr_param has an extra argument that specifies if cdecl is used
  1615. or not
  1616. Revision 1.44 2002/08/17 09:23:41 florian
  1617. * first part of procinfo rewrite
  1618. Revision 1.43 2002/08/16 14:24:59 carl
  1619. * issameref() to test if two references are the same (then emit no opcodes)
  1620. + ret_in_reg to replace ret_in_acc
  1621. (fix some register allocation bugs at the same time)
  1622. + save_std_register now has an extra parameter which is the
  1623. usedinproc registers
  1624. Revision 1.42 2002/08/13 18:01:52 carl
  1625. * rename swatoperands to swapoperands
  1626. + m68k first compilable version (still needs a lot of testing):
  1627. assembler generator, system information , inline
  1628. assembler reader.
  1629. Revision 1.41 2002/08/12 15:08:40 carl
  1630. + stab register indexes for powerpc (moved from gdb to cpubase)
  1631. + tprocessor enumeration moved to cpuinfo
  1632. + linker in target_info is now a class
  1633. * many many updates for m68k (will soon start to compile)
  1634. - removed some ifdef or correct them for correct cpu
  1635. Revision 1.40 2002/08/11 14:32:27 peter
  1636. * renamed current_library to objectlibrary
  1637. Revision 1.39 2002/08/11 13:24:13 peter
  1638. * saving of asmsymbols in ppu supported
  1639. * asmsymbollist global is removed and moved into a new class
  1640. tasmlibrarydata that will hold the info of a .a file which
  1641. corresponds with a single module. Added librarydata to tmodule
  1642. to keep the library info stored for the module. In the future the
  1643. objectfiles will also be stored to the tasmlibrarydata class
  1644. * all getlabel/newasmsymbol and friends are moved to the new class
  1645. Revision 1.38 2002/07/20 11:57:57 florian
  1646. * types.pas renamed to defbase.pas because D6 contains a types
  1647. unit so this would conflicts if D6 programms are compiled
  1648. + Willamette/SSE2 instructions to assembler added
  1649. Revision 1.37 2002/07/11 14:41:28 florian
  1650. * start of the new generic parameter handling
  1651. Revision 1.36 2002/07/01 18:46:25 peter
  1652. * internal linker
  1653. * reorganized aasm layer
  1654. Revision 1.35 2002/05/18 13:34:17 peter
  1655. * readded missing revisions
  1656. Revision 1.34 2002/05/16 19:46:44 carl
  1657. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1658. + try to fix temp allocation (still in ifdef)
  1659. + generic constructor calls
  1660. + start of tassembler / tmodulebase class cleanup
  1661. Revision 1.31 2002/05/12 16:53:10 peter
  1662. * moved entry and exitcode to ncgutil and cgobj
  1663. * foreach gets extra argument for passing local data to the
  1664. iterator function
  1665. * -CR checks also class typecasts at runtime by changing them
  1666. into as
  1667. * fixed compiler to cycle with the -CR option
  1668. * fixed stabs with elf writer, finally the global variables can
  1669. be watched
  1670. * removed a lot of routines from cga unit and replaced them by
  1671. calls to cgobj
  1672. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1673. u32bit then the other is typecasted also to u32bit without giving
  1674. a rangecheck warning/error.
  1675. * fixed pascal calling method with reversing also the high tree in
  1676. the parast, detected by tcalcst3 test
  1677. Revision 1.30 2002/04/20 21:32:24 carl
  1678. + generic FPC_CHECKPOINTER
  1679. + first parameter offset in stack now portable
  1680. * rename some constants
  1681. + move some cpu stuff to other units
  1682. - remove unused constents
  1683. * fix stacksize for some targets
  1684. * fix generic size problems which depend now on EXTEND_SIZE constant
  1685. Revision 1.29 2002/04/15 19:02:35 carl
  1686. + target_info.size_of_pointer -> pointer_Size
  1687. Revision 1.28 2002/04/02 17:11:29 peter
  1688. * tlocation,treference update
  1689. * LOC_CONSTANT added for better constant handling
  1690. * secondadd splitted in multiple routines
  1691. * location_force_reg added for loading a location to a register
  1692. of a specified size
  1693. * secondassignment parses now first the right and then the left node
  1694. (this is compatible with Kylix). This saves a lot of push/pop especially
  1695. with string operations
  1696. * adapted some routines to use the new cg methods
  1697. Revision 1.27 2002/01/29 21:32:03 peter
  1698. * allow accessing locals in other lexlevel when the current assembler
  1699. routine doesn't have locals.
  1700. Revision 1.26 2002/01/24 18:25:50 peter
  1701. * implicit result variable generation for assembler routines
  1702. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1703. }