| 1 | /* Copyright (C) 2005-2007 Henry Cejtin, Matthew Fluet, Suresh |
| 2 | * Jagannathan, and Stephen Weeks. |
| 3 | * |
| 4 | * MLton is released under a BSD-style license. |
| 5 | * See the file MLton-LICENSE for details. |
| 6 | */ |
| 7 | |
| 8 | /* isObjptr returns true if p looks like an object pointer. */ |
| 9 | bool isObjptr (objptr p) { |
| 10 | unsigned int shift = GC_MODEL_MINALIGN_SHIFT - GC_MODEL_OBJPTR_SHIFT; |
| 11 | objptr mask = ~((~((objptr)0)) << shift); |
| 12 | return (0 == (p & mask)); |
| 13 | } |
| 14 | |
| 15 | pointer objptrToPointer (objptr O, pointer B) { |
| 16 | uintptr_t O_ = (uintptr_t)O; |
| 17 | uintptr_t B_; |
| 18 | unsigned int S_ = GC_MODEL_OBJPTR_SHIFT; |
| 19 | uintptr_t P_; |
| 20 | pointer P; |
| 21 | |
| 22 | if (GC_MODEL_OBJPTR_BASE) { |
| 23 | B_ = (uintptr_t)B; |
| 24 | } else { |
| 25 | B_ = 0; |
| 26 | } |
| 27 | |
| 28 | P_ = ((O_ << S_) + B_); |
| 29 | P = (pointer)P_; |
| 30 | if (DEBUG_OBJPTR) |
| 31 | fprintf (stderr, "objptrToPointer ("FMTOBJPTR") = "FMTPTR"\n", O, (uintptr_t)P); |
| 32 | |
| 33 | return P; |
| 34 | } |
| 35 | |
| 36 | objptr pointerToObjptr (pointer P, pointer B) { |
| 37 | uintptr_t P_ = (uintptr_t)P; |
| 38 | uintptr_t B_; |
| 39 | unsigned int S_ = GC_MODEL_OBJPTR_SHIFT; |
| 40 | uintptr_t O_; |
| 41 | objptr O; |
| 42 | |
| 43 | if (GC_MODEL_OBJPTR_BASE) { |
| 44 | B_ = (uintptr_t)B; |
| 45 | } else { |
| 46 | B_ = 0; |
| 47 | } |
| 48 | |
| 49 | O_ = ((P_ - B_) >> S_); |
| 50 | O = (objptr)O_; |
| 51 | if (DEBUG_OBJPTR) |
| 52 | fprintf (stderr, "pointerToObjptr ("FMTPTR") = "FMTOBJPTR"\n", (uintptr_t)P, O); |
| 53 | |
| 54 | return O; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Note that by indirectly fetching and storing object pointers, the |
| 59 | * following functions admit implementations that behave according to |
| 60 | * model characteristics determined at runtime. Hence, by making |
| 61 | * exclusive use of these functions (and adding a GC_state->model |
| 62 | * field set by the compiled program), we may be able to implement the |
| 63 | * runtime in a manner which is agnostic to the actual objptr |
| 64 | * representation. |
| 65 | */ |
| 66 | pointer fetchObjptrToPointer (pointer OP, pointer B) { |
| 67 | return objptrToPointer (*((objptr*)OP), B); |
| 68 | } |
| 69 | void storeObjptrFromPointer (pointer OP, pointer P, pointer B) { |
| 70 | *((objptr*)OP) = pointerToObjptr (P, B); |
| 71 | } |