HWA
Bare metal programming with style
hwa_macros.h
Go to the documentation of this file.
1 
2 /* This file is part of the HWA project.
3  * Copyright (c) 2012,2015 Christophe Duparquet.
4  * All rights reserved. Read LICENSE.TXT for details.
5  */
6 
19 #define HW_A0(...) _HW_A0_2(__VA_ARGS__,)
20 #define _HW_A0_2(a0,...) a0
21 
22 #define HW_A0_A1(...) _HW_A0_A1_2(__VA_ARGS__,,)
23 #define _HW_A0_A1_2(a0,a1,...) a0,a1
24 
25 #define HW_T4(...) _HW_T4(__VA_ARGS__,,,,)
26 #define _HW_T4(a0,a1,a2,a3,...) __VA_ARGS__
27 
28 
36 #define HW_A1(...) _HW_A1_2(__VA_ARGS__,,)
37 #define _HW_A1_2(a0,a1,...) a1
38 
39 #define HW_A2(...) _HW_A2_2(__VA_ARGS__,,,)
40 #define _HW_A2_2(a0,a1,a2,...) a2
41 
42 #define HW_A3(...) _HW_A3_2(__VA_ARGS__,,,,)
43 #define _HW_A3_2(a0,a1,a2,a3,...) a3
44 
45 #define HW_A4(...) HW_A0(HW_T4(__VA_ARGS__))
46 #define HW_A5(...) HW_A1(HW_T4(__VA_ARGS__))
47 #define HW_A6(...) HW_A2(HW_T4(__VA_ARGS__))
48 #define HW_A7(...) HW_A3(HW_T4(__VA_ARGS__))
49 
50 #define HW_A8(...) HW_A0(HW_T4(HW_T4(__VA_ARGS__)))
51 #define HW_A9(...) HW_A1(HW_T4(HW_T4(__VA_ARGS__)))
52 #define HW_A10(...) HW_A2(HW_T4(HW_T4(__VA_ARGS__)))
53 #define HW_A11(...) HW_A3(HW_T4(HW_T4(__VA_ARGS__)))
54 
55 
64 #define HW_AD(x,...) _HW_B(_HW_AD1,_hw_par x)(x,__VA_ARGS__)
65 #define _HW_AD11(x,...) (HW_D3(HW_X x)),__VA_ARGS__
66 #define _HW_AD10(x,...) _HW_B(_HW_AD2,hw_class_##x)(x,__VA_ARGS__)
67 #define _HW_AD21(c,o,d,...) (c,o,d),__VA_ARGS__
68 #define _HW_AD20(x,...) (HW_D3(HW_X(x))),__VA_ARGS__
69 
70 
80 #define HW_ANAME(x,...) _HW_B(_HW_ANAME1_,_hw_par x)(x,__VA_ARGS__)
81 #define _HW_ANAME1_1(...) HW_E(HW_EM_TBI)
82 #define _HW_ANAME1_0(x,...) _HW_B(_HW_ANAME2_,hw_class_##x)(x,__VA_ARGS__)
83 #define _HW_ANAME2_1(c,o,d,...) o,__VA_ARGS__
84 #define _HW_ANAME2_0(...) __VA_ARGS__
85 
86 
96 #define HW_B(...) _HW_B0(__VA_ARGS__,,)
97 #define _HW_B0(f,x,...) _HW_B1(f,_hw_par x,0,x)
98 #define _HW_B1(...) _HW_B2(__VA_ARGS__)
99 #define _HW_B2(f,x,y,...) _HW_B2##y(f,__VA_ARGS__,)
100 #define _HW_B21(f,...) f##0
101 #define _HW_B20(f,x,...) _HW_B3(f,_hw_is__##x,0,)
102 #define _HW_B3(...) _HW_B4(__VA_ARGS__)
103 #define _HW_B4(f,x,y,...) f##y
104 
105 
116 #define _HW_B(...) _HW_B20(__VA_ARGS__,,)
117 
118 
128 #define HW_BED(f,x,...) _HW_BED1((f,__VA_ARGS__),x,) ,x)//PUSH
129 #define _HW_BED1(a,x,...) _HW_BED2(a,hw_class_##x)
130 #define _HW_BED2(...) _HW_BED3(__VA_ARGS__,)
131 #define _HW_BED3(a,x,...) _HW_BED4(a,_hw_is__##x,0,)
132 #define _HW_BED4(...) _HW_BED5(__VA_ARGS__)
133 #define _HW_BED5(a,x,r,...) _HW_BED6(r, HW_RP a)
134 #define _HW_BED6(...) _HW_BED7(__VA_ARGS__)
135 #define _HW_BED7(x,f,...) f##x(__VA_ARGS__ //POP
136 
137 
147 #define HW_BEZ(f,x,...) _HW_BEZ1((f,__VA_ARGS__),x,) ,x)//PUSH
148 #define _HW_BEZ1(a,x,...) _HW_BEZ4(a,_hw_is__##x,0,)
149 #define _HW_BEZ4(...) _HW_BEZ5(__VA_ARGS__)
150 #define _HW_BEZ5(a,x,r,...) _HW_BEZ6(r, HW_RP a)
151 #define _HW_BEZ6(...) _HW_BEZ7(__VA_ARGS__)
152 #define _HW_BEZ7(x,f,...) f##x(__VA_ARGS__ //POP
153 
154 
164 #define HW_BP(f,x) _HW_BP0(f,_hw_par x,0)
165 #define _HW_BP0(...) _HW_BP1(__VA_ARGS__)
166 #define _HW_BP1(f,x,r,...) f##r
167 
168 
192 #define HW_BV(f,p,s,...) _HW_B(_HW_BV1,_hw_par s)(f,p,s,__VA_ARGS__)
193 #define _HW_BV11(f,p,s,...) f##_
194 #define _HW_BV10(f,p,s,...) _HW_BV12(f,p,s,_hw_##p##s,__VA_ARGS__)
195 #define _HW_BV12(...) _HW_BV13(__VA_ARGS__)
196 #define _HW_BV13(f,p,s,...) _HW_B(_HW_BV2,__VA_ARGS__)(f,s,__VA_ARGS__)
197 #define _HW_BV20(f,s,r,...) f##0(s,__VA_ARGS__)
198 #define _HW_BV21(f,s,z,...) f##1(__VA_ARGS__)
199 
200 
217 #define HW_BW(...) _HW_BW01(__VA_ARGS__,,,)
218 #define _HW_BW01(f,k,w,...) _HW_BW02(f,k,w,_hw_par w, 0,)
219 #define _HW_BW02(...) _HW_BW03(__VA_ARGS__)
220 #define _HW_BW03(f,k,w,z,x,...) _HW_BW03##x(f,k,w)
221 #define _HW_BW031(f,k,w) f##0
222 #define _HW_BW030(f,k,w) _HW_BW04(f,k,w,_hw_is__##w, 0,)
223 #define _HW_BW04(...) _HW_BW05(__VA_ARGS__)
224 #define _HW_BW05(f,k,w,z,x,...) _HW_BW05##x(f,k,w)
225 #define _HW_BW051(f,k,w) f##0
226 #define _HW_BW050(f,k,w) _HW_BW06(f,k,w,_hw_is_##k##_##w, 0,)
227 #define _HW_BW06(...) _HW_BW07(__VA_ARGS__)
228 /*
229  * Avoid concatenation in case of math expression, e.g.:
230  * HW_BW(func,key,key/x) -> func1/x
231  */
232 //#define _HW_BW07(f,k,w,z,x,...) f##x
233 #define _HW_BW07(f,k,w,z,x,...) _HW_BW08(f,_HW_BW_##x,)
234 #define _HW_BW08(...) _HW_BW09(__VA_ARGS__)
235 #define _HW_BW09(f,v,...) f##v
236 
237 #define _HW_BW_0 0,
238 #define _HW_BW_1 1,
239 
240 
250 #define HW_BX(...) _HW_BX01(__VA_ARGS__,,,)
251 #define _HW_BX01(f,p,x,...) _HW_BX02(f,p,x,_hw_par x, 0,)
252 #define _HW_BX02(...) _HW_BX03(__VA_ARGS__)
253 #define _HW_BX03(f,p,x,z,r,...) _HW_BX03##r(f,p,x)
254 #define _HW_BX031(f,p,x) f##0
255 #define _HW_BX030(f,p,x) _HW_BX04(f,p,x,p##x, 0,)
256 #define _HW_BX04(...) _HW_BX05(__VA_ARGS__)
257 #define _HW_BX05(f,p,x,z,r,...) f##r
258 
259 
269 #define _HW_BZ(...) _HW_BZ1(__VA_ARGS__,,)
270 #define _HW_BZ1(f,x,...) _HW_BZ2(f,x,0,)
271 #define _HW_BZ2(f,x,r,...) f##r
272 
273 
281 #define _HW_COD(o) _HW_COD0(o)
282 #define _HW_COD0(o) _HW_COD1(o,hw_##o)
283 #define _HW_COD1(...) _HW_COD2(__VA_ARGS__)
284 #define _HW_COD2(o,c,...) c,o,(__VA_ARGS__)
285 
286 
295 #define HW_CODG(...) _HW_CODG(__VA_ARGS__)
296 #define _HW_CODG(c,o,d,r) HW_BED(_HW_CODG,hw_##o##_##r,) (c,o,d,r)//PUSH
297 #define _HW_CODG0(...) _HW_CODG01 //POP
298 #define _HW_CODG01(c,o,d,r) HW_BED(_HW_CODG1,hw_##c##_##r,) c,o,d,r)//PUSH
299 #define _HW_CODG11(z,...) _HW_CODG2(__VA_ARGS__, //POP
300 #define _HW_CODG2(c,...) _HW_CODG##c(__VA_ARGS__)
301 #define _HW_CODG_cb1(r,bn,bp,c,o,d,r0) _HW_OXR_ob1_01(o,HW_RP d,r,hw_##c##_##r,bn,bp)
302 
303 
311 #define HW_D3(...) _HW_D3(__VA_ARGS__)
312 #define _HW_D3(c,o,...) c,o,(__VA_ARGS__)
313 
314 
322 #define HW_EAT(...)
323 
324 
333 #define HW_EOL(...) HW_B(_HW_EOL_,__VA_ARGS__)(__VA_ARGS__,)
334 #define _HW_EOL_0(g,...) HW_E(HW_EM_G(g))
335 #define _HW_EOL_1(...)
336 
337 
353 #define HW_F(...) _HW_F(__VA_ARGS__,)
354 #define _HW_F(f,x,...) _HW_B(_HW_F,_hw_par x)(f,x,__VA_ARGS__)
355 /*
356  * x is not (...)
357  */
358 #define _HW_F0(f,x,...) _HW_B(_HW_F0,hw_class_##x)(f,x,__VA_ARGS__)
359 /*
360  * x is a class name
361  */
362 #define _HW_F01(f,x,o,d,...) _HW_F3(f,x,o,HW_RP d,__VA_ARGS__)
363 /*
364  * x is not a class name
365  */
366 #define _HW_F00(f,x,...) _HW_B(_HW_F00,x)(f,x,__VA_ARGS__)
367 /*
368  * x is void (error message or void)
369  */
370 #define _HW_F001(...) _HW_F02(__VA_ARGS__,,,,)
371 #define _HW_F02(f,z,o,e,...) _HW_B(_HW_F02,o)(f,z,o,e,__VA_ARGS__)
372 #define _HW_F021(f,z,o,e,...) f##_E(HW_EM_OM())
373 #define _HW_F020(f,z,o,e,...) f##_E(e)
374 /*
375  * x is not void
376  */
377 #define _HW_F000(f,x,...) _HW_F2(f, HW_X(x),__VA_ARGS__)
378 /*
379  * x is (...)
380  */
381 #define _HW_F1(f,x,...) _HW_F2(f, HW_X x,__VA_ARGS__)
382 #define _HW_F2(...) _HW_F3(__VA_ARGS__)
383 /*
384  * Got object definition in c,o,...
385  */
386 #define _HW_F3(f,c,o,...) _HW_B(_HW_F3,c)(f,c,o)(o,__VA_ARGS__) // PUSH
387 #define _HW_F31(f,c,o) f##_E(HW_EM_O(o)) HW_EAT // POP
388 /* Class method? */
389 #define _HW_F30(f,c,o) _HW_F32(f,c,o,f##_##c,)
390 #define _HW_F32(...) _HW_F33(__VA_ARGS__)
391 #define _HW_F33(f,c,o,x,...) _HW_B(_HW_F4,x)(f,c,o,x,__VA_ARGS__)
392 #define _HW_F41(f,c,o,z,y,...) y // POP
393 /* Object method? */
394 #define _HW_F40(f,c,o,...) _HW_F42(f,c,o,f##_##o,)
395 #define _HW_F42(...) _HW_F43(__VA_ARGS__)
396 #define _HW_F43(f,c,o,x,...) _HW_B(_HW_F5,x)(f,c,o,x,__VA_ARGS__)
397 #define _HW_F51(f,c,o,z,y,...) y // POP
398 /* Global method? */
399 #define _HW_F50(f,c,o,...) _HW_F52(f,c,o,f##_,)
400 #define _HW_F52(...) _HW_F53(__VA_ARGS__)
401 #define _HW_F53(f,c,o,x,...) _HW_B(_HW_F6,x)(f,c,o,x,__VA_ARGS__)
402 #define _HW_F61(f,c,o,z,y,...) y // POP
403 /* Fake object? */
404 #define _HW_F60(f,c,o,...) _HW_B(_HW_F7,_hw_is__fake_##c)(f,c,o)
405 #define _HW_F71(f,c,o)
406 #define _HW_F70(f,c,o) f##_E(HW_EM_FO(f,o)) HW_EAT // POP
407 
408 
414 #define HW_G2(...) _HW_G2_(__VA_ARGS__,,)
415 #define _HW_G2_(a,b,...) a##_##b
416 
417 
423 #define HW_G3(...) _HW_G3_(__VA_ARGS__,,,)
424 #define _HW_G3_(a,b,c,...) a##_##b##_##c
425 
426 
437 #define HW_IS(...) _HW_IS_2(__VA_ARGS__,,)
438 #define _HW_IS_2(x,y,...) HW_A1(_hw_is_##x##_##y,0)
439 
440 
448 #define HW_Q(...) _HW_Q_2(__VA_ARGS__)
449 #define _HW_Q_2(...) #__VA_ARGS__
450 
451 
457 #define HW_SHOW(x) #x: x
458 
459 
460 #if 0 // HW_BD may be used instead
461 
467 #define HW_TD(...) _HW_TD1(__VA_ARGS__,), __VA_ARGS__)
468 #define _HW_TD1(x,...) _HW_TD2(hw_class_##x)
469 #define _HW_TD2(...) _HW_TD3(__VA_ARGS__,)
470 #define _HW_TD3(x,...) _HW_TD4(_hw_is__##x,0,)
471 #define _HW_TD4(...) _HW_TD5(__VA_ARGS__)
472 #define _HW_TD5(x,r,...) r
473 #endif
474 
475 
483 #define HW_TL(...) _HW_TL2(__VA_ARGS__,)
484 #define _HW_TL2(a0,...) __VA_ARGS__
485 
486 
495 #define HW_VF(...) _HW_VF2(__VA_ARGS__)
496 #define _HW_VF2(x,f,a) f(a)
497 
498 
506 #define HW_RP(...) __VA_ARGS__
507 
508 
523 #define hw(...) hwx(hw_,__VA_ARGS__,,)
524 
525 
552 #define hwa(...) hwx(hwa_,__VA_ARGS__,,)
553 
554 
568 /* h: prefix used, "hw" or "hwa"
569  * f: 1st arg, function
570  * c: 2nd arg, class
571  */
572 #define hwx(h,f,x,...) _HW_B(_hwx0_,_hw_par x)(h,f,x,__VA_ARGS__)
573 /*
574  * x is ()
575  */
576 #define _hwx0_1(h,f,x,...) _hwx01(h,f,HW_X x,__VA_ARGS__)
577 #define _hwx01(h,f,x,...) _HW_B(_hwx01,x)(h,f,x,__VA_ARGS__)
578 #define _hwx011(h,f,x,o,e,...) HW_E(e) hw_foo()
579 #define _hwx010 _hwx_0
580 
581 /*
582  * x is not ()
583  */
584 #define _hwx0_0(h,f,x,...) _HW_B(_hwx1_,hw_class_##x)(h,f,x,__VA_ARGS__)
585 /*
586  * x is a class name, expand the definition.
587  */
588 #define _hwx1_1(h,f,x,o,d,...) _hwx_10(h,f,x,o,HW_RP d,__VA_ARGS__)
589 /*
590  * x is not a class name
591  */
592 #define _hwx1_0(h,f,x,...) _HW_B(_hwx10_,x)(h,f,x,__VA_ARGS__)
593 /*
594  * x is void
595  */
596 #define _hwx10_1(h,f,z,...) _HW_B(_hwx101_,__VA_ARGS__)(h,f,z,__VA_ARGS__)
597 #define _hwx101_0(h,f,z,o,e,...) HW_E(e)
598 #define _hwx101_1(h,f,x,...) _hwx_0(h,f,HW_X(x),__VA_ARGS__)
599 /*
600  * x is not void
601  */
602 #define _hwx10_0(h,f,x,...) _hwx_0(h,f,HW_X(x),__VA_ARGS__)
603 #define _hwx_0(...) _hwx_1(__VA_ARGS__)
604 #define _hwx_1(h,f,c,...) _HW_B(_hwx_1,c)(h,f,c,__VA_ARGS__)
605 /*
606  * o is not an object. Look for h_f_o
607  */
608 #define _hwx_11(h,f,c,o,...) _HW_B(_hwx_11,_hw_par o)(h,f,c,o,__VA_ARGS__)
609 #define _hwx_111(h,f,c,o,e,...) _hwx_14(h,e,f,h##f##_,__VA_ARGS__)
610 #define _hwx_110(h,f,c,o,e,...) _hwx_12(h,e,f,h##f##_##o,o,__VA_ARGS__)
611 
612 #define _hwx_12(...) _hwx_13(__VA_ARGS__)
613 #define _hwx_13(h,e,f,x,...) _HW_B(_hwx_13,x)(h,e,f,x,__VA_ARGS__)
614 #define _hwx_131(h,e,f,z,y,...) y(__VA_ARGS__)
615 /*
616  * Look for h_f_
617  */
618 #define _hwx_130(h,e,f,m,...) _hwx_14(h,e,f,h##f##_,__VA_ARGS__)
619 #define _hwx_14(...) _hwx_15(__VA_ARGS__)
620 #define _hwx_15(h,e,f,x,...) _HW_B(_hwx_15,x)(h,e,f,x,__VA_ARGS__)
621 #define _hwx_151(h,f,z,y,...) y(__VA_ARGS__)
622 #define _hwx_150(h,e,...) HW_E(e) hw_foo()
623 /*
624  * 2nd arg is an object of class c
625  */
626 /* Class method? */
627 #define _hwx_10(h,f,c,o,...) _hwx_2(h,f,c,o,h##f##_##c,__VA_ARGS__)
628 #define _hwx_2(...) _hwx_3(__VA_ARGS__)
629 #define _hwx_3(h,f,c,o,x,...) _HW_B(_hwx_3,x)(h,f,c,o,x,__VA_ARGS__)
630 #define _hwx_31(h,f,c,o,z,y,...) y(o,__VA_ARGS__)
631 /* Global method? */
632 #define _hwx_30(h,f,c,o,x,...) _hwx_6(h,f,c,o,h##f,__VA_ARGS__)
633 #define _hwx_6(...) _hwx_7(__VA_ARGS__)
634 #define _hwx_7(h,f,c,o,x,...) _HW_B(_hwx_7,x)(h,f,c,o,x,__VA_ARGS__)
635 #define _hwx_71(h,f,c,o,z,y,...) y(c,o,__VA_ARGS__)
636 /* Fake object? */
637 #define _hwx_70(h,f,c,o,x,...) _HW_B(_hwx_8,_hw_is__fake_##c)(h,f,c,o)
638 #define _hwx_81(...) // Nothing to do, fake is always OK.
639 /*
640  * Error: list valid actions if known.
641  */
642 #define _hwx_80(h,f,c,o) HW_B(_hwx_80,_##h##actions_##c)(h,f,c,o)
643 #define _hwx_800(h,f,c,o) HW_E(HW_EM_OCM(o,c,f)) hw_foo()
644 #define _hwx_801(h,f,c,o) HW_E(HW_EM_AOCL(f,o,c,HW_A1(_##h##actions_##c))) hw_foo()
645 
646 
654 #define _hw(...) _hw_1(__VA_ARGS__,,)
655 #define _hw_1(f,o,...) _hw_2(f,HW_X(o),__VA_ARGS__)
656 #define _hw_2(...) _hw_3(__VA_ARGS__)
657 #define _hw_3(f,c,...) HW_B(_hw_4,c)(f,c,__VA_ARGS__)
658 #define _hw_41(c,o,e,...) HW_E(e) // Error, stop here.
659 #define _hw_40(f,c,...) HW_B(_hw_5,hw_##f##_##c)(f,c,__VA_ARGS__)
660 #define _hw_51(f,c,...) HW_A1(hw_##f##_##c)(__VA_ARGS__)
661 #define _hw_50(f,c,o,...) HW_E_OCM(o,c,hw_##f)
662 
663 
671 #define _hwa(...) _hwa_1(__VA_ARGS__,)
672 #define _hwa_1(f,o,...) _hwa_2(f,HW_X(o),__VA_ARGS__)
673 #define _hwa_2(...) _hwa_3(__VA_ARGS__)
674 #define _hwa_3(f,c,...) HW_B(_hwa_4,c)(f,c,__VA_ARGS__)
675 #define _hwa_41(c,o,e,...) HW_E(e) // Error, stop here.
676 #define _hwa_40(f,c,...) HW_B(_hwa_5,hwa_##f##_##c)(f,c,__VA_ARGS__)
677 #define _hwa_51(f,c,...) HW_A1(hwa_##f##_##c)(__VA_ARGS__)
678 #define _hwa_50(f,c,o,...) HW_E_OCM(o,c,hwa_##f)
679 
680 
689 #define _hw_a(...) _hw_a1(__VA_ARGS__,)
690 #define _hw_a1(f,o,...) _hw_a2(f,HW_X(o),__VA_ARGS__)
691 #define _hw_a2(...) _hw_a3(__VA_ARGS__)
692 #define _hw_a3(f,c,...) HW_B(_hw_a4,c)(f,c,__VA_ARGS__)
693 #define _hw_a41(c,o,e,...) HW_E(e) // Error, stop here.
694 #define _hw_a40(f,c,...) HW_B(_hw_a5,hwa_##f##_##c)(f,c,__VA_ARGS__)
695 #define _hw_a51(f,c,...) HW_A1(hwa_##f##_##c)(__VA_ARGS__)
696 #define _hw_a50(f,c,o,...) HW_E_OCM(o,c,hwa_##f)
697 
698 
711 #define hw_stat_t(object) hw(stat_t,object)
712 
713 
729 #define hw_uint_t(n) _hw_uintt0(n)
730 #define _hw_uintt0(n) HW_BW(_hw_uintt0,8,n)(n)
731 #define _hw_uintt01(n) uint8_t
732 #define _hw_uintt00(n) HW_BW(_hw_uintt1,16,n)(n)
733 #define _hw_uintt11(n) uint16_t
734 #define _hw_uintt10(n) HW_E(HW_EM_XNIL(n,(8,16))) uint8_t
735