+static void CvtMemoryToSegment (ExprDesc* ED)
+/* Convert a memory area into a segment by searching the list of run segments
+ * in this memory area and assigning the nearest one.
+ */
+{
+ /* Get the memory area from the expression */
+ MemoryArea* M = ED->MemRef;
+
+ /* Remember the "nearest" segment and its offset */
+ Segment* Nearest = 0;
+ unsigned long Offs = ULONG_MAX;
+
+ /* Walk over all segments */
+ unsigned I;
+ for (I = 0; I < CollCount (&M->SegList); ++I) {
+
+ /* Get the segment and check if it's a run segment */
+ SegDesc* S = CollAtUnchecked (&M->SegList, I);
+ if (S->Run == M) {
+
+ unsigned long O;
+
+ /* Get the segment from the segment descriptor */
+ Segment* Seg = S->Seg;
+
+ /* Check the PC. */
+ if ((long) Seg->PC <= ED->Val && (O = (ED->Val - Seg->PC)) < Offs) {
+ /* This is the nearest segment for now */
+ Offs = O;
+ Nearest = Seg;
+
+ /* If we found an exact match, don't look further */
+ if (Offs == 0) {
+ break;
+ }
+ }
+ }
+ }
+
+ /* If we found a segment, use it and adjust the offset */
+ if (Nearest) {
+ ED->SegRef = Nearest;
+ ED->MemRef = 0;
+ ED->Val -= Nearest->PC;
+ }
+}
+
+
+