c_ast.g 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. # This file is part of asmc, a bootstrapping OS with minimal seed
  2. # Copyright (C) 2018-2019 Giovanni Mascellani <gio@debian.org>
  3. # https://gitlab.com/giomasce/asmc
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. const ASTINT_GET_TOKEN 0
  15. const ASTINT_GET_TOKEN_OR_FAIL 4
  16. const ASTINT_GIVE_BACK_TOKEN 8
  17. const ASTINT_PARSE_TYPE 12
  18. const AST_TYPE 0 # 0 for operand, 1 for operator
  19. const AST_NAME 4 # char*
  20. const AST_LEFT 8 # AST*
  21. const AST_CENTER 12 # AST*
  22. const AST_RIGHT 16 # AST*
  23. const AST_TYPE_IDX 20 # int
  24. const AST_ORIG_TYPE_IDX 24 # int
  25. const AST_CAST_TYPE_IDX 28 # int
  26. const AST_COMMON_TYPE_IDX 32 # int
  27. const AST_VALUE 36 # int64*
  28. const SIZEOF_AST 40
  29. fun ast_init 0 {
  30. $ptr
  31. @ptr SIZEOF_AST malloc = ;
  32. ptr AST_TYPE take_addr 0 = ;
  33. ptr AST_NAME take_addr 0 = ;
  34. ptr AST_LEFT take_addr 0 = ;
  35. ptr AST_CENTER take_addr 0 = ;
  36. ptr AST_RIGHT take_addr 0 = ;
  37. ptr AST_TYPE_IDX take_addr 0xffffffff = ;
  38. ptr AST_ORIG_TYPE_IDX take_addr 0xffffffff = ;
  39. ptr AST_CAST_TYPE_IDX take_addr 0xffffffff = ;
  40. ptr AST_COMMON_TYPE_IDX take_addr 0xffffffff = ;
  41. ptr AST_VALUE take_addr 0 = ;
  42. ptr ret ;
  43. }
  44. fun ast_destroy 1 {
  45. $ptr
  46. @ptr 0 param = ;
  47. ptr AST_NAME take free ;
  48. if ptr AST_LEFT take {
  49. ptr AST_LEFT take ast_destroy ;
  50. }
  51. if ptr AST_CENTER take {
  52. ptr AST_CENTER take ast_destroy ;
  53. }
  54. if ptr AST_RIGHT take {
  55. ptr AST_RIGHT take ast_destroy ;
  56. }
  57. if ptr AST_VALUE take {
  58. ptr AST_VALUE take i64_destroy ;
  59. }
  60. ptr free ;
  61. }
  62. fun ast_is_operator 1 {
  63. $str
  64. @str 0 param = ;
  65. str "++" strcmp 0 ==
  66. str "--" strcmp 0 == ||
  67. str "." strcmp 0 == ||
  68. str "->" strcmp 0 == ||
  69. str "defined" strcmp 0 == ||
  70. str "+" strcmp 0 == ||
  71. str "-" strcmp 0 == ||
  72. str "!" strcmp 0 == ||
  73. str "~" strcmp 0 == ||
  74. str "*" strcmp 0 == ||
  75. str "&" strcmp 0 == ||
  76. str "sizeof" strcmp 0 == ||
  77. str "/" strcmp 0 == ||
  78. str "%" strcmp 0 == ||
  79. str "<<" strcmp 0 == ||
  80. str ">>" strcmp 0 == ||
  81. str "<" strcmp 0 == ||
  82. str "<=" strcmp 0 == ||
  83. str ">" strcmp 0 == ||
  84. str ">=" strcmp 0 == ||
  85. str "==" strcmp 0 == ||
  86. str "!=" strcmp 0 == ||
  87. str "^" strcmp 0 == ||
  88. str "|" strcmp 0 == ||
  89. str "&&" strcmp 0 == ||
  90. str "||" strcmp 0 == ||
  91. str "?" strcmp 0 == ||
  92. str "=" strcmp 0 == ||
  93. str "+=" strcmp 0 == ||
  94. str "-=" strcmp 0 == ||
  95. str "*=" strcmp 0 == ||
  96. str "/=" strcmp 0 == ||
  97. str "%=" strcmp 0 == ||
  98. str "<<=" strcmp 0 == ||
  99. str ">>=" strcmp 0 == ||
  100. str "&=" strcmp 0 == ||
  101. str "^=" strcmp 0 == ||
  102. str "|=" strcmp 0 == ||
  103. str "," strcmp 0 == ||
  104. ret ;
  105. }
  106. # See http://en.cppreference.com/w/c/language/operator_precedence
  107. fun ast_get_priority 1 {
  108. $str
  109. @str 0 param = ;
  110. if str "++_POST" strcmp 0 == { 1 ret ; }
  111. if str "--_POST" strcmp 0 == { 1 ret ; }
  112. if str "(" strcmp 0 == { 1 ret ; }
  113. if str "[" strcmp 0 == { 1 ret ; }
  114. if str "." strcmp 0 == { 1 ret ; }
  115. if str "->" strcmp 0 == { 1 ret ; }
  116. if str "defined_PRE" strcmp 0 == { 2 ret ; }
  117. if str "++_PRE" strcmp 0 == { 2 ret ; }
  118. if str "--_PRE" strcmp 0 == { 2 ret ; }
  119. if str "+_PRE" strcmp 0 == { 2 ret ; }
  120. if str "-_PRE" strcmp 0 == { 2 ret ; }
  121. if str "!_PRE" strcmp 0 == { 2 ret ; }
  122. if str "~_PRE" strcmp 0 == { 2 ret ; }
  123. if str "(_PRE" strcmp 0 == { 2 ret ; }
  124. if str "*_PRE" strcmp 0 == { 2 ret ; }
  125. if str "&_PRE" strcmp 0 == { 2 ret ; }
  126. if str "sizeof_PRE" strcmp 0 == { 2 ret ; }
  127. if str "*" strcmp 0 == { 3 ret ; }
  128. if str "/" strcmp 0 == { 3 ret ; }
  129. if str "%" strcmp 0 == { 3 ret ; }
  130. if str "+" strcmp 0 == { 4 ret ; }
  131. if str "-" strcmp 0 == { 4 ret ; }
  132. if str "<<" strcmp 0 == { 5 ret ; }
  133. if str ">>" strcmp 0 == { 5 ret ; }
  134. if str "<" strcmp 0 == { 6 ret ; }
  135. if str "<=" strcmp 0 == { 6 ret ; }
  136. if str ">" strcmp 0 == { 6 ret ; }
  137. if str ">=" strcmp 0 == { 6 ret ; }
  138. if str "==" strcmp 0 == { 7 ret ; }
  139. if str "!=" strcmp 0 == { 7 ret ; }
  140. if str "&" strcmp 0 == { 8 ret ; }
  141. if str "^" strcmp 0 == { 9 ret ; }
  142. if str "|" strcmp 0 == { 10 ret ; }
  143. if str "&&" strcmp 0 == { 11 ret ; }
  144. if str "||" strcmp 0 == { 12 ret ; }
  145. if str "?" strcmp 0 == { 13 ret ; }
  146. if str "=" strcmp 0 == { 14 ret ; }
  147. if str "+=" strcmp 0 == { 14 ret ; }
  148. if str "-=" strcmp 0 == { 14 ret ; }
  149. if str "*=" strcmp 0 == { 14 ret ; }
  150. if str "/=" strcmp 0 == { 14 ret ; }
  151. if str "%=" strcmp 0 == { 14 ret ; }
  152. if str "<<=" strcmp 0 == { 14 ret ; }
  153. if str ">>=" strcmp 0 == { 14 ret ; }
  154. if str "&=" strcmp 0 == { 14 ret ; }
  155. if str "^=" strcmp 0 == { 14 ret ; }
  156. if str "|=" strcmp 0 == { 14 ret ; }
  157. if str "," strcmp 0 == { 15 ret ; }
  158. if str " " strcmp 0 == { 100 ret ; }
  159. 0 "Not an operator" str assert_msg_str ;
  160. }
  161. # See http://en.cppreference.com/w/c/language/operator_precedence
  162. # 0 is right-to-left
  163. # 1 is left-to-right
  164. fun ast_get_ass_direction 1 {
  165. $str
  166. @str 0 param = ;
  167. if str "++_POST" strcmp 0 == { 1 ret ; }
  168. if str "--_POST" strcmp 0 == { 1 ret ; }
  169. if str "(" strcmp 0 == { 1 ret ; }
  170. if str "[" strcmp 0 == { 1 ret ; }
  171. if str "." strcmp 0 == { 1 ret ; }
  172. if str "->" strcmp 0 == { 1 ret ; }
  173. if str "defined_PRE" strcmp 0 == { 0 ret ; }
  174. if str "++_PRE" strcmp 0 == { 0 ret ; }
  175. if str "--_PRE" strcmp 0 == { 0 ret ; }
  176. if str "+_PRE" strcmp 0 == { 0 ret ; }
  177. if str "-_PRE" strcmp 0 == { 0 ret ; }
  178. if str "!_PRE" strcmp 0 == { 0 ret ; }
  179. if str "~_PRE" strcmp 0 == { 0 ret ; }
  180. if str "(_PRE" strcmp 0 == { 0 ret ; }
  181. if str "*_PRE" strcmp 0 == { 0 ret ; }
  182. if str "&_PRE" strcmp 0 == { 0 ret ; }
  183. if str "sizeof_PRE" strcmp 0 == { 0 ret ; }
  184. if str "*" strcmp 0 == { 1 ret ; }
  185. if str "/" strcmp 0 == { 1 ret ; }
  186. if str "%" strcmp 0 == { 1 ret ; }
  187. if str "+" strcmp 0 == { 1 ret ; }
  188. if str "-" strcmp 0 == { 1 ret ; }
  189. if str "<<" strcmp 0 == { 1 ret ; }
  190. if str ">>" strcmp 0 == { 1 ret ; }
  191. if str "<" strcmp 0 == { 1 ret ; }
  192. if str "<=" strcmp 0 == { 1 ret ; }
  193. if str ">" strcmp 0 == { 1 ret ; }
  194. if str ">=" strcmp 0 == { 1 ret ; }
  195. if str "==" strcmp 0 == { 1 ret ; }
  196. if str "!=" strcmp 0 == { 1 ret ; }
  197. if str "&" strcmp 0 == { 1 ret ; }
  198. if str "^" strcmp 0 == { 1 ret ; }
  199. if str "|" strcmp 0 == { 1 ret ; }
  200. if str "&&" strcmp 0 == { 1 ret ; }
  201. if str "||" strcmp 0 == { 1 ret ; }
  202. if str "?" strcmp 0 == { 0 ret ; }
  203. if str "=" strcmp 0 == { 0 ret ; }
  204. if str "+=" strcmp 0 == { 0 ret ; }
  205. if str "-=" strcmp 0 == { 0 ret ; }
  206. if str "*=" strcmp 0 == { 0 ret ; }
  207. if str "/=" strcmp 0 == { 0 ret ; }
  208. if str "%=" strcmp 0 == { 0 ret ; }
  209. if str "<<=" strcmp 0 == { 0 ret ; }
  210. if str ">>=" strcmp 0 == { 0 ret ; }
  211. if str "&=" strcmp 0 == { 0 ret ; }
  212. if str "^=" strcmp 0 == { 0 ret ; }
  213. if str "|=" strcmp 0 == { 0 ret ; }
  214. if str "," strcmp 0 == { 1 ret ; }
  215. if str " " strcmp 0 == { 0 ret ; }
  216. 0 "Not an operator" str assert_msg_str ;
  217. }
  218. fun ast_rewind_stack 3 {
  219. $operator_stack
  220. $operand_stack
  221. $center_stack
  222. @operator_stack 2 param = ;
  223. @operand_stack 1 param = ;
  224. @center_stack 0 param = ;
  225. operand_stack vector_size operator_stack vector_size == "Stacks do not have the same size" assert_msg ;
  226. operand_stack vector_size center_stack vector_size == "Stacks do not have the same size" assert_msg ;
  227. $cont
  228. @cont 1 = ;
  229. while operator_stack vector_size 2 >= cont && {
  230. $last_pri
  231. $nlast_pri
  232. $dir
  233. @last_pri operator_stack operator_stack vector_size 1 - vector_at ast_get_priority = ;
  234. @nlast_pri operator_stack operator_stack vector_size 2 - vector_at ast_get_priority = ;
  235. @dir operator_stack operator_stack vector_size 1 - vector_at ast_get_ass_direction = ;
  236. if last_pri nlast_pri > last_pri nlast_pri == dir && || {
  237. $ast
  238. @ast ast_init = ;
  239. $tmp
  240. $tmp2
  241. $center
  242. @tmp operator_stack vector_pop_back = ;
  243. @tmp2 center_stack vector_pop_back = ;
  244. ast AST_TYPE take_addr 1 = ;
  245. ast AST_RIGHT take_addr operand_stack vector_pop_back = ;
  246. ast AST_LEFT take_addr operand_stack vector_pop_back = ;
  247. ast AST_NAME take_addr operator_stack vector_pop_back = ;
  248. @center center_stack vector_pop_back = ;
  249. operand_stack ast vector_push_back ;
  250. operator_stack tmp vector_push_back ;
  251. center_stack tmp2 vector_push_back ;
  252. # Sanity check
  253. if ast AST_NAME take "?" strcmp 0 == {
  254. center 0 != "ast_rewind_stack: ternary operator misses center operand" assert_msg ;
  255. ast AST_CENTER take_addr center = ;
  256. } else {
  257. if ast AST_NAME take "(_PRE" strcmp 0 == {
  258. center 0xffffffff 0 != "ast_rewind_stack: cast operator misses type index" assert_msg ;
  259. ast AST_CAST_TYPE_IDX take_addr center = ;
  260. } else {
  261. ast AST_CENTER take 0 == "ast_rewind_stack: center operand for non-ternary operator" assert_msg ;
  262. }
  263. }
  264. } else {
  265. @cont 0 = ;
  266. }
  267. operand_stack vector_size operator_stack vector_size == "Stacks do not have the same size" assert_msg ;
  268. operand_stack vector_size center_stack vector_size == "Stacks do not have the same size" assert_msg ;
  269. }
  270. }
  271. ifun ast_parse1 2
  272. fun ast_parsev 2 {
  273. $int
  274. $end_toks
  275. @int 1 param = ;
  276. @end_toks 0 param = ;
  277. $cont
  278. @cont 1 = ;
  279. $expect_operator
  280. @expect_operator 0 = ;
  281. $operator_stack
  282. @operator_stack 4 vector_init = ;
  283. $operand_stack
  284. @operand_stack 4 vector_init = ;
  285. $center_stack
  286. @center_stack 4 vector_init = ;
  287. # "Beginning parse\n" log ;
  288. while cont {
  289. $tok
  290. $stop
  291. @stop 0 = ;
  292. @tok int int ASTINT_GET_TOKEN take \1 = ;
  293. if tok 0 == {
  294. @stop 1 = ;
  295. }
  296. $i
  297. @i 0 = ;
  298. while i end_toks vector_size < stop ! && {
  299. if end_toks i vector_at tok strcmp 0 == {
  300. int int ASTINT_GIVE_BACK_TOKEN take \1 ;
  301. @stop 1 = ;
  302. }
  303. @i i 1 + = ;
  304. }
  305. if stop {
  306. @cont 0 = ;
  307. } else {
  308. if " " tok strcmp 0 != {
  309. $is_operator
  310. @is_operator tok ast_is_operator = ;
  311. # "Found: " log ;
  312. # tok log ;
  313. # if is_operator {
  314. # " (operator)" log ;
  315. # } else {
  316. # " (operand)" log ;
  317. # }
  318. # "\n" log ;
  319. if expect_operator {
  320. if is_operator {
  321. $center_ast
  322. @center_ast 0 = ;
  323. # Operator as we expect, push it in the operator stack.
  324. # If the operator is postfix, mangle it and push
  325. # a placeholder operand in the operand stack
  326. $is_postfix
  327. @is_postfix 0 = ;
  328. if tok "++" strcmp 0 == {
  329. @tok "++_POST" = ;
  330. @is_postfix 1 = ;
  331. }
  332. if tok "--" strcmp 0 == {
  333. @tok "--_POST" = ;
  334. @is_postfix 1 = ;
  335. }
  336. # If this is the ternary operator, parse the center part
  337. # immediately
  338. if tok "?" strcmp 0 == {
  339. @center_ast int ":" ast_parse1 = ;
  340. int int ASTINT_GET_TOKEN_OR_FAIL take \1 ":" strcmp 0 == "ast_parsev: error 1" assert_msg ;
  341. }
  342. operator_stack tok strdup vector_push_back ;
  343. center_stack center_ast vector_push_back ;
  344. operator_stack operand_stack center_stack ast_rewind_stack ;
  345. if is_postfix {
  346. operand_stack ast_init vector_push_back ;
  347. } else {
  348. @expect_operator 0 = ;
  349. }
  350. } else {
  351. $ast
  352. # Here we treat the argument-separating comma as the
  353. # comma operator; this is not correct, in theory, because
  354. # it makes a((b,c),d) the same thing as a(b,c,d). However
  355. # we hope that no sane program relies on that.
  356. if tok "(" strcmp 0 == {
  357. $tok2
  358. @tok2 int int ASTINT_GET_TOKEN_OR_FAIL take \1 = ;
  359. if tok2 ")" strcmp 0 == {
  360. # No arguments, push a placeholder
  361. @ast ast_init = ;
  362. } else {
  363. # Roll back token and parse arguments
  364. int int ASTINT_GIVE_BACK_TOKEN take \1 ;
  365. @ast int ")" ast_parse1 = ;
  366. int int ASTINT_GET_TOKEN_OR_FAIL take \1 ")" strcmp 0 == "ast_parsev: error 2" assert_msg ;
  367. }
  368. operator_stack tok strdup vector_push_back ;
  369. center_stack 0 vector_push_back ;
  370. operator_stack operand_stack center_stack ast_rewind_stack ;
  371. operand_stack ast vector_push_back ;
  372. } else {
  373. if tok "[" strcmp 0 == {
  374. @ast int "]" ast_parse1 = ;
  375. int int ASTINT_GET_TOKEN_OR_FAIL take \1 "]" strcmp 0 == "ast_parsev: error 3" assert_msg ;
  376. operator_stack tok strdup vector_push_back ;
  377. center_stack 0 vector_push_back ;
  378. operator_stack operand_stack center_stack ast_rewind_stack ;
  379. operand_stack ast vector_push_back ;
  380. } else {
  381. # Operand instead of operator: error!
  382. 0 "Operand instead of operator" assert_msg ;
  383. }
  384. }
  385. }
  386. } else {
  387. # sizeof has two forms: one with a type name surrounded by
  388. # parentheses, and another with a generic expression; so we
  389. # have to do a little bit more of parsing to see what is the
  390. # case here
  391. $sizeof_type_idx
  392. @sizeof_type_idx 0xffffffff = ;
  393. if tok "sizeof" strcmp 0 == {
  394. $tok2
  395. @tok2 int int ASTINT_GET_TOKEN_OR_FAIL take \1 = ;
  396. if tok2 "(" strcmp 0 == {
  397. @sizeof_type_idx int int ASTINT_PARSE_TYPE take \1 = ;
  398. if sizeof_type_idx 0xffffffff != {
  399. @tok2 int int ASTINT_GET_TOKEN_OR_FAIL take \1 = ;
  400. tok2 ")" strcmp 0 == "Expect ) after type name" assert_msg ;
  401. } else {
  402. int int ASTINT_GIVE_BACK_TOKEN take \1 ;
  403. }
  404. } else {
  405. int int ASTINT_GIVE_BACK_TOKEN take \1 ;
  406. }
  407. @is_operator sizeof_type_idx 0xffffffff == = ;
  408. }
  409. if is_operator {
  410. # Operator instead of operand, it must be a prefix.
  411. # Mangle it, push it in the operator stack
  412. # and push a placeholder operand in the operand stack
  413. $found
  414. @found 0 = ;
  415. if tok "defined" strcmp 0 == {
  416. @tok "defined_PRE" = ;
  417. @found 1 = ;
  418. }
  419. if tok "sizeof" strcmp 0 == {
  420. @tok "sizeof_PRE" = ;
  421. @found 1 = ;
  422. }
  423. if tok "!" strcmp 0 == {
  424. @tok "!_PRE" = ;
  425. @found 1 = ;
  426. }
  427. if tok "~" strcmp 0 == {
  428. @tok "~_PRE" = ;
  429. @found 1 = ;
  430. }
  431. if tok "++" strcmp 0 == {
  432. @tok "++_PRE" = ;
  433. @found 1 = ;
  434. }
  435. if tok "--" strcmp 0 == {
  436. @tok "--_PRE" = ;
  437. @found 1 = ;
  438. }
  439. if tok "+" strcmp 0 == {
  440. @tok "+_PRE" = ;
  441. @found 1 = ;
  442. }
  443. if tok "-" strcmp 0 == {
  444. @tok "-_PRE" = ;
  445. @found 1 = ;
  446. }
  447. if tok "*" strcmp 0 == {
  448. @tok "*_PRE" = ;
  449. @found 1 = ;
  450. }
  451. if tok "&" strcmp 0 == {
  452. @tok "&_PRE" = ;
  453. @found 1 = ;
  454. }
  455. found "Expect prefix operator" assert_msg ;
  456. operator_stack tok strdup vector_push_back ;
  457. operand_stack ast_init vector_push_back ;
  458. center_stack 0 vector_push_back ;
  459. } else {
  460. $ast
  461. if tok "(" strcmp 0 == {
  462. # This might be a grouping parenthesis or a cast
  463. # operator; I try to parse a type: if it works, it is a
  464. # cast (and I have to treat it as a prefix operator),
  465. # otherwise it is a grouping parenthesis
  466. $type_idx
  467. @type_idx int int ASTINT_PARSE_TYPE take \1 = ;
  468. if type_idx 0xffffffff == {
  469. @ast int ")" ast_parse1 = ;
  470. int int ASTINT_GET_TOKEN_OR_FAIL take \1 ")" strcmp 0 == "ast_parsev: error 4" assert_msg ;
  471. operand_stack ast vector_push_back ;
  472. @expect_operator 1 = ;
  473. } else {
  474. int int ASTINT_GET_TOKEN_OR_FAIL take \1 ")" strcmp 0 == "ast_parsev: error 5" assert_msg ;
  475. operator_stack "(_PRE" strdup vector_push_back ;
  476. operand_stack ast_init vector_push_back ;
  477. center_stack type_idx vector_push_back ;
  478. }
  479. } else {
  480. # Operand as we expect, push it in the operand stack
  481. @ast ast_init = ;
  482. ast AST_TYPE take_addr 0 = ;
  483. ast AST_NAME take_addr tok strdup = ;
  484. ast AST_CAST_TYPE_IDX take_addr sizeof_type_idx = ;
  485. operand_stack ast vector_push_back ;
  486. @expect_operator 1 = ;
  487. }
  488. }
  489. }
  490. # Partially rewind the stack so that priority is decreasing
  491. if expect_operator ! {
  492. operator_stack operand_stack center_stack ast_rewind_stack ;
  493. }
  494. }
  495. }
  496. }
  497. expect_operator "Expect operand" assert_msg ;
  498. # Ad a final placeholder operator with the weakest possible priority,
  499. # in order to force the whole stack rewind
  500. $tmp
  501. @tmp " " strdup = ;
  502. operator_stack tmp vector_push_back ;
  503. center_stack 0 vector_push_back ;
  504. operator_stack operand_stack center_stack ast_rewind_stack ;
  505. operator_stack vector_pop_back tmp == "Internal error" assert_msg ;
  506. tmp free ;
  507. center_stack vector_pop_back 0 == "Internal error" assert_msg ;
  508. $res
  509. @res operand_stack vector_pop_back = ;
  510. operand_stack vector_size 0 == "Internal error" assert_msg ;
  511. operator_stack vector_size 0 == "Internal error" assert_msg ;
  512. center_stack vector_size 0 == "Internal error" assert_msg ;
  513. operand_stack vector_destroy ;
  514. operator_stack vector_destroy ;
  515. center_stack vector_destroy ;
  516. # "Ending parse\n" log ;
  517. res ret ;
  518. }
  519. fun ast_parse1 2 {
  520. $int
  521. $end_tok
  522. @int 1 param = ;
  523. @end_tok 0 param = ;
  524. $end_toks
  525. @end_toks 4 vector_init = ;
  526. end_toks end_tok vector_push_back ;
  527. $res
  528. @res int end_toks ast_parsev = ;
  529. end_toks vector_destroy ;
  530. res ret ;
  531. }
  532. fun ast_parse2 3 {
  533. $int
  534. $end_tok1
  535. $end_tok2
  536. @int 2 param = ;
  537. @end_tok1 1 param = ;
  538. @end_tok2 0 param = ;
  539. $end_toks
  540. @end_toks 4 vector_init = ;
  541. end_toks end_tok1 vector_push_back ;
  542. end_toks end_tok2 vector_push_back ;
  543. $res
  544. @res int end_toks ast_parsev = ;
  545. end_toks vector_destroy ;
  546. res ret ;
  547. }
  548. fun ast_parse3 4 {
  549. $int
  550. $end_tok1
  551. $end_tok2
  552. $end_tok3
  553. @int 3 param = ;
  554. @end_tok1 2 param = ;
  555. @end_tok2 1 param = ;
  556. @end_tok3 0 param = ;
  557. $end_toks
  558. @end_toks 4 vector_init = ;
  559. end_toks end_tok1 vector_push_back ;
  560. end_toks end_tok2 vector_push_back ;
  561. end_toks end_tok3 vector_push_back ;
  562. $res
  563. @res int end_toks ast_parsev = ;
  564. end_toks vector_destroy ;
  565. res ret ;
  566. }
  567. fun ast_dump_int 2 {
  568. $ast
  569. $depth
  570. @ast 1 param = ;
  571. @depth 0 param = ;
  572. $i
  573. @i 0 = ;
  574. while i depth < {
  575. " " log ;
  576. @i i 1 + = ;
  577. }
  578. if ast AST_TYPE take 0 == {
  579. if ast AST_NAME take 0 == {
  580. "Placeholder operand" log ;
  581. } else {
  582. "Operand of type #" log ;
  583. ast AST_TYPE_IDX take itoa log ;
  584. if ast AST_ORIG_TYPE_IDX take 0xffffffff != {
  585. " (orig: #" log ;
  586. ast AST_ORIG_TYPE_IDX take itoa log ;
  587. ")" log ;
  588. }
  589. if ast AST_COMMON_TYPE_IDX take 0xffffffff != {
  590. " (common: #" log ;
  591. ast AST_COMMON_TYPE_IDX take itoa log ;
  592. ")" log ;
  593. }
  594. ": " log ;
  595. ast AST_NAME take log ;
  596. }
  597. "\n" log ;
  598. } else {
  599. ast AST_TYPE take 1 == "ast_dump_int: error 1" assert_msg ;
  600. "Operator of type #" log ;
  601. ast AST_TYPE_IDX take itoa log ;
  602. if ast AST_ORIG_TYPE_IDX take 0xffffffff != {
  603. " (orig: #" log ;
  604. ast AST_ORIG_TYPE_IDX take itoa log ;
  605. ")" log ;
  606. }
  607. if ast AST_COMMON_TYPE_IDX take 0xffffffff != {
  608. " (common: #" log ;
  609. ast AST_COMMON_TYPE_IDX take itoa log ;
  610. ")" log ;
  611. }
  612. ": " log ;
  613. ast AST_NAME take log ;
  614. "\n" log ;
  615. ast AST_LEFT take depth 1 + ast_dump_int ;
  616. ast AST_RIGHT take depth 1 + ast_dump_int ;
  617. }
  618. }
  619. fun ast_dump 1 {
  620. 0 param 0 ast_dump_int ;
  621. }
  622. fun ast_eval 3 {
  623. $ast
  624. $ext
  625. $ctx
  626. @ast 2 param = ;
  627. @ext 1 param = ;
  628. @ctx 0 param = ;
  629. # Already evaluated: return directly
  630. if ast AST_VALUE take 0 != {
  631. ast AST_VALUE take ret ;
  632. }
  633. # Try to hand over to extension: if it succeeds, return directly
  634. $res
  635. @res ctx ast ext \2 = ;
  636. if res 0 != {
  637. ast AST_VALUE take_addr res = ;
  638. res ret ;
  639. }
  640. # No success so far: we have to try to do something ourselves
  641. $name
  642. @name ast AST_NAME take = ;
  643. # "ast_eval: " log ;
  644. # name log ;
  645. # "\n" log ;
  646. if ast AST_TYPE take 0 == {
  647. # We have no idea of how to treat an operand in general...
  648. 0 "ast_eval: failed with operand" name assert_msg_str ;
  649. } else {
  650. # Operator: first evaluate the right operand, and fail if it fails
  651. $right_value
  652. ast AST_RIGHT take AST_NAME take 0 != "ast_eval: right missing" name assert_msg_str ;
  653. @right_value ast AST_RIGHT take ext ctx ast_eval = ;
  654. $value
  655. @value i64_init = ;
  656. value right_value i64_copy ;
  657. ast AST_VALUE take_addr value = ;
  658. # Then execute prefix operators
  659. if name "!_PRE" strcmp 0 == {
  660. value i64_lnot ;
  661. value ret ;
  662. }
  663. # Then execute prefix operators
  664. if name "~_PRE" strcmp 0 == {
  665. value i64_not ;
  666. value ret ;
  667. }
  668. # Then execute prefix operators
  669. if name "-_PRE" strcmp 0 == {
  670. value i64_neg ;
  671. value ret ;
  672. }
  673. # If nothing matched, evaluate left operand and try with infix
  674. # operators
  675. $left_value
  676. ast AST_LEFT take AST_NAME take 0 != "ast_eval: left missing" name assert_msg_str ;
  677. @left_value ast AST_LEFT take ext ctx ast_eval = ;
  678. value left_value i64_copy ;
  679. if name "&&" strcmp 0 == {
  680. value right_value i64_land ;
  681. value ret ;
  682. }
  683. if name "||" strcmp 0 == {
  684. value right_value i64_lor ;
  685. value ret ;
  686. }
  687. if name "&" strcmp 0 == {
  688. value right_value i64_and ;
  689. value ret ;
  690. }
  691. if name "|" strcmp 0 == {
  692. value right_value i64_or ;
  693. value ret ;
  694. }
  695. if name "^" strcmp 0 == {
  696. value right_value i64_xor ;
  697. value ret ;
  698. }
  699. if name "==" strcmp 0 == {
  700. value right_value i64_eq ;
  701. value ret ;
  702. }
  703. if name "!=" strcmp 0 == {
  704. value right_value i64_neq ;
  705. value ret ;
  706. }
  707. if name ">=" strcmp 0 == {
  708. value right_value i64_ge ;
  709. value ret ;
  710. }
  711. if name "<=" strcmp 0 == {
  712. value right_value i64_le ;
  713. value ret ;
  714. }
  715. if name ">" strcmp 0 == {
  716. value right_value i64_g ;
  717. value ret ;
  718. }
  719. if name "<" strcmp 0 == {
  720. value right_value i64_l ;
  721. value ret ;
  722. }
  723. if name "+" strcmp 0 == {
  724. value right_value i64_add ;
  725. value ret ;
  726. }
  727. if name "-" strcmp 0 == {
  728. value right_value i64_sub ;
  729. value ret ;
  730. }
  731. if name "*" strcmp 0 == {
  732. value right_value i64_mul ;
  733. value ret ;
  734. }
  735. if name "/" strcmp 0 == {
  736. value right_value i64_udiv ;
  737. value ret ;
  738. }
  739. if name "%" strcmp 0 == {
  740. value right_value i64_umod ;
  741. value ret ;
  742. }
  743. if name "<<" strcmp 0 == {
  744. value right_value i64_shl ;
  745. value ret ;
  746. }
  747. if name ">>" strcmp 0 == {
  748. value right_value i64_shr ;
  749. value ret ;
  750. }
  751. # If nothing matched, evaluate center operand and try with the
  752. # ternary operator
  753. $center_value
  754. ast AST_CENTER take AST_NAME take 0 != "ast_eval: center missing" name assert_msg_str ;
  755. @center_value ast AST_CENTER take ext ctx ast_eval = ;
  756. if name "?" strcmp 0 == {
  757. if left_value i64_to_bool {
  758. value center_value i64_copy ;
  759. } else {
  760. value right_value i64_copy ;
  761. }
  762. value ret ;
  763. }
  764. # Nothing matched, so we declare failure
  765. 0 "ast_eval: failed with operator" name assert_msg_str ;
  766. }
  767. 0 "ast_eval: should not arrive here" assert_msg ;
  768. }