(Lispex)sicp.io
5.6 · 컴파일된 변수 접근

어휘 주소는 이름 검색을 두 인덱스로 바꾼다.

컴파일러가 변수를 프레임 깊이와 바인딩 오프셋으로 바꾸면 기계는 런타임 환경에서 값을 직접 가져올 수 있습니다.

생각해 볼 질문

컴파일할 때 환경에 관해 어떤 지식을 런타임 조회 밖으로 옮길 수 있을까요?

  • Represent a lexical address as frame depth and binding offset
  • Select a runtime frame without inspecting variable names
  • Select one value slot inside that frame
  • Connect compile-time name search to runtime direct lookup

The address (depth offset) counts outward through environment frames, then across one frame. lexical-ref follows only those two numbers. The runtime frames contain values rather than name-value pairs, so no assoc or symbol comparison occurs during the fetch.

find-address performs the name search against the compiler environment, whose frames contain variable names. Once it produces (1 0) for x, the runtime can use that address to fetch 42 from matching value frames. The example makes the compiler and machine agree on one frame layout.

리스펙스 · SICP 코드SICP에 필요한 Scheme 호환 문법을 리스펙스 SICP 프로필로 실행합니다.
(begin
  (define (frame-at environment depth)
    (if (= depth 0)
        (car environment)
        (frame-at (cdr environment) (- depth 1))))
  (define (lexical-ref address environment)
    (list-ref (frame-at environment (car address))
              (cadr address)))
  (define environment '((10 20) (30 40 50)))
  (list (lexical-ref '(0 1) environment)
        (lexical-ref '(1 0) environment)
        (lexical-ref '(1 2) environment)))
리스펙스 학습용 런타임리스펙스 SICP 프로필 1.0.0
리스펙스 SICP 런타임 불러오는 중
리스펙스 · SICP 코드UTF-8 438 / 1,048,576바이트
예제
결과
출력
진단
보이는 실행 흐름0 / 0 개의 실행 이벤트
    이 브라우저 결과는 리스펙스 바우치나 권한이 아닙니다.wasm —
    예상 관찰

    The first program returns (20 30 50). The second resolves x to address (1 0), then returns ((1 0) 42).

    실행 흐름에서 볼 점

    In the first run, follow depth recursion separately from list-ref offset selection and confirm that no variable symbol is searched. In the second run, separate the compile-environment name search from the later value-frame lookup.

    직접 해보기

    힌트를 보기 전에 프로그램을 바꿔 보세요.

    Add w after z in the innermost compile-time frame and add 11 to the matching runtime frame. Predict the address and value of w.

    힌트 하나 보기

    The innermost frame has depth 0. Its second slot has offset 1.