c.drv 11.8 KB
Newer Older
1
printer "c"
2

3 4 5
prelude "#include <stdlib.h>"
prelude "#include <stdint.h>"
prelude "#include <stdio.h>"
6
prelude "#include <assert.h>"
7

8 9 10
module ref.Ref

  syntax type ref "%1"
11 12
  syntax val ref "%1"
  syntax val (!_) "%1"
13
  syntax converter (!_) "%1"
14
  syntax val (:=) "%1 = %2"
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
15
  syntax converter contents "%1"
16 17
end

18 19 20 21 22 23
module mach.int.Unsigned

  syntax constant zero_unsigned "0"

end

24 25 26 27
module mach.int.Int32
  syntax val of_int "%1"
  syntax converter of_int "%1"

28
  syntax type int32  "int32_t"
29

30 31
  syntax val (+)     "(%1) + (%2)"
  syntax val (-)     "(%1) - (%2)"
32
  syntax val (-_)    "-(%1)"
33 34 35 36 37 38 39 40
  syntax val (*)     "(%1) * (%2)"
  syntax val (/)     "(%1) / (%2)"
  syntax val (%)     "(%1) % (%2)"
  syntax val (=)     "(%1) == (%2)"
  syntax val (<=)    "(%1) <= (%2)"
  syntax val (<)     "(%1) < (%2)"
  syntax val (>=)    "(%1) >= (%2)"
  syntax val (>)     "(%1) > (%2)"
41 42
end

Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
43 44 45 46 47 48 49 50 51 52 53 54 55
module mach.int.UInt32Gen

  syntax type uint32 "uint32_t"

  syntax converter max_uint32 "0xffffffff"
  syntax converter length "32"

end

module mach.int.UInt32

  syntax converter of_int "%1U"

56 57 58 59 60 61 62 63 64 65 66

  syntax val (+)     "(%1) + (%2)"
  syntax val (-)     "(%1) - (%2)"
  syntax val (*)     "(%1) * (%2)"
  syntax val (/)     "(%1) / (%2)"
  syntax val (%)     "(%1) % (%2)"
  syntax val (=)     "(%1) == (%2)"
  syntax val (<=)    "(%1) <= (%2)"
  syntax val (<)     "(%1) < (%2)"
  syntax val (>=)    "(%1) >= (%2)"
  syntax val (>)     "(%1) > (%2)"
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
67 68 69

end

70
module mach.int.UInt32GMP
71

72 73 74 75 76
  prelude

"
#define LOW_MASK 0x00000000FFFFFFFFULL

77 78 79 80 81
struct __add32_with_carry_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

82
struct __add32_with_carry_result add32_with_carry(uint32_t x, uint32_t y, uint32_t c)
83
{
84
  struct __add32_with_carry_result result;
85
  uint64_t r = (uint64_t)x + (uint64_t)y + (uint64_t) c;
86 87 88
  result.__field_0 = (uint32_t)(r & LOW_MASK);
  result.__field_1 = (uint32_t)(r >> 32);
  return result;
89 90
}

91 92 93 94 95
struct __sub32_with_borrow_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

96
struct __sub32_with_borrow_result sub32_with_borrow(uint32_t x, uint32_t y, uint32_t b)
97
{
98
  struct __sub32_with_borrow_result result;
99
  uint64_t r = (uint64_t)x - (uint64_t)y - (uint64_t) b;
100 101 102
  result.__field_0 = (uint32_t)(r & LOW_MASK);
  result.__field_1 = (uint32_t)(r >> 63);
  return result;
103 104
}

105 106 107 108 109
struct __mul32_double_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

110
struct __mul32_double_result mul32_double(uint32_t x, uint32_t y)
111
{
112
  struct __mul32_double_result result;
113
  uint64_t r = (uint64_t)x * (uint64_t)y;
114 115 116
  result.__field_0 = (uint32_t)(r & LOW_MASK);
  result.__field_1 = (uint32_t)(r >> 32);
  return result;
117 118
}

119 120 121 122 123
struct __add32_3_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

124
struct __add32_3_result add32_3(uint32_t x, uint32_t y, uint32_t z)
125
{
126
  struct __add32_3_result result;
127
  uint64_t r = (uint64_t)x + (uint64_t)y + (uint64_t) z;
128 129 130
  result.__field_0 = (uint32_t)(r & LOW_MASK);
  result.__field_1 = (uint32_t)(r >> 32);
  return result;
131 132
}

133 134 135 136 137
struct __lsld32_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

138
struct __lsld32_result lsld32(uint32_t x, uint32_t cnt)
139
{
140
  struct __lsld32_result result;
141
  uint64_t r = (uint64_t)x << cnt;
142 143 144
  result.__field_0 = (uint32_t)(r & LOW_MASK);
  result.__field_1 = (uint32_t)(r >> 32);
  return result;
145
}
146 147 148 149 150 151 152 153 154
"

  interface
"
struct __add32_with_carry_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

155
struct __add32_with_carry_result add32_with_carry(uint32_t x, uint32_t y, uint32_t c);
156 157 158 159 160 161

struct __sub32_with_borrow_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

162
struct __sub32_with_borrow_result sub32_with_borrow(uint32_t x, uint32_t y, uint32_t b);
163 164 165 166 167 168

struct __mul32_double_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

169
struct __mul32_double_result mul32_double(uint32_t x, uint32_t y);
170 171 172 173 174 175

struct __add32_3_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

176
struct __add32_3_result add32_3(uint32_t x, uint32_t y, uint32_t z);
177 178 179 180 181 182

struct __lsld32_result
{ uint32_t __field_0;
  uint32_t __field_1;
};

183
struct __lsld32_result lsld32(uint32_t x, uint32_t cnt);
184 185
"

186
  syntax converter of_int "%1U"
187

188 189 190 191 192 193 194 195 196 197
  syntax val (+)     "(%1) + (%2)"
  syntax val (-)     "(%1) - (%2)"
  syntax val (*)     "(%1) * (%2)"
  syntax val (/)     "(%1) / (%2)"
  syntax val (%)     "(%1) % (%2)"
  syntax val (=)     "(%1) == (%2)"
  syntax val (<=)    "(%1) <= (%2)"
  syntax val (<)     "(%1) < (%2)"
  syntax val (>=)    "(%1) >= (%2)"
  syntax val (>)     "(%1) > (%2)"
198

Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
199 200 201 202 203 204
  syntax val add_with_carry "add32_with_carry"
  syntax val sub_with_borrow "sub32_with_borrow"
  syntax val mul_double "mul32_double"
  syntax val add3 "add32_3"
  syntax val lsld "lsld32"

205 206 207
  syntax val add_mod "(%1) + (%2)"
  syntax val sub_mod "(%1) - (%2)"
  syntax val mul_mod "(%1) * (%2)"
208

209
  syntax val div2by1
210
         "(uint32_t)((((uint64_t)%1) | (((uint64_t)%2) << 32))/(uint64_t)(%3))"
211

212 213
  syntax val lsl "(%1) << (%2)"
  syntax val lsr "(%1) >> (%2)"
214

215
  syntax val is_msb_set "(%1) & 0x80000000U"
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
216

217 218
  syntax val count_leading_zeros "__builtin_clz(%1)"

219 220 221 222 223 224 225 226
  syntax val of_int32 "(uint32_t)(%1)"


end

module mach.int.Int64
  syntax val of_int "%1"
  syntax converter of_int "%1"
227

228
  syntax type int64  "int64_t"
229

230 231 232 233 234 235 236 237 238 239 240
  syntax val (+)     "(%1) + (%2)"
  syntax val (-)     "(%1) - (%2)"
  syntax val (-_)    "-(%1)"
  syntax val (*)     "(%1) * (%2)"
  syntax val (/)     "(%1) / (%2)"
  syntax val (%)     "(%1) % (%2)"
  syntax val (=)     "(%1) == (%2)"
  syntax val (<=)    "(%1) <= (%2)"
  syntax val (<)     "(%1) < (%2)"
  syntax val (>=)    "(%1) >= (%2)"
  syntax val (>)     "(%1) > (%2)"
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
end

module mach.int.UInt64Gen

  syntax type uint64 "uint64_t"

  syntax converter max_uint64 "0xffffffffffffffff"
  syntax converter length "64"

end

module mach.int.UInt64

  syntax converter of_int "%1ULL"

256 257 258 259 260 261 262 263 264 265 266
  syntax val (+)     "(%1) + (%2)"
  syntax val (-)     "(%1) - (%2)"
  syntax val (-_)    "-(%1)"
  syntax val (*)     "(%1) * (%2)"
  syntax val (/)     "(%1) / (%2)"
  syntax val (%)     "(%1) % (%2)"
  syntax val (=)     "(%1) == (%2)"
  syntax val (<=)    "(%1) <= (%2)"
  syntax val (<)     "(%1) < (%2)"
  syntax val (>=)    "(%1) >= (%2)"
  syntax val (>)     "(%1) > (%2)"
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
267

268 269
end

270
module mach.int.UInt64GMP
271 272 273 274

  prelude

"
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
275 276 277 278 279 280
#include \"gmp.h\"
#include \"gmp-impl.h\"
#include \"longlong.h\"

#undef invert_limb

281 282 283 284 285
struct __add64_with_carry_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

286
struct __add64_with_carry_result
287
add64_with_carry(uint64_t x, uint64_t y, uint64_t c)
288
{
289
  struct __add64_with_carry_result result;
290
  uint64_t r = x + y + c;
291 292 293 294
  result.__field_0 = r;
  if (r == x) result.__field_1 = c;
  else result.__field_1 = (r < x);
  return result;
295 296
}

297 298 299 300 301
struct __add64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

302
struct __add64_double_result
303 304 305 306 307 308 309
add64_double(uint64_t a1, uint64_t a0, uint64_t b1, uint64_t b0)
{
  struct __add64_double_result result;
  add_ssaaaa(result.__field_0, result.__field_1, a1, a0, b1, b0);
  return result;
}

310 311 312 313 314
struct __sub64_with_borrow_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

315
struct __sub64_with_borrow_result
316
sub64_with_borrow(uint64_t x, uint64_t y, uint64_t b)
317
{
318
  struct __sub64_with_borrow_result result;
319
  uint64_t r = x - y - b;
320 321 322 323 324
  result.__field_0 = r;
  if (r > x) result.__field_1 = 1;
  else if (r == x) result.__field_1 = b;
  else result.__field_1 = 0;
  return result;
325 326
}

327 328 329 330 331
struct __sub64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

332
struct __sub64_double_result
333 334 335 336 337 338 339
sub64_double(uint64_t a1, uint64_t a0, uint64_t b1, uint64_t b0)
{
  struct __sub64_double_result result;
  sub_ddmmss(result.__field_0, result.__field_1, a1, a0, b1, b0);
  return result;
}

340 341 342 343 344
struct __mul64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

345
struct __mul64_double_result mul64_double(uint64_t x, uint64_t y)
346
{
347 348 349
  struct __mul64_double_result result;
  umul_ppmm(result.__field_1,result.__field_0,x,y);
  return result;
350 351
}

352
uint64_t div64_2by1(uint64_t ul, uint64_t uh, uint64_t d)
353
{
354 355
  uint64_t q;
  uint64_t _dummy __attribute__((unused));
356
  udiv_qrnnd(q,_dummy,uh,ul,d);
357 358 359
  return q;
}

360 361 362 363 364
struct __add64_3_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

365
struct __add64_3_result add64_3(uint64_t x, uint64_t y, uint64_t z)
366
{
367
  struct __add64_3_result result;
368
  uint64_t r, c1, c2;
369 370 371 372
  r = x + y;
  c1 = r < y;
  r += z;
  c2 = r < z;
373 374 375
  result.__field_1 = c1 + c2;
  result.__field_0 = r;
  return result;
376 377
}

378 379 380 381
struct __lsld64_result
{ uint64_t __field_0;
  uint64_t __field_1;
};
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
382

383
struct __lsld64_result lsld64(uint64_t x, uint64_t cnt)
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
384
{
385 386 387 388
  struct __lsld64_result result;
  result.__field_1 = x >> (64 - cnt);
  result.__field_0 = x << cnt;
  return result;
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
389
}
390
"
391

392 393 394 395 396 397 398 399 400 401 402 403 404
  interface
"
#include \"gmp.h\"
#include \"gmp-impl.h\"
#include \"longlong.h\"

#undef invert_limb

struct __add64_with_carry_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

405
struct __add64_with_carry_result
406 407 408 409 410 411 412
add64_with_carry(uint64_t x, uint64_t y, uint64_t c);

struct __add64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

413
struct __add64_double_result
414 415 416 417 418 419 420
add64_double(uint64_t a1, uint64_t a0, uint64_t b1, uint64_t b0);

struct __sub64_with_borrow_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

421
struct __sub64_with_borrow_result
422 423 424 425 426 427 428
sub64_with_borrow(uint64_t x, uint64_t y, uint64_t b);

struct __sub64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

429
struct __sub64_double_result
430 431 432 433 434 435 436
sub64_double(uint64_t a1, uint64_t a0, uint64_t b1, uint64_t b0);

struct __mul64_double_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

437
struct __mul64_double_result mul64_double(uint64_t x, uint64_t y);
438

439
uint64_t div64_2by1(uint64_t ul, uint64_t uh, uint64_t d);
440 441 442 443 444 445

struct __add64_3_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

446
struct __add64_3_result add64_3(uint64_t x, uint64_t y, uint64_t z);
447 448 449 450 451 452

struct __lsld64_result
{ uint64_t __field_0;
  uint64_t __field_1;
};

453
struct __lsld64_result lsld64(uint64_t x, uint64_t cnt);
454
"
455 456
  syntax converter of_int "%1ULL"

457 458 459 460 461 462 463 464 465 466
  syntax val (+)     "(%1) + (%2)"
  syntax val (-)     "(%1) - (%2)"
  syntax val (*)     "(%1) * (%2)"
  syntax val (/)     "(%1) / (%2)"
  syntax val (%)     "(%1) % (%2)"
  syntax val (=)     "(%1) == (%2)"
  syntax val (<=)    "(%1) <= (%2)"
  syntax val (<)     "(%1) < (%2)"
  syntax val (>=)    "(%1) >= (%2)"
  syntax val (>)     "(%1) > (%2)"
467

Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
468
  syntax val add_with_carry "add64_with_carry"
469 470 471 472 473 474
  syntax val add_double "add64_double"
  syntax val add_double_nc "add64_double"
  syntax val add_double_gc "add64_double"
  syntax val sub_double "sub64_double"
  syntax val sub_double_nb "sub64_double"
  syntax val sub_double_gb "sub64_double"
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
475 476 477 478 479 480
  syntax val sub_with_borrow "sub64_with_borrow"
  syntax val mul_double "mul64_double"
  syntax val div2by1 "div64_2by1"
  syntax val add3 "add64_3"
  syntax val lsld "lsld64"

481 482 483
  syntax val add_mod "(%1) + (%2)"
  syntax val sub_mod "(%1) - (%2)"
  syntax val mul_mod "(%1) * (%2)"
484

485 486
  syntax val lsl "(%1) << (%2)"
  syntax val lsr "(%1) >> (%2)"
487

488
  syntax val is_msb_set "(%1) & 0x8000000000000000ULL"
489

490 491
  syntax val count_leading_zeros "__builtin_clzll(%1)"

492 493 494
  syntax val of_int32 "(uint64_t)(%1)"
  syntax val to_int64 "(int64_t)(%1)"
  syntax val of_int64 "(uint64_t)(%1)"
495 496 497

end

498 499


500 501
module mach.c.C

502
  prelude "#include <assert.h>"
503 504
  prelude "#define IGNORE2(x,y) do {} while (0)"
  interface "#define IGNORE2(x,y) do {} while (0)"
505

506
  syntax type ptr "%1 *"
507
  syntax type bool "int" (* ? *)
508

Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
509
  syntax val malloc "malloc((%1) * sizeof(%v0))"
510
  syntax val free   "free(%1)"
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
511
  syntax val realloc "realloc(%1, (%2) * sizeof(%v0))"
512

Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
513
  (* syntax val is_null "(%1) == NULL" *)
514
  syntax val is_not_null "(%1) != NULL"
515 516
  syntax val null "NULL"

Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
517
  syntax val incr "%1+(%2)"
518

519
  syntax val get "*(%1)"
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
520
  syntax val get_ofs "*(%1+(%2))"
521

522
  syntax val set "*(%1) = %2"
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
523
  syntax val set_ofs "*(%1+(%2)) = %3"
524

525
  syntax val incr_split "%1+(%2)"
526 527 528
  syntax val decr_split "%1-(%2)"
  syntax val join "IGNORE2"
  syntax val join_r "IGNORE2"
529

530 531
  syntax val c_assert "assert ( %1 )"

532 533
  syntax val print_space "printf(\" \")"
  syntax val print_newline "printf(\"\\n\")"
534
  syntax val print_uint32 "printf(\"%#010x\",%1)"
535

536
end