rautils.pas 53 KB

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