tcctest.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  1. /*
  2. * TCC auto test program
  3. */
  4. #include "../config.h"
  5. #if GCC_MAJOR >= 3
  6. /* Unfortunately, gcc version < 3 does not handle that! */
  7. #define ALL_ISOC99
  8. /* only gcc 3 handles _Bool correctly */
  9. #define BOOL_ISOC99
  10. /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
  11. #define CORRECT_CR_HANDLING
  12. #endif
  13. /* deprecated and no longer supported in gcc 3.3 */
  14. //#define ACCEPT_CR_IN_STRINGS
  15. /* __VA_ARGS__ and __func__ support */
  16. #define C99_MACROS
  17. /* test various include syntaxes */
  18. #define TCCLIB_INC <tcclib.h>
  19. #define TCCLIB_INC1 <tcclib
  20. #define TCCLIB_INC2 h>
  21. #define TCCLIB_INC3 "tcclib"
  22. #include TCCLIB_INC
  23. #include TCCLIB_INC1.TCCLIB_INC2
  24. #include TCCLIB_INC1.h>
  25. /* gcc 3.2 does not accept that (bug ?) */
  26. //#include TCCLIB_INC3 ".h"
  27. #include <tcclib.h>
  28. #include "tcclib.h"
  29. void string_test();
  30. void expr_test();
  31. void macro_test();
  32. void scope_test();
  33. void forward_test();
  34. void funcptr_test();
  35. void loop_test();
  36. void switch_test();
  37. void goto_test();
  38. void enum_test();
  39. void typedef_test();
  40. void struct_test();
  41. void array_test();
  42. void expr_ptr_test();
  43. void bool_test();
  44. void expr2_test();
  45. void constant_expr_test();
  46. void expr_cmp_test();
  47. void char_short_test();
  48. void init_test(void);
  49. void compound_literal_test(void);
  50. int kr_test();
  51. void struct_assign_test(void);
  52. void cast_test(void);
  53. void bitfield_test(void);
  54. void c99_bool_test(void);
  55. void float_test(void);
  56. void longlong_test(void);
  57. void manyarg_test(void);
  58. void stdarg_test(void);
  59. void whitespace_test(void);
  60. void relocation_test(void);
  61. void old_style_function(void);
  62. void alloca_test(void);
  63. void sizeof_test(void);
  64. void typeof_test(void);
  65. void local_label_test(void);
  66. void statement_expr_test(void);
  67. void asm_test(void);
  68. void builtin_test(void);
  69. int fib(int n);
  70. void num(int n);
  71. void forward_ref(void);
  72. int isid(int c);
  73. #define A 2
  74. #define N 1234 + A
  75. #define pf printf
  76. #define M1(a, b) (a) + (b)
  77. #define str\
  78. (s) # s
  79. #define glue(a, b) a ## b
  80. #define xglue(a, b) glue(a, b)
  81. #define HIGHLOW "hello"
  82. #define LOW LOW ", world"
  83. #define min(a, b) ((a) < (b) ? (a) : (b))
  84. #ifdef C99_MACROS
  85. #define dprintf(level,...) printf(__VA_ARGS__)
  86. #endif
  87. /* gcc vararg macros */
  88. #define dprintf1(level, fmt, args...) printf(fmt, ## args)
  89. #define MACRO_NOARGS()
  90. #define AAA 3
  91. #undef AAA
  92. #define AAA 4
  93. #if 1
  94. #define B3 1
  95. #elif 1
  96. #define B3 2
  97. #elif 0
  98. #define B3 3
  99. #else
  100. #define B3 4
  101. #endif
  102. #define __INT64_C(c) c ## LL
  103. #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
  104. int qq(int x)
  105. {
  106. return x + 40;
  107. }
  108. #define qq(x) x
  109. #define spin_lock(lock) do { } while (0)
  110. #define wq_spin_lock spin_lock
  111. #define TEST2() wq_spin_lock(a)
  112. void macro_test(void)
  113. {
  114. printf("macro:\n");
  115. pf("N=%d\n", N);
  116. printf("aaa=%d\n", AAA);
  117. printf("min=%d\n", min(1, min(2, -1)));
  118. printf("s1=%s\n", glue(HIGH, LOW));
  119. printf("s2=%s\n", xglue(HIGH, LOW));
  120. printf("s3=%s\n", str("c"));
  121. printf("s4=%s\n", str(a1));
  122. printf("B3=%d\n", B3);
  123. #ifdef A
  124. printf("A defined\n");
  125. #endif
  126. #ifdef B
  127. printf("B defined\n");
  128. #endif
  129. #ifdef A
  130. printf("A defined\n");
  131. #else
  132. printf("A not defined\n");
  133. #endif
  134. #ifdef B
  135. printf("B defined\n");
  136. #else
  137. printf("B not defined\n");
  138. #endif
  139. #ifdef A
  140. printf("A defined\n");
  141. #ifdef B
  142. printf("B1 defined\n");
  143. #else
  144. printf("B1 not defined\n");
  145. #endif
  146. #else
  147. printf("A not defined\n");
  148. #ifdef B
  149. printf("B2 defined\n");
  150. #else
  151. printf("B2 not defined\n");
  152. #endif
  153. #endif
  154. #if 1+1
  155. printf("test true1\n");
  156. #endif
  157. #if 0
  158. printf("test true2\n");
  159. #endif
  160. #if 1-1
  161. printf("test true3\n");
  162. #endif
  163. #if defined(A)
  164. printf("test trueA\n");
  165. #endif
  166. #if defined(B)
  167. printf("test trueB\n");
  168. #endif
  169. #if 0
  170. printf("test 0\n");
  171. #elif 0
  172. printf("test 1\n");
  173. #elif 2
  174. printf("test 2\n");
  175. #else
  176. printf("test 3\n");
  177. #endif
  178. MACRO_NOARGS();
  179. #ifdef __LINE__
  180. printf("__LINE__ defined\n");
  181. #endif
  182. printf("__LINE__=%d __FILE__=%s\n",
  183. __LINE__, __FILE__);
  184. #line 200
  185. printf("__LINE__=%d __FILE__=%s\n",
  186. __LINE__, __FILE__);
  187. #line 203 "test"
  188. printf("__LINE__=%d __FILE__=%s\n",
  189. __LINE__, __FILE__);
  190. #line 227 "tcctest.c"
  191. /* not strictly preprocessor, but we test it there */
  192. #ifdef C99_MACROS
  193. printf("__func__ = %s\n", __func__);
  194. dprintf(1, "vaarg=%d\n", 1);
  195. #endif
  196. dprintf1(1, "vaarg1\n");
  197. dprintf1(1, "vaarg1=%d\n", 2);
  198. dprintf1(1, "vaarg1=%d %d\n", 1, 2);
  199. /* gcc extension */
  200. printf("func='%s'\n", __FUNCTION__);
  201. /* complicated macros in glibc */
  202. printf("INT64_MIN=%Ld\n", INT64_MIN);
  203. {
  204. int a;
  205. a = 1;
  206. glue(a+, +);
  207. printf("a=%d\n", a);
  208. glue(a <, <= 2);
  209. printf("a=%d\n", a);
  210. }
  211. /* macro function with argument outside the macro string */
  212. #define MF_s MF_hello
  213. #define MF_hello(msg) printf("%s\n",msg)
  214. #define MF_t printf("tralala\n"); MF_hello
  215. MF_s("hi");
  216. MF_t("hi");
  217. /* test macro substituion inside args (should not eat stream) */
  218. printf("qq=%d\n", qq(qq)(2));
  219. /* test zero argument case. NOTE: gcc 2.95.x does not accept a
  220. null argument without a space. gcc 3.2 fixes that. */
  221. #define qq1(x) 1
  222. printf("qq1=%d\n", qq1( ));
  223. /* comment with stray handling *\
  224. /
  225. /* this is a valid *\/ comment */
  226. /* this is a valid comment *\*/
  227. // this is a valid\
  228. comment
  229. /* test function macro substitution when the function name is
  230. substituted */
  231. TEST2();
  232. }
  233. int op(a,b)
  234. {
  235. return a / b;
  236. }
  237. int ret(a)
  238. {
  239. if (a == 2)
  240. return 1;
  241. if (a == 3)
  242. return 2;
  243. return 0;
  244. }
  245. void ps(const char *s)
  246. {
  247. int c;
  248. while (1) {
  249. c = *s;
  250. if (c == 0)
  251. break;
  252. printf("%c", c);
  253. s++;
  254. }
  255. }
  256. const char foo1_string[] = "\
  257. bar\n\
  258. test\14\
  259. 1";
  260. void string_test()
  261. {
  262. unsigned int b;
  263. printf("string:\n");
  264. printf("\141\1423\143\n");/* dezdez test */
  265. printf("\x41\x42\x43\x3a\n");
  266. printf("c=%c\n", 'r');
  267. printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
  268. printf("foo1_string='%s'\n", foo1_string);
  269. #if 0
  270. printf("wstring=%S\n", L"abc");
  271. printf("wstring=%S\n", L"abc" L"def" "ghi");
  272. printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
  273. printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
  274. #endif
  275. ps("test\n");
  276. b = 32;
  277. while ((b = b + 1) < 96) {
  278. printf("%c", b);
  279. }
  280. printf("\n");
  281. printf("fib=%d\n", fib(33));
  282. b = 262144;
  283. while (b != 0x80000000) {
  284. num(b);
  285. b = b * 2;
  286. }
  287. }
  288. void loop_test()
  289. {
  290. int i;
  291. i = 0;
  292. while (i < 10)
  293. printf("%d", i++);
  294. printf("\n");
  295. for(i = 0; i < 10;i++)
  296. printf("%d", i);
  297. printf("\n");
  298. i = 0;
  299. do {
  300. printf("%d", i++);
  301. } while (i < 10);
  302. printf("\n");
  303. /* break/continue tests */
  304. i = 0;
  305. while (1) {
  306. if (i == 6)
  307. break;
  308. i++;
  309. if (i == 3)
  310. continue;
  311. printf("%d", i);
  312. }
  313. printf("\n");
  314. /* break/continue tests */
  315. i = 0;
  316. do {
  317. if (i == 6)
  318. break;
  319. i++;
  320. if (i == 3)
  321. continue;
  322. printf("%d", i);
  323. } while(1);
  324. printf("\n");
  325. for(i = 0;i < 10;i++) {
  326. if (i == 3)
  327. continue;
  328. printf("%d", i);
  329. }
  330. printf("\n");
  331. }
  332. void goto_test()
  333. {
  334. int i;
  335. static void *label_table[3] = { &&label1, &&label2, &&label3 };
  336. printf("goto:\n");
  337. i = 0;
  338. s_loop:
  339. if (i >= 10)
  340. goto s_end;
  341. printf("%d", i);
  342. i++;
  343. goto s_loop;
  344. s_end:
  345. printf("\n");
  346. /* we also test computed gotos (GCC extension) */
  347. for(i=0;i<3;i++) {
  348. goto *label_table[i];
  349. label1:
  350. printf("label1\n");
  351. goto next;
  352. label2:
  353. printf("label2\n");
  354. goto next;
  355. label3:
  356. printf("label3\n");
  357. next: ;
  358. }
  359. }
  360. enum {
  361. E0,
  362. E1 = 2,
  363. E2 = 4,
  364. E3,
  365. E4,
  366. };
  367. enum test {
  368. E5 = 1000,
  369. };
  370. void enum_test()
  371. {
  372. enum test b1;
  373. printf("enum:\n%d %d %d %d %d %d\n",
  374. E0, E1, E2, E3, E4, E5);
  375. b1 = 1;
  376. printf("b1=%d\n", b1);
  377. }
  378. typedef int *my_ptr;
  379. typedef int mytype1;
  380. typedef int mytype2;
  381. void typedef_test()
  382. {
  383. my_ptr a;
  384. mytype1 mytype2;
  385. int b;
  386. a = &b;
  387. *a = 1234;
  388. printf("typedef:\n");
  389. printf("a=%d\n", *a);
  390. mytype2 = 2;
  391. printf("mytype2=%d\n", mytype2);
  392. }
  393. void forward_test()
  394. {
  395. printf("forward:\n");
  396. forward_ref();
  397. forward_ref();
  398. }
  399. void forward_ref(void)
  400. {
  401. printf("forward ok\n");
  402. }
  403. typedef struct struct1 {
  404. int f1;
  405. int f2, f3;
  406. union union1 {
  407. int v1;
  408. int v2;
  409. } u;
  410. char str[3];
  411. } struct1;
  412. struct struct2 {
  413. int a;
  414. char b;
  415. };
  416. union union2 {
  417. int w1;
  418. int w2;
  419. };
  420. struct struct1 st1, st2;
  421. int main(int argc, char **argv)
  422. {
  423. string_test();
  424. expr_test();
  425. macro_test();
  426. scope_test();
  427. forward_test();
  428. funcptr_test();
  429. loop_test();
  430. switch_test();
  431. goto_test();
  432. enum_test();
  433. typedef_test();
  434. struct_test();
  435. array_test();
  436. expr_ptr_test();
  437. bool_test();
  438. expr2_test();
  439. constant_expr_test();
  440. expr_cmp_test();
  441. char_short_test();
  442. init_test();
  443. compound_literal_test();
  444. kr_test();
  445. struct_assign_test();
  446. cast_test();
  447. bitfield_test();
  448. c99_bool_test();
  449. float_test();
  450. longlong_test();
  451. manyarg_test();
  452. stdarg_test();
  453. whitespace_test();
  454. relocation_test();
  455. old_style_function();
  456. alloca_test();
  457. sizeof_test();
  458. typeof_test();
  459. statement_expr_test();
  460. local_label_test();
  461. asm_test();
  462. builtin_test();
  463. return 0;
  464. }
  465. int tab[3];
  466. int tab2[3][2];
  467. int g;
  468. void f1(g)
  469. {
  470. printf("g1=%d\n", g);
  471. }
  472. void scope_test()
  473. {
  474. printf("scope:\n");
  475. g = 2;
  476. f1(1);
  477. printf("g2=%d\n", g);
  478. {
  479. int g;
  480. g = 3;
  481. printf("g3=%d\n", g);
  482. {
  483. int g;
  484. g = 4;
  485. printf("g4=%d\n", g);
  486. }
  487. }
  488. printf("g5=%d\n", g);
  489. }
  490. void array_test(int a[4])
  491. {
  492. int i, j;
  493. printf("array:\n");
  494. printf("sizeof(a) = %d\n", sizeof(a));
  495. printf("sizeof(\"a\") = %d\n", sizeof("a"));
  496. #ifdef C99_MACROS
  497. printf("sizeof(__func__) = %d\n", sizeof(__func__));
  498. #endif
  499. printf("sizeof tab %d\n", sizeof(tab));
  500. printf("sizeof tab2 %d\n", sizeof tab2);
  501. tab[0] = 1;
  502. tab[1] = 2;
  503. tab[2] = 3;
  504. printf("%d %d %d\n", tab[0], tab[1], tab[2]);
  505. for(i=0;i<3;i++)
  506. for(j=0;j<2;j++)
  507. tab2[i][j] = 10 * i + j;
  508. for(i=0;i<3*2;i++) {
  509. printf(" %3d", ((int *)tab2)[i]);
  510. }
  511. printf("\n");
  512. }
  513. void expr_test()
  514. {
  515. int a, b;
  516. a = 0;
  517. printf("%d\n", a += 1);
  518. printf("%d\n", a -= 2);
  519. printf("%d\n", a *= 31232132);
  520. printf("%d\n", a /= 4);
  521. printf("%d\n", a %= 20);
  522. printf("%d\n", a &= 6);
  523. printf("%d\n", a ^= 7);
  524. printf("%d\n", a |= 8);
  525. printf("%d\n", a >>= 3);
  526. printf("%d\n", a <<= 4);
  527. a = 22321;
  528. b = -22321;
  529. printf("%d\n", a + 1);
  530. printf("%d\n", a - 2);
  531. printf("%d\n", a * 312);
  532. printf("%d\n", a / 4);
  533. printf("%d\n", b / 4);
  534. printf("%d\n", (unsigned)b / 4);
  535. printf("%d\n", a % 20);
  536. printf("%d\n", b % 20);
  537. printf("%d\n", (unsigned)b % 20);
  538. printf("%d\n", a & 6);
  539. printf("%d\n", a ^ 7);
  540. printf("%d\n", a | 8);
  541. printf("%d\n", a >> 3);
  542. printf("%d\n", b >> 3);
  543. printf("%d\n", (unsigned)b >> 3);
  544. printf("%d\n", a << 4);
  545. printf("%d\n", ~a);
  546. printf("%d\n", -a);
  547. printf("%d\n", +a);
  548. printf("%d\n", 12 + 1);
  549. printf("%d\n", 12 - 2);
  550. printf("%d\n", 12 * 312);
  551. printf("%d\n", 12 / 4);
  552. printf("%d\n", 12 % 20);
  553. printf("%d\n", 12 & 6);
  554. printf("%d\n", 12 ^ 7);
  555. printf("%d\n", 12 | 8);
  556. printf("%d\n", 12 >> 2);
  557. printf("%d\n", 12 << 4);
  558. printf("%d\n", ~12);
  559. printf("%d\n", -12);
  560. printf("%d\n", +12);
  561. printf("%d %d %d %d\n",
  562. isid('a'),
  563. isid('g'),
  564. isid('T'),
  565. isid('('));
  566. }
  567. int isid(int c)
  568. {
  569. return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
  570. }
  571. /**********************/
  572. int vstack[10], *vstack_ptr;
  573. void vpush(int vt, int vc)
  574. {
  575. *vstack_ptr++ = vt;
  576. *vstack_ptr++ = vc;
  577. }
  578. void vpop(int *ft, int *fc)
  579. {
  580. *fc = *--vstack_ptr;
  581. *ft = *--vstack_ptr;
  582. }
  583. void expr2_test()
  584. {
  585. int a, b;
  586. printf("expr2:\n");
  587. vstack_ptr = vstack;
  588. vpush(1432432, 2);
  589. vstack_ptr[-2] &= ~0xffffff80;
  590. vpop(&a, &b);
  591. printf("res= %d %d\n", a, b);
  592. }
  593. void constant_expr_test()
  594. {
  595. int a;
  596. printf("constant_expr:\n");
  597. a = 3;
  598. printf("%d\n", a * 16);
  599. printf("%d\n", a * 1);
  600. printf("%d\n", a + 0);
  601. }
  602. int tab4[10];
  603. void expr_ptr_test()
  604. {
  605. int *p, *q;
  606. int i = -1;
  607. printf("expr_ptr:\n");
  608. p = tab4;
  609. q = tab4 + 10;
  610. printf("diff=%d\n", q - p);
  611. p++;
  612. printf("inc=%d\n", p - tab4);
  613. p--;
  614. printf("dec=%d\n", p - tab4);
  615. ++p;
  616. printf("inc=%d\n", p - tab4);
  617. --p;
  618. printf("dec=%d\n", p - tab4);
  619. printf("add=%d\n", p + 3 - tab4);
  620. printf("add=%d\n", 3 + p - tab4);
  621. /* check if 64bit support is ok */
  622. q = p = 0;
  623. q += i;
  624. printf("%p %p %ld\n", q, p, p-q);
  625. printf("%d %d %d %d %d %d\n",
  626. p == q, p != q, p < q, p <= q, p >= q, p > q);
  627. i = 0xf0000000;
  628. p += i;
  629. printf("%p %p %ld\n", q, p, p-q);
  630. printf("%d %d %d %d %d %d\n",
  631. p == q, p != q, p < q, p <= q, p >= q, p > q);
  632. p = (int *)((char *)p + 0xf0000000);
  633. printf("%p %p %ld\n", q, p, p-q);
  634. printf("%d %d %d %d %d %d\n",
  635. p == q, p != q, p < q, p <= q, p >= q, p > q);
  636. p += 0xf0000000;
  637. printf("%p %p %ld\n", q, p, p-q);
  638. printf("%d %d %d %d %d %d\n",
  639. p == q, p != q, p < q, p <= q, p >= q, p > q);
  640. {
  641. struct size12 {
  642. int i, j, k;
  643. };
  644. struct size12 s[2], *sp = s;
  645. int i, j;
  646. sp->i = 42;
  647. sp++;
  648. j = -1;
  649. printf("%d\n", sp[j].i);
  650. }
  651. }
  652. void expr_cmp_test()
  653. {
  654. int a, b;
  655. printf("constant_expr:\n");
  656. a = -1;
  657. b = 1;
  658. printf("%d\n", a == a);
  659. printf("%d\n", a != a);
  660. printf("%d\n", a < b);
  661. printf("%d\n", a <= b);
  662. printf("%d\n", a <= a);
  663. printf("%d\n", b >= a);
  664. printf("%d\n", a >= a);
  665. printf("%d\n", b > a);
  666. printf("%d\n", (unsigned)a < b);
  667. printf("%d\n", (unsigned)a <= b);
  668. printf("%d\n", (unsigned)a <= a);
  669. printf("%d\n", (unsigned)b >= a);
  670. printf("%d\n", (unsigned)a >= a);
  671. printf("%d\n", (unsigned)b > a);
  672. }
  673. struct empty {
  674. };
  675. struct aligntest1 {
  676. char a[10];
  677. };
  678. struct aligntest2 {
  679. int a;
  680. char b[10];
  681. };
  682. struct aligntest3 {
  683. double a, b;
  684. };
  685. struct aligntest4 {
  686. double a[0];
  687. };
  688. void struct_test()
  689. {
  690. struct1 *s;
  691. union union2 u;
  692. printf("struct:\n");
  693. printf("sizes: %d %d %d %d\n",
  694. sizeof(struct struct1),
  695. sizeof(struct struct2),
  696. sizeof(union union1),
  697. sizeof(union union2));
  698. st1.f1 = 1;
  699. st1.f2 = 2;
  700. st1.f3 = 3;
  701. printf("st1: %d %d %d\n",
  702. st1.f1, st1.f2, st1.f3);
  703. st1.u.v1 = 1;
  704. st1.u.v2 = 2;
  705. printf("union1: %d\n", st1.u.v1);
  706. u.w1 = 1;
  707. u.w2 = 2;
  708. printf("union2: %d\n", u.w1);
  709. s = &st2;
  710. s->f1 = 3;
  711. s->f2 = 2;
  712. s->f3 = 1;
  713. printf("st2: %d %d %d\n",
  714. s->f1, s->f2, s->f3);
  715. printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
  716. /* align / size tests */
  717. printf("aligntest1 sizeof=%d alignof=%d\n",
  718. sizeof(struct aligntest1), __alignof__(struct aligntest1));
  719. printf("aligntest2 sizeof=%d alignof=%d\n",
  720. sizeof(struct aligntest2), __alignof__(struct aligntest2));
  721. printf("aligntest3 sizeof=%d alignof=%d\n",
  722. sizeof(struct aligntest3), __alignof__(struct aligntest3));
  723. printf("aligntest4 sizeof=%d alignof=%d\n",
  724. sizeof(struct aligntest4), __alignof__(struct aligntest4));
  725. /* empty structures (GCC extension) */
  726. printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
  727. printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
  728. }
  729. /* XXX: depend on endianness */
  730. void char_short_test()
  731. {
  732. int var1, var2;
  733. printf("char_short:\n");
  734. var1 = 0x01020304;
  735. var2 = 0xfffefdfc;
  736. printf("s8=%d %d\n",
  737. *(char *)&var1, *(char *)&var2);
  738. printf("u8=%d %d\n",
  739. *(unsigned char *)&var1, *(unsigned char *)&var2);
  740. printf("s16=%d %d\n",
  741. *(short *)&var1, *(short *)&var2);
  742. printf("u16=%d %d\n",
  743. *(unsigned short *)&var1, *(unsigned short *)&var2);
  744. printf("s32=%d %d\n",
  745. *(int *)&var1, *(int *)&var2);
  746. printf("u32=%d %d\n",
  747. *(unsigned int *)&var1, *(unsigned int *)&var2);
  748. *(char *)&var1 = 0x08;
  749. printf("var1=%x\n", var1);
  750. *(short *)&var1 = 0x0809;
  751. printf("var1=%x\n", var1);
  752. *(int *)&var1 = 0x08090a0b;
  753. printf("var1=%x\n", var1);
  754. }
  755. /******************/
  756. typedef struct Sym {
  757. int v;
  758. int t;
  759. int c;
  760. struct Sym *next;
  761. struct Sym *prev;
  762. } Sym;
  763. #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
  764. #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
  765. static int toupper1(int a)
  766. {
  767. return TOUPPER(a);
  768. }
  769. void bool_test()
  770. {
  771. int *s, a, b, t, f, i;
  772. a = 0;
  773. s = (void*)0;
  774. printf("!s=%d\n", !s);
  775. if (!s || !s[0])
  776. a = 1;
  777. printf("a=%d\n", a);
  778. printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
  779. printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
  780. printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
  781. #if 1 && 1
  782. printf("a1\n");
  783. #endif
  784. #if 1 || 0
  785. printf("a2\n");
  786. #endif
  787. #if 1 ? 0 : 1
  788. printf("a3\n");
  789. #endif
  790. #if 0 ? 0 : 1
  791. printf("a4\n");
  792. #endif
  793. a = 4;
  794. printf("b=%d\n", a + (0 ? 1 : a / 2));
  795. /* test register spilling */
  796. a = 10;
  797. b = 10;
  798. a = (a + b) * ((a < b) ?
  799. ((b - a) * (a - b)): a + b);
  800. printf("a=%d\n", a);
  801. /* test complex || or && expressions */
  802. t = 1;
  803. f = 0;
  804. a = 32;
  805. printf("exp=%d\n", f == (32 <= a && a <= 3));
  806. printf("r=%d\n", (t || f) + (t && f));
  807. /* test ? : cast */
  808. {
  809. int aspect_on;
  810. int aspect_native = 65536;
  811. double bfu_aspect = 1.0;
  812. int aspect;
  813. for(aspect_on = 0; aspect_on < 2; aspect_on++) {
  814. aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
  815. printf("aspect=%d\n", aspect);
  816. }
  817. }
  818. /* test ? : GCC extension */
  819. {
  820. static int v1 = 34 ? : -1; /* constant case */
  821. static int v2 = 0 ? : -1; /* constant case */
  822. int a = 30;
  823. printf("%d %d\n", v1, v2);
  824. printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
  825. }
  826. /* again complex expression */
  827. for(i=0;i<256;i++) {
  828. if (toupper1 (i) != TOUPPER (i))
  829. printf("error %d\n", i);
  830. }
  831. }
  832. /* GCC accepts that */
  833. static int tab_reinit[];
  834. static int tab_reinit[10];
  835. //int cinit1; /* a global variable can be defined several times without error ! */
  836. int cinit1;
  837. int cinit1;
  838. int cinit1 = 0;
  839. int *cinit2 = (int []){3, 2, 1};
  840. void compound_literal_test(void)
  841. {
  842. int *p, i;
  843. char *q, *q3;
  844. printf("compound_test:\n");
  845. p = (int []){1, 2, 3};
  846. for(i=0;i<3;i++)
  847. printf(" %d", p[i]);
  848. printf("\n");
  849. for(i=0;i<3;i++)
  850. printf("%d", cinit2[i]);
  851. printf("\n");
  852. q = "tralala1";
  853. printf("q1=%s\n", q);
  854. q = (char *){ "tralala2" };
  855. printf("q2=%s\n", q);
  856. q3 = (char *){ q };
  857. printf("q3=%s\n", q3);
  858. q = (char []){ "tralala3" };
  859. printf("q4=%s\n", q);
  860. #ifdef ALL_ISOC99
  861. p = (int []){1, 2, cinit1 + 3};
  862. for(i=0;i<3;i++)
  863. printf(" %d", p[i]);
  864. printf("\n");
  865. for(i=0;i<3;i++) {
  866. p = (int []){1, 2, 4 + i};
  867. printf("%d %d %d\n",
  868. p[0],
  869. p[1],
  870. p[2]);
  871. }
  872. #endif
  873. }
  874. /* K & R protos */
  875. kr_func1(a, b)
  876. {
  877. return a + b;
  878. }
  879. int kr_func2(a, b)
  880. {
  881. return a + b;
  882. }
  883. kr_test()
  884. {
  885. printf("kr_test:\n");
  886. printf("func1=%d\n", kr_func1(3, 4));
  887. printf("func2=%d\n", kr_func2(3, 4));
  888. return 0;
  889. }
  890. void num(int n)
  891. {
  892. char *tab, *p;
  893. tab = (char*)malloc(20);
  894. p = tab;
  895. while (1) {
  896. *p = 48 + (n % 10);
  897. p++;
  898. n = n / 10;
  899. if (n == 0)
  900. break;
  901. }
  902. while (p != tab) {
  903. p--;
  904. printf("%c", *p);
  905. }
  906. printf("\n");
  907. }
  908. /* structure assignment tests */
  909. struct structa1 {
  910. int f1;
  911. char f2;
  912. };
  913. struct structa1 ssta1;
  914. void struct_assign_test1(struct structa1 s1, int t)
  915. {
  916. printf("%d %d %d\n", s1.f1, s1.f2, t);
  917. }
  918. struct structa1 struct_assign_test2(struct structa1 s1, int t)
  919. {
  920. s1.f1 += t;
  921. s1.f2 -= t;
  922. return s1;
  923. }
  924. void struct_assign_test(void)
  925. {
  926. struct structa1 lsta1, lsta2;
  927. #if 0
  928. printf("struct_assign_test:\n");
  929. lsta1.f1 = 1;
  930. lsta1.f2 = 2;
  931. printf("%d %d\n", lsta1.f1, lsta1.f2);
  932. lsta2 = lsta1;
  933. printf("%d %d\n", lsta2.f1, lsta2.f2);
  934. #else
  935. lsta2.f1 = 1;
  936. lsta2.f2 = 2;
  937. #endif
  938. struct_assign_test1(lsta2, 3);
  939. printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
  940. lsta2 = struct_assign_test2(lsta2, 4);
  941. printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
  942. }
  943. /* casts to short/char */
  944. void cast1(char a, short b, unsigned char c, unsigned short d)
  945. {
  946. printf("%d %d %d %d\n", a, b, c, d);
  947. }
  948. char bcast;
  949. short scast;
  950. void cast_test()
  951. {
  952. int a;
  953. char c;
  954. char tab[10];
  955. unsigned b,d;
  956. short s;
  957. char *p = NULL;
  958. p -= 0x700000000042;
  959. printf("cast_test:\n");
  960. a = 0xfffff;
  961. cast1(a, a, a, a);
  962. a = 0xffffe;
  963. printf("%d %d %d %d\n",
  964. (char)(a + 1),
  965. (short)(a + 1),
  966. (unsigned char)(a + 1),
  967. (unsigned short)(a + 1));
  968. printf("%d %d %d %d\n",
  969. (char)0xfffff,
  970. (short)0xfffff,
  971. (unsigned char)0xfffff,
  972. (unsigned short)0xfffff);
  973. a = (bcast = 128) + 1;
  974. printf("%d\n", a);
  975. a = (scast = 65536) + 1;
  976. printf("%d\n", a);
  977. printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
  978. /* test cast from unsigned to signed short to int */
  979. b = 0xf000;
  980. d = (short)b;
  981. printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
  982. b = 0xf0f0;
  983. d = (char)b;
  984. printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
  985. /* test implicit int casting for array accesses */
  986. c = 0;
  987. tab[1] = 2;
  988. tab[c] = 1;
  989. printf("%d %d\n", tab[0], tab[1]);
  990. /* test implicit casting on some operators */
  991. printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
  992. printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
  993. printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
  994. /* from pointer to integer types */
  995. printf("%d %d %ld %ld %lld %lld\n",
  996. (int)p, (unsigned int)p,
  997. (long)p, (unsigned long)p,
  998. (long long)p, (unsigned long long)p);
  999. /* from integers to pointers */
  1000. printf("%p %p %p %p\n",
  1001. (void *)a, (void *)b, (void *)c, (void *)d);
  1002. }
  1003. /* initializers tests */
  1004. struct structinit1 {
  1005. int f1;
  1006. char f2;
  1007. short f3;
  1008. int farray[3];
  1009. };
  1010. int sinit1 = 2;
  1011. int sinit2 = { 3 };
  1012. int sinit3[3] = { 1, 2, {{3}}, };
  1013. int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
  1014. int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
  1015. int sinit6[] = { 1, 2, 3 };
  1016. int sinit7[] = { [2] = 3, [0] = 1, 2 };
  1017. char sinit8[] = "hello" "trala";
  1018. struct structinit1 sinit9 = { 1, 2, 3 };
  1019. struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
  1020. struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
  1021. #ifdef ALL_ISOC99
  1022. .farray[0] = 10,
  1023. .farray[1] = 11,
  1024. .farray[2] = 12,
  1025. #endif
  1026. };
  1027. char *sinit12 = "hello world";
  1028. char *sinit13[] = {
  1029. "test1",
  1030. "test2",
  1031. "test3",
  1032. };
  1033. char sinit14[10] = { "abc" };
  1034. int sinit15[3] = { sizeof(sinit15), 1, 2 };
  1035. struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
  1036. struct bar {
  1037. char *s;
  1038. int len;
  1039. } sinit17[] = {
  1040. "a1", 4,
  1041. "a2", 1
  1042. };
  1043. int sinit18[10] = {
  1044. [2 ... 5] = 20,
  1045. 2,
  1046. [8] = 10,
  1047. };
  1048. void init_test(void)
  1049. {
  1050. int linit1 = 2;
  1051. int linit2 = { 3 };
  1052. int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
  1053. int linit6[] = { 1, 2, 3 };
  1054. int i, j;
  1055. char linit8[] = "hello" "trala";
  1056. int linit12[10] = { 1, 2 };
  1057. int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
  1058. char linit14[10] = "abc";
  1059. int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
  1060. struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
  1061. int linit17 = sizeof(linit17);
  1062. printf("init_test:\n");
  1063. printf("sinit1=%d\n", sinit1);
  1064. printf("sinit2=%d\n", sinit2);
  1065. printf("sinit3=%d %d %d %d\n",
  1066. sizeof(sinit3),
  1067. sinit3[0],
  1068. sinit3[1],
  1069. sinit3[2]
  1070. );
  1071. printf("sinit6=%d\n", sizeof(sinit6));
  1072. printf("sinit7=%d %d %d %d\n",
  1073. sizeof(sinit7),
  1074. sinit7[0],
  1075. sinit7[1],
  1076. sinit7[2]
  1077. );
  1078. printf("sinit8=%s\n", sinit8);
  1079. printf("sinit9=%d %d %d\n",
  1080. sinit9.f1,
  1081. sinit9.f2,
  1082. sinit9.f3
  1083. );
  1084. printf("sinit10=%d %d %d\n",
  1085. sinit10.f1,
  1086. sinit10.f2,
  1087. sinit10.f3
  1088. );
  1089. printf("sinit11=%d %d %d %d %d %d\n",
  1090. sinit11.f1,
  1091. sinit11.f2,
  1092. sinit11.f3,
  1093. sinit11.farray[0],
  1094. sinit11.farray[1],
  1095. sinit11.farray[2]
  1096. );
  1097. for(i=0;i<3;i++)
  1098. for(j=0;j<2;j++)
  1099. printf("[%d][%d] = %d %d %d\n",
  1100. i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
  1101. printf("linit1=%d\n", linit1);
  1102. printf("linit2=%d\n", linit2);
  1103. printf("linit6=%d\n", sizeof(linit6));
  1104. printf("linit8=%d %s\n", sizeof(linit8), linit8);
  1105. printf("sinit12=%s\n", sinit12);
  1106. printf("sinit13=%d %s %s %s\n",
  1107. sizeof(sinit13),
  1108. sinit13[0],
  1109. sinit13[1],
  1110. sinit13[2]);
  1111. printf("sinit14=%s\n", sinit14);
  1112. for(i=0;i<10;i++) printf(" %d", linit12[i]);
  1113. printf("\n");
  1114. for(i=0;i<10;i++) printf(" %d", linit13[i]);
  1115. printf("\n");
  1116. for(i=0;i<10;i++) printf(" %d", linit14[i]);
  1117. printf("\n");
  1118. for(i=0;i<10;i++) printf(" %d", linit15[i]);
  1119. printf("\n");
  1120. printf("%d %d %d %d\n",
  1121. linit16.a1,
  1122. linit16.a2,
  1123. linit16.a3,
  1124. linit16.a4);
  1125. /* test that initialisation is done after variable declare */
  1126. printf("linit17=%d\n", linit17);
  1127. printf("sinit15=%d\n", sinit15[0]);
  1128. printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
  1129. printf("sinit17=%s %d %s %d\n",
  1130. sinit17[0].s, sinit17[0].len,
  1131. sinit17[1].s, sinit17[1].len);
  1132. for(i=0;i<10;i++)
  1133. printf("%x ", sinit18[i]);
  1134. printf("\n");
  1135. }
  1136. void switch_test()
  1137. {
  1138. int i;
  1139. for(i=0;i<15;i++) {
  1140. switch(i) {
  1141. case 0:
  1142. case 1:
  1143. printf("a");
  1144. break;
  1145. default:
  1146. printf("%d", i);
  1147. break;
  1148. case 8 ... 12:
  1149. printf("c");
  1150. break;
  1151. case 3:
  1152. printf("b");
  1153. break;
  1154. }
  1155. }
  1156. printf("\n");
  1157. }
  1158. /* ISOC99 _Bool type */
  1159. void c99_bool_test(void)
  1160. {
  1161. #ifdef BOOL_ISOC99
  1162. int a;
  1163. _Bool b;
  1164. printf("bool_test:\n");
  1165. printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
  1166. a = 3;
  1167. printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
  1168. b = 3;
  1169. printf("b = %d\n", b);
  1170. b++;
  1171. printf("b = %d\n", b);
  1172. #endif
  1173. }
  1174. void bitfield_test(void)
  1175. {
  1176. int a;
  1177. struct sbf1 {
  1178. int f1 : 3;
  1179. int : 2;
  1180. int f2 : 1;
  1181. int : 0;
  1182. int f3 : 5;
  1183. int f4 : 7;
  1184. unsigned int f5 : 7;
  1185. } st1;
  1186. printf("bitfield_test:");
  1187. printf("sizeof(st1) = %d\n", sizeof(st1));
  1188. st1.f1 = 3;
  1189. st1.f2 = 1;
  1190. st1.f3 = 15;
  1191. a = 120;
  1192. st1.f4 = a;
  1193. st1.f5 = a;
  1194. st1.f5++;
  1195. printf("%d %d %d %d %d\n",
  1196. st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
  1197. st1.f1 = 7;
  1198. if (st1.f1 == -1)
  1199. printf("st1.f1 == -1\n");
  1200. else
  1201. printf("st1.f1 != -1\n");
  1202. if (st1.f2 == -1)
  1203. printf("st1.f2 == -1\n");
  1204. else
  1205. printf("st1.f2 != -1\n");
  1206. /* bit sizes below must be bigger than 32 since GCC doesn't allow
  1207. long-long bitfields whose size is not bigger than int */
  1208. struct sbf2 {
  1209. long long f1 : 45;
  1210. long long : 2;
  1211. long long f2 : 35;
  1212. unsigned long long f3 : 38;
  1213. } st2;
  1214. st2.f1 = 0x123456789ULL;
  1215. a = 120;
  1216. st2.f2 = (long long)a << 25;
  1217. st2.f3 = a;
  1218. st2.f2++;
  1219. printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
  1220. }
  1221. #ifdef __x86_64__
  1222. #define FLOAT_FMT "%f\n"
  1223. #else
  1224. /* x86's float isn't compatible with GCC */
  1225. #define FLOAT_FMT "%.5f\n"
  1226. #endif
  1227. /* declare strto* functions as they are C99 */
  1228. double strtod(const char *nptr, char **endptr);
  1229. float strtof(const char *nptr, char **endptr);
  1230. long double strtold(const char *nptr, char **endptr);
  1231. #define FTEST(prefix, type, fmt)\
  1232. void prefix ## cmp(type a, type b)\
  1233. {\
  1234. printf("%d %d %d %d %d %d\n",\
  1235. a == b,\
  1236. a != b,\
  1237. a < b,\
  1238. a > b,\
  1239. a >= b,\
  1240. a <= b);\
  1241. printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
  1242. a,\
  1243. b,\
  1244. a + b,\
  1245. a - b,\
  1246. a * b,\
  1247. a / b,\
  1248. -a);\
  1249. printf(fmt "\n", ++a);\
  1250. printf(fmt "\n", a++);\
  1251. printf(fmt "\n", a);\
  1252. b = 0;\
  1253. printf("%d %d\n", !a, !b);\
  1254. }\
  1255. void prefix ## fcast(type a)\
  1256. {\
  1257. float fa;\
  1258. double da;\
  1259. long double la;\
  1260. int ia;\
  1261. unsigned int ua;\
  1262. type b;\
  1263. fa = a;\
  1264. da = a;\
  1265. la = a;\
  1266. printf("ftof: %f %f %Lf\n", fa, da, la);\
  1267. ia = (int)a;\
  1268. ua = (unsigned int)a;\
  1269. printf("ftoi: %d %u\n", ia, ua);\
  1270. ia = -1234;\
  1271. ua = 0x81234500;\
  1272. b = ia;\
  1273. printf("itof: " fmt "\n", b);\
  1274. b = ua;\
  1275. printf("utof: " fmt "\n", b);\
  1276. }\
  1277. \
  1278. float prefix ## retf(type a) { return a; }\
  1279. double prefix ## retd(type a) { return a; }\
  1280. long double prefix ## retld(type a) { return a; }\
  1281. \
  1282. void prefix ## call(void)\
  1283. {\
  1284. printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
  1285. printf("double: %f\n", prefix ## retd(42.123456789));\
  1286. printf("long double: %Lf\n", prefix ## retld(42.123456789));\
  1287. printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
  1288. }\
  1289. \
  1290. void prefix ## test(void)\
  1291. {\
  1292. printf("testing '%s'\n", #type);\
  1293. prefix ## cmp(1, 2.5);\
  1294. prefix ## cmp(2, 1.5);\
  1295. prefix ## cmp(1, 1);\
  1296. prefix ## fcast(234.6);\
  1297. prefix ## fcast(-2334.6);\
  1298. prefix ## call();\
  1299. }
  1300. FTEST(f, float, "%f")
  1301. FTEST(d, double, "%f")
  1302. FTEST(ld, long double, "%Lf")
  1303. double ftab1[3] = { 1.2, 3.4, -5.6 };
  1304. void float_test(void)
  1305. {
  1306. float fa, fb;
  1307. double da, db;
  1308. int a;
  1309. unsigned int b;
  1310. printf("float_test:\n");
  1311. printf("sizeof(float) = %d\n", sizeof(float));
  1312. printf("sizeof(double) = %d\n", sizeof(double));
  1313. printf("sizeof(long double) = %d\n", sizeof(long double));
  1314. ftest();
  1315. dtest();
  1316. ldtest();
  1317. printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
  1318. printf("%f %f %f\n", 2.12, .5, 2.3e10);
  1319. // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
  1320. da = 123;
  1321. printf("da=%f\n", da);
  1322. fa = 123;
  1323. printf("fa=%f\n", fa);
  1324. a = 4000000000;
  1325. da = a;
  1326. printf("da = %f\n", da);
  1327. b = 4000000000;
  1328. db = b;
  1329. printf("db = %f\n", db);
  1330. }
  1331. int fib(int n)
  1332. {
  1333. if (n <= 2)
  1334. return 1;
  1335. else
  1336. return fib(n-1) + fib(n-2);
  1337. }
  1338. void funcptr_test()
  1339. {
  1340. void (*func)(int);
  1341. int a;
  1342. struct {
  1343. int dummy;
  1344. void (*func)(int);
  1345. } st1;
  1346. printf("funcptr:\n");
  1347. func = &num;
  1348. (*func)(12345);
  1349. func = num;
  1350. a = 1;
  1351. a = 1;
  1352. func(12345);
  1353. /* more complicated pointer computation */
  1354. st1.func = num;
  1355. st1.func(12346);
  1356. printf("sizeof1 = %d\n", sizeof(funcptr_test));
  1357. printf("sizeof2 = %d\n", sizeof funcptr_test);
  1358. printf("sizeof3 = %d\n", sizeof(&funcptr_test));
  1359. printf("sizeof4 = %d\n", sizeof &funcptr_test);
  1360. }
  1361. void lloptest(long long a, long long b)
  1362. {
  1363. unsigned long long ua, ub;
  1364. ua = a;
  1365. ub = b;
  1366. /* arith */
  1367. printf("arith: %Ld %Ld %Ld\n",
  1368. a + b,
  1369. a - b,
  1370. a * b);
  1371. if (b != 0) {
  1372. printf("arith1: %Ld %Ld\n",
  1373. a / b,
  1374. a % b);
  1375. }
  1376. /* binary */
  1377. printf("bin: %Ld %Ld %Ld\n",
  1378. a & b,
  1379. a | b,
  1380. a ^ b);
  1381. /* tests */
  1382. printf("test: %d %d %d %d %d %d\n",
  1383. a == b,
  1384. a != b,
  1385. a < b,
  1386. a > b,
  1387. a >= b,
  1388. a <= b);
  1389. printf("utest: %d %d %d %d %d %d\n",
  1390. ua == ub,
  1391. ua != ub,
  1392. ua < ub,
  1393. ua > ub,
  1394. ua >= ub,
  1395. ua <= ub);
  1396. /* arith2 */
  1397. a++;
  1398. b++;
  1399. printf("arith2: %Ld %Ld\n", a, b);
  1400. printf("arith2: %Ld %Ld\n", a++, b++);
  1401. printf("arith2: %Ld %Ld\n", --a, --b);
  1402. printf("arith2: %Ld %Ld\n", a, b);
  1403. b = ub = 0;
  1404. printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
  1405. }
  1406. void llshift(long long a, int b)
  1407. {
  1408. printf("shift: %Ld %Ld %Ld\n",
  1409. (unsigned long long)a >> b,
  1410. a >> b,
  1411. a << b);
  1412. printf("shiftc: %Ld %Ld %Ld\n",
  1413. (unsigned long long)a >> 3,
  1414. a >> 3,
  1415. a << 3);
  1416. printf("shiftc: %Ld %Ld %Ld\n",
  1417. (unsigned long long)a >> 35,
  1418. a >> 35,
  1419. a << 35);
  1420. }
  1421. void llfloat(void)
  1422. {
  1423. float fa;
  1424. double da;
  1425. long double lda;
  1426. long long la, lb, lc;
  1427. unsigned long long ula, ulb, ulc;
  1428. la = 0x12345678;
  1429. ula = 0x72345678;
  1430. la = (la << 20) | 0x12345;
  1431. ula = ula << 33;
  1432. printf("la=%Ld ula=%Lu\n", la, ula);
  1433. fa = la;
  1434. da = la;
  1435. lda = la;
  1436. printf("lltof: %f %f %Lf\n", fa, da, lda);
  1437. la = fa;
  1438. lb = da;
  1439. lc = lda;
  1440. printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
  1441. fa = ula;
  1442. da = ula;
  1443. lda = ula;
  1444. printf("ulltof: %f %f %Lf\n", fa, da, lda);
  1445. ula = fa;
  1446. ulb = da;
  1447. ulc = lda;
  1448. printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
  1449. }
  1450. long long llfunc1(int a)
  1451. {
  1452. return a * 2;
  1453. }
  1454. struct S {
  1455. int id;
  1456. char item;
  1457. };
  1458. long long int value(struct S *v)
  1459. {
  1460. return ((long long int)v->item);
  1461. }
  1462. void longlong_test(void)
  1463. {
  1464. long long a, b, c;
  1465. int ia;
  1466. unsigned int ua;
  1467. printf("longlong_test:\n");
  1468. printf("sizeof(long long) = %d\n", sizeof(long long));
  1469. ia = -1;
  1470. ua = -2;
  1471. a = ia;
  1472. b = ua;
  1473. printf("%Ld %Ld\n", a, b);
  1474. printf("%Ld %Ld %Ld %Lx\n",
  1475. (long long)1,
  1476. (long long)-2,
  1477. 1LL,
  1478. 0x1234567812345679);
  1479. a = llfunc1(-3);
  1480. printf("%Ld\n", a);
  1481. lloptest(1000, 23);
  1482. lloptest(0xff, 0x1234);
  1483. b = 0x72345678 << 10;
  1484. lloptest(-3, b);
  1485. llshift(0x123, 5);
  1486. llshift(-23, 5);
  1487. b = 0x72345678LL << 10;
  1488. llshift(b, 47);
  1489. llfloat();
  1490. #if 1
  1491. b = 0x12345678;
  1492. a = -1;
  1493. c = a + b;
  1494. printf("%Lx\n", c);
  1495. #endif
  1496. /* long long reg spill test */
  1497. {
  1498. struct S a;
  1499. a.item = 3;
  1500. printf("%lld\n", value(&a));
  1501. }
  1502. lloptest(0x80000000, 0);
  1503. /* another long long spill test */
  1504. {
  1505. long long *p, v;
  1506. v = 1;
  1507. p = &v;
  1508. p[0]++;
  1509. printf("%lld\n", *p);
  1510. }
  1511. a = 68719476720LL;
  1512. b = 4294967295LL;
  1513. printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
  1514. printf("%Ld\n", 0x123456789LLU);
  1515. }
  1516. void manyarg_test(void)
  1517. {
  1518. long double ld = 1234567891234LL;
  1519. printf("manyarg_test:\n");
  1520. printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
  1521. 1, 2, 3, 4, 5, 6, 7, 8,
  1522. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
  1523. printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  1524. "%Ld %Ld %f %f\n",
  1525. 1, 2, 3, 4, 5, 6, 7, 8,
  1526. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  1527. 1234567891234LL, 987654321986LL,
  1528. 42.0, 43.0);
  1529. printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  1530. "%Ld %Ld %f %f\n",
  1531. ld, 1, 2, 3, 4, 5, 6, 7, 8,
  1532. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  1533. 1234567891234LL, 987654321986LL,
  1534. 42.0, 43.0);
  1535. /* XXX: known bug of x86-64 */
  1536. #ifndef __x86_64__
  1537. printf("%d %d %d %d %d %d %d %d %Lf\n",
  1538. 1, 2, 3, 4, 5, 6, 7, 8, ld);
  1539. printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  1540. "%Ld %Ld %f %f %Lf\n",
  1541. 1, 2, 3, 4, 5, 6, 7, 8,
  1542. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  1543. 1234567891234LL, 987654321986LL,
  1544. 42.0, 43.0, ld);
  1545. printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  1546. "%Lf %Ld %Ld %f %f %Lf\n",
  1547. 1, 2, 3, 4, 5, 6, 7, 8,
  1548. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  1549. ld, 1234567891234LL, 987654321986LL,
  1550. 42.0, 43.0, ld);
  1551. #endif
  1552. }
  1553. void vprintf1(const char *fmt, ...)
  1554. {
  1555. va_list ap;
  1556. const char *p;
  1557. int c, i;
  1558. double d;
  1559. long long ll;
  1560. long double ld;
  1561. va_start(ap, fmt);
  1562. p = fmt;
  1563. for(;;) {
  1564. c = *p;
  1565. if (c == '\0')
  1566. break;
  1567. p++;
  1568. if (c == '%') {
  1569. c = *p;
  1570. switch(c) {
  1571. case '\0':
  1572. goto the_end;
  1573. case 'd':
  1574. i = va_arg(ap, int);
  1575. printf("%d", i);
  1576. break;
  1577. case 'f':
  1578. d = va_arg(ap, double);
  1579. printf("%f", d);
  1580. break;
  1581. case 'l':
  1582. ll = va_arg(ap, long long);
  1583. printf("%Ld", ll);
  1584. break;
  1585. case 'F':
  1586. ld = va_arg(ap, long double);
  1587. printf("%Lf", ld);
  1588. break;
  1589. }
  1590. p++;
  1591. } else {
  1592. putchar(c);
  1593. }
  1594. }
  1595. the_end:
  1596. va_end(ap);
  1597. }
  1598. void stdarg_test(void)
  1599. {
  1600. long double ld = 1234567891234LL;
  1601. vprintf1("%d %d %d\n", 1, 2, 3);
  1602. vprintf1("%f %d %f\n", 1.0, 2, 3.0);
  1603. vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
  1604. vprintf1("%F %F %F\n", 1.2L, 2.3L, 3.4L);
  1605. #ifdef __x86_64__
  1606. /* a bug of x86's TCC */
  1607. vprintf1("%d %f %l %F %d %f %l %F\n",
  1608. 1, 1.2, 3L, 4.5L, 6, 7.8, 9L, 0.1L);
  1609. #endif
  1610. vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
  1611. 1, 2, 3, 4, 5, 6, 7, 8,
  1612. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
  1613. vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
  1614. 1, 2, 3, 4, 5, 6, 7, 8,
  1615. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
  1616. vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  1617. "%l %l %f %f\n",
  1618. 1, 2, 3, 4, 5, 6, 7, 8,
  1619. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  1620. 1234567891234LL, 987654321986LL,
  1621. 42.0, 43.0);
  1622. vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  1623. "%l %l %f %f\n",
  1624. ld, 1, 2, 3, 4, 5, 6, 7, 8,
  1625. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  1626. 1234567891234LL, 987654321986LL,
  1627. 42.0, 43.0);
  1628. vprintf1("%d %d %d %d %d %d %d %d %F\n",
  1629. 1, 2, 3, 4, 5, 6, 7, 8, ld);
  1630. vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  1631. "%l %l %f %f %F\n",
  1632. 1, 2, 3, 4, 5, 6, 7, 8,
  1633. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  1634. 1234567891234LL, 987654321986LL,
  1635. 42.0, 43.0, ld);
  1636. vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
  1637. "%F %l %l %f %f %F\n",
  1638. 1, 2, 3, 4, 5, 6, 7, 8,
  1639. 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
  1640. ld, 1234567891234LL, 987654321986LL,
  1641. 42.0, 43.0, ld);
  1642. }
  1643. void whitespace_test(void)
  1644. {
  1645. char *str;
  1646. #if 1
  1647. pri\
  1648. ntf("whitspace:\n");
  1649. #endif
  1650. pf("N=%d\n", 2);
  1651. #ifdef CORRECT_CR_HANDLING
  1652. pri\
  1653. ntf("aaa=%d\n", 3);
  1654. #endif
  1655. pri\
  1656. \
  1657. ntf("min=%d\n", 4);
  1658. #ifdef ACCEPT_CR_IN_STRINGS
  1659. printf("len1=%d\n", strlen("
  1660. "));
  1661. #ifdef CORRECT_CR_HANDLING
  1662. str = "
  1663. ";
  1664. printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
  1665. #endif
  1666. printf("len1=%d\n", strlen(" a
  1667. "));
  1668. #endif /* ACCEPT_CR_IN_STRINGS */
  1669. }
  1670. int reltab[3] = { 1, 2, 3 };
  1671. int *rel1 = &reltab[1];
  1672. int *rel2 = &reltab[2];
  1673. void relocation_test(void)
  1674. {
  1675. printf("*rel1=%d\n", *rel1);
  1676. printf("*rel2=%d\n", *rel2);
  1677. }
  1678. void old_style_f(a,b,c)
  1679. int a, b;
  1680. double c;
  1681. {
  1682. printf("a=%d b=%d b=%f\n", a, b, c);
  1683. }
  1684. void decl_func1(int cmpfn())
  1685. {
  1686. printf("cmpfn=%lx\n", (long)cmpfn);
  1687. }
  1688. void decl_func2(cmpfn)
  1689. int cmpfn();
  1690. {
  1691. printf("cmpfn=%lx\n", (long)cmpfn);
  1692. }
  1693. void old_style_function(void)
  1694. {
  1695. old_style_f((void *)1, 2, 3.0);
  1696. decl_func1(NULL);
  1697. decl_func2(NULL);
  1698. }
  1699. void alloca_test()
  1700. {
  1701. #if defined __i386__ || defined __x86_64__
  1702. char *p = alloca(16);
  1703. strcpy(p,"123456789012345");
  1704. printf("alloca: p is %s\n", p);
  1705. char *demo = "This is only a test.\n";
  1706. /* Test alloca embedded in a larger expression */
  1707. printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
  1708. #endif
  1709. }
  1710. void sizeof_test(void)
  1711. {
  1712. int a;
  1713. int **ptr;
  1714. printf("sizeof(int) = %d\n", sizeof(int));
  1715. printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
  1716. printf("sizeof(long) = %d\n", sizeof(long));
  1717. printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
  1718. printf("sizeof(short) = %d\n", sizeof(short));
  1719. printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
  1720. printf("sizeof(char) = %d\n", sizeof(char));
  1721. printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
  1722. printf("sizeof(func) = %d\n", sizeof sizeof_test());
  1723. a = 1;
  1724. printf("sizeof(a++) = %d\n", sizeof a++);
  1725. printf("a=%d\n", a);
  1726. ptr = NULL;
  1727. printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
  1728. /* some alignof tests */
  1729. printf("__alignof__(int) = %d\n", __alignof__(int));
  1730. printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
  1731. printf("__alignof__(short) = %d\n", __alignof__(short));
  1732. printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
  1733. printf("__alignof__(char) = %d\n", __alignof__(char));
  1734. printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
  1735. printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
  1736. }
  1737. void typeof_test(void)
  1738. {
  1739. double a;
  1740. typeof(a) b;
  1741. typeof(float) c;
  1742. a = 1.5;
  1743. b = 2.5;
  1744. c = 3.5;
  1745. printf("a=%f b=%f c=%f\n", a, b, c);
  1746. }
  1747. void statement_expr_test(void)
  1748. {
  1749. int a, i;
  1750. a = 0;
  1751. for(i=0;i<10;i++) {
  1752. a += 1 +
  1753. ( { int b, j;
  1754. b = 0;
  1755. for(j=0;j<5;j++)
  1756. b += j; b;
  1757. } );
  1758. }
  1759. printf("a=%d\n", a);
  1760. }
  1761. void local_label_test(void)
  1762. {
  1763. int a;
  1764. goto l1;
  1765. l2:
  1766. a = 1 + ({
  1767. __label__ l1, l2, l3, l4;
  1768. goto l1;
  1769. l4:
  1770. printf("aa1\n");
  1771. goto l3;
  1772. l2:
  1773. printf("aa3\n");
  1774. goto l4;
  1775. l1:
  1776. printf("aa2\n");
  1777. goto l2;
  1778. l3:;
  1779. 1;
  1780. });
  1781. printf("a=%d\n", a);
  1782. return;
  1783. l4:
  1784. printf("bb1\n");
  1785. goto l2;
  1786. l1:
  1787. printf("bb2\n");
  1788. goto l4;
  1789. }
  1790. /* inline assembler test */
  1791. #ifdef __i386__
  1792. /* from linux kernel */
  1793. static char * strncat1(char * dest,const char * src,size_t count)
  1794. {
  1795. int d0, d1, d2, d3;
  1796. __asm__ __volatile__(
  1797. "repne\n\t"
  1798. "scasb\n\t"
  1799. "decl %1\n\t"
  1800. "movl %8,%3\n"
  1801. "1:\tdecl %3\n\t"
  1802. "js 2f\n\t"
  1803. "lodsb\n\t"
  1804. "stosb\n\t"
  1805. "testb %%al,%%al\n\t"
  1806. "jne 1b\n"
  1807. "2:\txorl %2,%2\n\t"
  1808. "stosb"
  1809. : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
  1810. : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
  1811. : "memory");
  1812. return dest;
  1813. }
  1814. static inline void * memcpy1(void * to, const void * from, size_t n)
  1815. {
  1816. int d0, d1, d2;
  1817. __asm__ __volatile__(
  1818. "rep ; movsl\n\t"
  1819. "testb $2,%b4\n\t"
  1820. "je 1f\n\t"
  1821. "movsw\n"
  1822. "1:\ttestb $1,%b4\n\t"
  1823. "je 2f\n\t"
  1824. "movsb\n"
  1825. "2:"
  1826. : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  1827. :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
  1828. : "memory");
  1829. return (to);
  1830. }
  1831. static __inline__ void sigaddset1(unsigned int *set, int _sig)
  1832. {
  1833. __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
  1834. }
  1835. static __inline__ void sigdelset1(unsigned int *set, int _sig)
  1836. {
  1837. asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
  1838. }
  1839. static __inline__ __const__ unsigned int swab32(unsigned int x)
  1840. {
  1841. __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
  1842. "rorl $16,%0\n\t" /* swap words */
  1843. "xchgb %b0,%h0" /* swap higher bytes */
  1844. :"=q" (x)
  1845. : "0" (x));
  1846. return x;
  1847. }
  1848. static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
  1849. {
  1850. unsigned long long res;
  1851. __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
  1852. return res;
  1853. }
  1854. static __inline__ unsigned long long inc64(unsigned long long a)
  1855. {
  1856. unsigned long long res;
  1857. __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
  1858. return res;
  1859. }
  1860. unsigned int set;
  1861. void asm_test(void)
  1862. {
  1863. char buf[128];
  1864. unsigned int val;
  1865. printf("inline asm:\n");
  1866. /* test the no operand case */
  1867. asm volatile ("xorl %eax, %eax");
  1868. memcpy1(buf, "hello", 6);
  1869. strncat1(buf, " worldXXXXX", 3);
  1870. printf("%s\n", buf);
  1871. /* 'A' constraint test */
  1872. printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
  1873. printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
  1874. set = 0xff;
  1875. sigdelset1(&set, 2);
  1876. sigaddset1(&set, 16);
  1877. /* NOTE: we test here if C labels are correctly restored after the
  1878. asm statement */
  1879. goto label1;
  1880. label2:
  1881. __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
  1882. #ifdef __GNUC__ // works strange with GCC 4.3
  1883. set=0x1080fd;
  1884. #endif
  1885. printf("set=0x%x\n", set);
  1886. val = 0x01020304;
  1887. printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
  1888. return;
  1889. label1:
  1890. goto label2;
  1891. }
  1892. #else
  1893. void asm_test(void)
  1894. {
  1895. }
  1896. #endif
  1897. #define COMPAT_TYPE(type1, type2) \
  1898. {\
  1899. printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
  1900. __builtin_types_compatible_p (type1, type2));\
  1901. }
  1902. int constant_p_var;
  1903. void builtin_test(void)
  1904. {
  1905. #if GCC_MAJOR >= 3
  1906. COMPAT_TYPE(int, int);
  1907. COMPAT_TYPE(int, unsigned int);
  1908. COMPAT_TYPE(int, char);
  1909. COMPAT_TYPE(int, const int);
  1910. COMPAT_TYPE(int, volatile int);
  1911. COMPAT_TYPE(int *, int *);
  1912. COMPAT_TYPE(int *, void *);
  1913. COMPAT_TYPE(int *, const int *);
  1914. COMPAT_TYPE(char *, unsigned char *);
  1915. /* space is needed because tcc preprocessor introduces a space between each token */
  1916. COMPAT_TYPE(char * *, void *);
  1917. #endif
  1918. printf("res = %d\n", __builtin_constant_p(1));
  1919. printf("res = %d\n", __builtin_constant_p(1 + 2));
  1920. printf("res = %d\n", __builtin_constant_p(&constant_p_var));
  1921. printf("res = %d\n", __builtin_constant_p(constant_p_var));
  1922. }
  1923. void const_func(const int a)
  1924. {
  1925. }
  1926. void const_warn_test(void)
  1927. {
  1928. const_func(1);
  1929. }