DEADSOFTWARE

Переделано присваивание строк и добавлены процедуры COPY, ASSERT и HALT
[dsw-obn.git] / src / backends / jvm / generator-jvm-abi.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <string.h>
7 #include <assert.h>
9 #include <gc.h>
11 #include "../../../include/oberon.h"
12 #include "../../oberon-internals.h"
13 #include "generator-jvm.h"
14 #include "generator-jvm-abi.h"
15 #include "generator-jvm-asm.h"
16 #include "generator-jvm-basic.h"
18 void
19 jvm_generate_ldst_prepare(gen_proc_t * p, gen_var_t * v)
20 {
21 char * desc = v -> type -> desc;
22 char * full_name = v -> full_name;
23 switch(v -> storage)
24 {
25 case JVM_STORAGE_REGISTER:
26 case JVM_STORAGE_STATIC:
27 case JVM_STORAGE_FIELD:
28 case JVM_STORAGE_PARAM:
29 break;
30 case JVM_STORAGE_REGISTER_VAR:
31 case JVM_STORAGE_PARAM_VAR:
32 jvm_generate(p, 0, 1, "aload %i", v -> reg);
33 jvm_generate(p, 0, 1, "iconst_0");
34 break;
35 case JVM_STORAGE_STATIC_VAR:
36 jvm_generate(p, 0, 1, "getstatic %s [%s", full_name, desc);
37 jvm_generate(p, 0, 1, "iconst_0");
38 break;
39 case JVM_STORAGE_FIELD_VAR:
40 jvm_generate(p, 1, 1, "getfield %s [%s", full_name, desc);
41 jvm_generate(p, 0, 1, "iconst_0");
42 break;
43 case JVM_STORAGE_PARAM_VARPTR:
44 jvm_generate(p, 0, 1, "aload %i", v -> reg);
45 jvm_generate(p, 0, 1, "iload %i", v -> reg + 1);
46 break;
47 case JVM_STORAGE_FRAME:
48 case JVM_STORAGE_FRAME_PARAM:
49 jvm_generate(p, 0, 1, "aload %i", p -> reg_frame[v -> level]);
50 break;
51 case JVM_STORAGE_FRAME_VAR:
52 case JVM_STORAGE_FRAME_PARAM_VAR:
53 jvm_generate(p, 0, 1, "aload %i", p -> reg_frame[v -> level]);
54 jvm_generate(p, 1, 1, "getfield %s [%s", full_name, desc);
55 jvm_generate(p, 0, 1, "iconst_0");
56 break;
57 case JVM_STORAGE_FRAME_PARAM_VARPTR:
58 jvm_generate(p, 0, 1, "aload %i", p -> reg_frame[v -> level]);
59 jvm_generate(p, 1, 1, "getfield %s [%s", full_name, desc);
60 jvm_generate(p, 0, 1, "aload %i", p -> reg_frame[v -> level]);
61 jvm_generate(p, 1, 1, "getfield %s$offset I", full_name);
62 break;
63 default:
64 gen_error("jvm_abi_obn_generate_ldst_prepare: wat %i", v -> storage);
65 }
66 }
68 void
69 jvm_generate_load(gen_proc_t * p, gen_var_t * src)
70 {
71 char prefix = src -> type -> prefix;
72 char postfix = src -> type -> postfix;
73 int cell_size = src -> type -> cell_size;
74 char * full_name = src -> full_name;
75 char * desc = src -> type -> desc;
77 switch(src -> storage)
78 {
79 case JVM_STORAGE_REGISTER:
80 case JVM_STORAGE_PARAM:
81 jvm_generate(p, 0, cell_size, "%cload %i", prefix, src -> reg);
82 break;
83 case JVM_STORAGE_STATIC:
84 jvm_generate(p, 0, cell_size, "getstatic %s %s", full_name, desc);
85 break;
86 case JVM_STORAGE_FIELD:
87 jvm_generate(p, 0, cell_size, "getfield %s %s", full_name, desc);
88 break;
89 case JVM_STORAGE_REGISTER_VAR:
90 case JVM_STORAGE_STATIC_VAR:
91 case JVM_STORAGE_FIELD_VAR:
92 case JVM_STORAGE_PARAM_VAR:
93 case JVM_STORAGE_PARAM_VARPTR:
94 case JVM_STORAGE_FRAME_VAR:
95 case JVM_STORAGE_FRAME_PARAM_VAR:
96 case JVM_STORAGE_FRAME_PARAM_VARPTR:
97 jvm_generate_ldst_prepare(p, src);
98 jvm_generate(p, 1 + 1, cell_size, "%caload", postfix);
99 break;
100 case JVM_STORAGE_FRAME:
101 case JVM_STORAGE_FRAME_PARAM:
102 jvm_generate_ldst_prepare(p, src);
103 jvm_generate(p, 1, cell_size, "getfield %s %s", full_name, desc);
104 break;
105 default:
106 gen_error("jvm_generate_load: unknow storage type %i", src -> storage);
107 break;
111 void
112 jvm_generate_store(gen_proc_t * p, gen_var_t * dst)
114 char prefix = dst -> type -> prefix;
115 char postfix = dst -> type -> postfix;
116 int cell_size = dst -> type -> cell_size;
117 char * full_name = dst -> full_name;
118 char * desc = dst -> type -> desc;
120 switch(dst -> storage)
122 case JVM_STORAGE_REGISTER:
123 case JVM_STORAGE_PARAM:
124 jvm_generate(p, cell_size, 0, "%cstore %i", prefix, dst -> reg);
125 break;
126 case JVM_STORAGE_STATIC:
127 jvm_generate(p, 0, cell_size, "putstatic %s %s", full_name, desc);
128 break;
129 case JVM_STORAGE_FIELD:
130 jvm_generate(p, 0, cell_size, "putfield %s %s", full_name, desc);
131 break;
132 case JVM_STORAGE_REGISTER_VAR:
133 case JVM_STORAGE_STATIC_VAR:
134 case JVM_STORAGE_FIELD_VAR:
135 case JVM_STORAGE_PARAM_VAR:
136 case JVM_STORAGE_PARAM_VARPTR:
137 case JVM_STORAGE_FRAME_VAR:
138 case JVM_STORAGE_FRAME_PARAM_VAR:
139 case JVM_STORAGE_FRAME_PARAM_VARPTR:
140 jvm_generate(p, 1 + 1 + cell_size, 0, "%castore", postfix);
141 break;
142 case JVM_STORAGE_FRAME:
143 case JVM_STORAGE_FRAME_PARAM:
144 jvm_generate(p, 0, cell_size, "putfield %s %s", full_name, desc);
145 break;
146 default:
147 gen_error("jvm_generate_store: unknow storage type %i", dst -> storage);
148 break;
152 static gen_var_t *
153 jvm_new_var()
155 gen_var_t * v = GC_MALLOC(sizeof *v);
156 memset(v, 0, sizeof *v);
157 return v;
160 gen_var_t *
161 jvm_create_class_var(struct gen_class * class, enum gen_storage storage, char * name, gen_type_t * t)
163 gen_var_t * v = jvm_new_var();
164 v -> storage = storage;
165 v -> name = new_string(name);
166 v -> type = t;
167 v -> class = class;
169 assert(class -> p == NULL);
170 switch(storage)
172 case JVM_STORAGE_STATIC:
173 case JVM_STORAGE_FIELD:
174 case JVM_STORAGE_STATIC_VAR:
175 case JVM_STORAGE_FIELD_VAR:
176 v -> full_name = new_string("%s/%s", class -> full_name, name);
177 break;
178 default:
179 gen_error("jvm_create_class_var: invalid storage %i", storage);
180 break;
183 return v;
186 gen_var_t *
187 jvm_create_function_var(gen_proc_t * p, enum gen_storage storage, char * name, gen_type_t * t)
189 gen_var_t * v = jvm_new_var();
190 v -> storage = storage;
191 v -> name = new_string(name);
192 v -> type = t;
193 v -> p = p;
194 v -> level = p -> level;
196 switch(storage)
198 case JVM_STORAGE_REGISTER:
199 case JVM_STORAGE_PARAM:
200 case JVM_STORAGE_PARAM_VAR:
201 v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide);
202 break;
203 case JVM_STORAGE_PARAM_VARPTR:
204 /* Содержит пару массив+индекс */
205 v -> reg = jvm_alloc_register_untyped(p -> rf, true);
206 break;
207 case JVM_STORAGE_REGISTER_VAR:
208 /* Содержит массив */
209 v -> reg = jvm_alloc_register_untyped(p -> rf, false);
210 break;
211 case JVM_STORAGE_FRAME:
212 case JVM_STORAGE_FRAME_VAR:
213 v -> full_name = new_string("%s$FRAME/%s", p -> full_name, name);
214 break;
215 case JVM_STORAGE_FRAME_PARAM:
216 case JVM_STORAGE_FRAME_PARAM_VAR:
217 v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide);
218 v -> full_name = new_string("%s$FRAME/%s", p -> full_name, name);
219 break;
220 case JVM_STORAGE_FRAME_PARAM_VARPTR:
221 /* Содержит пару массив+индекс */
222 v -> reg = jvm_alloc_register_untyped(p -> rf, true);
223 v -> full_name = new_string("%s$FRAME/%s", p -> full_name, name);
224 break;
225 default:
226 gen_error("jvm_create_class_var: invalid storage %i", storage);
227 break;
230 return v;
233 gen_var_t *
234 jvm_create_function_temp_var(gen_proc_t * p, gen_type_t * t)
236 gen_var_t * v = jvm_new_var();
237 v -> storage = JVM_STORAGE_REGISTER;
238 v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide);
239 v -> name = new_string("$TEMP_%i$", v -> reg);
240 v -> type = t;
241 v -> p = p;
242 v -> level = p -> level;
243 return v;
246 void
247 jvm_generate_var(gen_var_t * v)
249 enum gen_storage storage = v -> storage;
250 int reg = v -> reg;
251 char * name = v -> name;
252 char * desc = v -> type -> desc;
253 gen_proc_t * p = v -> p;
255 switch(v -> storage)
257 case JVM_STORAGE_REGISTER:
258 case JVM_STORAGE_PARAM:
259 case JVM_STORAGE_PARAM_VAR:
260 jvm_generate(p, 0, 0, ".var %i is %s %s from start to end", reg, name, desc);
261 break;
262 case JVM_STORAGE_REGISTER_VAR:
263 jvm_generate(p, 0, 0, ".var %i is %s [%s from start to end", reg, name, desc);
264 break;
265 case JVM_STORAGE_PARAM_VARPTR:
266 jvm_generate(p, 0, 0, ".var %i is %s [%s from start to end", reg, name, desc);
267 jvm_generate(p, 0, 0, ".var %i is %s$offset I from start to end", reg + 1, name);
268 break;
269 case JVM_STORAGE_STATIC:
270 fprintf(v -> class -> fp, ".field public static %s %s\n\n", name, desc);
271 break;
272 case JVM_STORAGE_STATIC_VAR:
273 fprintf(v -> class -> fp, ".field public static %s [%s\n\n", name, desc);
274 break;
275 case JVM_STORAGE_FIELD:
276 fprintf(v -> class -> fp, ".field public %s %s\n\n", name, desc);
277 break;
278 case JVM_STORAGE_FIELD_VAR:
279 fprintf(v -> class -> fp, ".field public %s [%s\n\n", name, desc);
280 break;
281 case JVM_STORAGE_FRAME:
282 case JVM_STORAGE_FRAME_VAR:
283 case JVM_STORAGE_FRAME_PARAM:
284 case JVM_STORAGE_FRAME_PARAM_VAR:
285 case JVM_STORAGE_FRAME_PARAM_VARPTR:
286 /* См. ниже jvm_generate_frame_class() */
287 break;
288 default:
289 gen_error("jvm_generate_var: invalid storage %i", storage);
290 break;
294 static void
295 jvm_generate_param_initialization(gen_proc_t * p, gen_var_t * v)
297 int old_reg = v -> reg;
298 int cell_size = v -> type -> cell_size;
299 char prefix = v -> type -> prefix;
300 char postfix = v -> type -> postfix;
301 char * name = v -> name;
302 char * desc = v -> type -> desc;
304 v -> reg = jvm_alloc_register_untyped(p -> rf, false);
305 jvm_generate(p, 0, 0, ".var %i is $%s [%s from start to end", v -> reg, name, desc);
307 jvm_generate(p, 0, 1, "iconst_1");
308 jvm_generate(p, 1, 1, "multianewarray [%s 1", desc);
309 jvm_generate(p, 1, 2, "dup");
310 jvm_generate(p, 1, 0, "astore %i", v -> reg);
312 jvm_generate(p, 0, 1, "iconst_0");
313 jvm_generate(p, 0, cell_size, "%cload %i", prefix, old_reg);
314 jvm_generate(p, 1 + 1 + cell_size, 0, "%castore", postfix);
317 void
318 jvm_generate_var_initialization(gen_proc_t * p, gen_var_t * v)
320 if(v -> storage == JVM_STORAGE_REGISTER_VAR)
322 jvm_generate(p, 0, 1, "iconst_1");
323 jvm_generate(p, 1, 1, "multianewarray [%s 1", v -> type -> desc);
324 jvm_generate(p, 1, 0, "astore %i", v -> reg);
326 else if(v -> storage == JVM_STORAGE_STATIC_VAR)
328 jvm_generate(p, 0, 1, "iconst_1");
329 jvm_generate(p, 1, 1, "multianewarray [%s 1", v -> type -> desc);
330 jvm_generate(p, 1, 0, "putstatic %s [%s", v -> full_name, v -> type -> desc);
332 else if(v -> storage == JVM_STORAGE_FIELD_VAR)
334 jvm_generate(p, 0, 1, "iconst_1");
335 jvm_generate(p, 1, 1, "multianewarray [%s 1", v -> type -> desc);
336 jvm_generate(p, 1, 0, "putfield %s [%s", v -> full_name, v -> type -> desc);
338 else if(v -> storage == JVM_STORAGE_PARAM_VAR)
340 jvm_generate_param_initialization(p, v);
342 else if(v -> storage == JVM_STORAGE_FRAME_PARAM
343 || v -> storage == JVM_STORAGE_FRAME_PARAM_VAR)
345 jvm_generate_ldst_prepare(p, v);
346 jvm_generate(p, 0, v -> type -> cell_size, "%cload %i", v -> type -> prefix, v -> reg);
347 jvm_generate_store(p, v);
349 else if(v -> storage == JVM_STORAGE_FRAME_PARAM_VARPTR)
351 jvm_generate(p, 0, 1, "aload %i", p -> reg_frame[v -> level]);
352 jvm_generate(p, 0, 1, "aload %i", v -> reg);
353 jvm_generate(p, 1, 0, "putfield %s [%s", v -> full_name, v -> type -> desc);
355 jvm_generate(p, 0, 1, "aload %i", p -> reg_frame[v -> level]);
356 jvm_generate(p, 0, 1, "iload %i", v -> reg + 1);
357 jvm_generate(p, 1, 0, "putfield %s$offset I", v -> full_name);
361 /*
362 static void
363 jvm_generate_abstract_frame_class()
365 FILE * fp;
366 char * cname;
367 struct gen_class * class;
369 cname = "SYSTEM$FRAME";
370 class = jvm_create_class(cname);
371 fp = class -> fp;
373 fprintf(fp, ".source SYSTEM\n");
374 fprintf(fp, ".class public abstract %s\n", cname);
375 fprintf(fp, ".super java/lang/Object\n\n");
377 fprintf(fp, ".field public $UP L%s;\n\n", cname);
379 fprintf(fp, ".method <init>()V\n");
380 fprintf(fp, " aload_0\n");
381 fprintf(fp, " invokespecial java/lang/Object/<init>()V\n");
382 fprintf(fp, " return\n");
383 fprintf(fp, ".end method\n\n");
385 jvm_destroy_class(class);
387 */
389 static struct gen_class *
390 jvm_generate_frame_class(oberon_object_t * proc)
392 FILE * fp;
393 char * cname;
394 char * abscname;
395 struct gen_class * class;
396 oberon_object_t * var;
398 abscname = "SYSTEM$FRAME";
399 cname = jvm_get_field_full_name_safe(proc);
400 cname = new_string("%s$FRAME", cname);
401 class = jvm_create_class(cname);
402 fp = class -> fp;
404 class -> full_name = cname;
406 fprintf(fp, ".source %s\n", proc -> module -> name);
407 fprintf(fp, ".class public %s\n", cname);
408 fprintf(fp, ".super %s\n\n", abscname);
410 var = proc -> scope -> list -> next;
411 while(var)
413 if(var -> class == OBERON_CLASS_VAR
414 || var -> class == OBERON_CLASS_PARAM
415 || var -> class == OBERON_CLASS_VAR_PARAM)
417 gen_var_t * v = var -> gen_var;
418 if(v -> storage == JVM_STORAGE_FRAME
419 || v -> storage == JVM_STORAGE_FRAME_PARAM)
421 fprintf(fp, "; LEVEL %i\n", v -> level);
422 fprintf(fp, ".field public %s %s\n\n", v -> name, v -> type -> desc);
424 else if(v -> storage == JVM_STORAGE_FRAME_VAR
425 || v -> storage == JVM_STORAGE_FRAME_PARAM_VAR)
427 fprintf(fp, "; LEVEL %i\n", v -> level);
428 fprintf(fp, ".field public %s [%s\n\n", v -> name, v -> type -> desc);
430 else if(v -> storage == JVM_STORAGE_FRAME_PARAM_VARPTR)
432 fprintf(fp, "; LEVEL %i\n", v -> level);
433 fprintf(fp, ".field public %s [%s\n", v -> name, v -> type -> desc);
434 fprintf(fp, ".field public %s$offset I\n\n", v -> name);
437 var = var -> next;
440 fprintf(fp, ".method <init>()V\n");
441 fprintf(fp, " .limit stack 2\n");
442 fprintf(fp, " aload_0\n");
443 fprintf(fp, " invokespecial %s/<init>()V\n", abscname);
445 var = proc -> scope -> list -> next;
446 while(var)
448 if(var -> class == OBERON_CLASS_VAR
449 || var -> class == OBERON_CLASS_PARAM
450 || var -> class == OBERON_CLASS_VAR_PARAM)
452 gen_var_t * v = var -> gen_var;
453 if(v -> storage == JVM_STORAGE_FRAME_VAR
454 || v -> storage == JVM_STORAGE_FRAME_PARAM_VAR)
456 fprintf(fp, " aload_0\n");
457 fprintf(fp, " iconst_1\n");
458 fprintf(fp, " multianewarray [%s 1\n", v -> type -> desc);
459 fprintf(fp, " putfield %s [%s\n", v -> full_name, v -> type -> desc);
462 var = var -> next;
465 fprintf(fp, " return\n");
466 fprintf(fp, ".end method\n\n");
468 jvm_destroy_class(class);
470 return class;
473 void
474 jvm_generate_procedure_frame(oberon_object_t * proc)
476 gen_proc_t * p;
477 gen_type_t * t;
478 gen_var_t * v;
479 struct gen_class * class;
481 p = proc -> gen_proc;
483 // jvm_generate_abstract_frame_class();
485 class = jvm_generate_frame_class(proc);
487 t = GC_MALLOC(sizeof *t);
488 memset(t, 0, sizeof *t);
489 t -> class = class;
490 t -> wide = false;
491 t -> prefix = 'a';
492 t -> postfix = 'a';
493 t -> cell_size = 1;
494 t -> desc = new_string("L%s;", class -> full_name);
496 v = jvm_create_function_var(p, JVM_STORAGE_REGISTER, "$FP$", t);
497 p -> frame_v = v;
498 printf("jvm_generate_procedure_frame: %p level %i\n", p -> reg_frame, p -> level);
499 p -> reg_frame[p -> level] = v -> reg;
502 void
503 jvm_generate_frame_initialization(gen_proc_t * p)
505 jvm_generate_var(p -> frame_v);
506 jvm_generate_ldst_prepare(p, p -> frame_v);
507 jvm_generate(p, 0, 1, "new %s", p -> frame_v -> type -> class -> full_name);
508 jvm_generate(p, 1, 2, "dup");
509 jvm_generate(p, 1, 0, "invokespecial %s/<init>()V", p -> frame_v -> type -> class -> full_name);
510 jvm_generate_store(p, p -> frame_v);
513 char *
514 jvm_get_procedure_signature(oberon_type_t * ps)
516 char * signature;
517 char * desc;
518 int num;
519 oberon_object_t * arg;
521 signature = new_string("(");
523 num = ps -> num_decl;
524 arg = ps -> decl;
525 for(int i = 0; i < num; i++)
527 desc = jvm_get_descriptor(arg -> type);
528 if(arg -> class == OBERON_CLASS_VAR_PARAM)
530 signature = new_string("%s[%sI", signature, desc);
532 else
534 signature = new_string("%s%s", signature, desc);
536 arg = arg -> next;
539 desc = jvm_get_descriptor(ps -> base);
540 signature = new_string("%s)%s", signature, desc);
542 return signature;
545 static char *
546 jvm_get_frame_descriptor(oberon_object_t * proc)
548 char * full_name = jvm_get_field_full_name_safe(proc);
549 return new_string("L%s$FRAME;", full_name);
552 static char *
553 jvm_get_oberon_signature_static_links(oberon_object_t * parent)
555 if(parent == NULL)
557 return "";
560 char * up = jvm_get_oberon_signature_static_links(parent -> parent);
561 char * desc = jvm_get_frame_descriptor(parent);
562 return new_string("%s%s", up, desc);
565 char *
566 jvm_get_local_procedure_signature(oberon_object_t * proc)
568 char * stlinks;
569 char * signature;
570 char * desc;
571 int num;
572 oberon_object_t * arg;
573 oberon_type_t * ps;
575 ps = proc -> type;
577 signature = new_string("(");
579 if(proc -> local)
581 stlinks = jvm_get_oberon_signature_static_links(proc -> parent);
582 signature = new_string("%s%s", signature, stlinks);
585 num = ps -> num_decl;
586 arg = ps -> decl;
587 for(int i = 0; i < num; i++)
589 desc = jvm_get_descriptor(arg -> type);
590 if(arg -> class == OBERON_CLASS_VAR_PARAM)
592 signature = new_string("%s[%sI", signature, desc);
594 else
596 signature = new_string("%s%s", signature, desc);
598 arg = arg -> next;
601 desc = jvm_get_descriptor(ps -> base);
602 signature = new_string("%s)%s", signature, desc);
604 return signature;
608 static void
609 jvm_generate_staticlinks_recursive(gen_proc_t * p, oberon_object_t * parent, int level)
611 if(parent == NULL)
613 return;
616 jvm_generate_staticlinks_recursive(p, parent -> parent, level - 1);
617 int reg = p -> reg_frame[level];
618 char * desc = jvm_get_frame_descriptor(parent);
619 jvm_generate(p, 0, 0, ".var %i is SL$%i %s from start to end", reg, level, desc);
622 void
623 jvm_generate_staticlinks(oberon_object_t * proc)
625 gen_proc_t * p;
627 p = proc -> gen_proc;
629 jvm_generate_staticlinks_recursive(p, proc -> parent, p -> level);
632 static void
633 jvm_create_staticlinks_recursive(gen_proc_t * p, oberon_object_t * parent, int level)
635 if(parent == NULL)
637 return;
640 jvm_create_staticlinks_recursive(p, parent -> parent, level - 1);
641 p -> reg_frame[level] = jvm_alloc_register_untyped(p -> rf, false);
644 void
645 jvm_create_static_links(oberon_object_t * proc)
647 gen_proc_t * p;
648 p = proc -> gen_proc;
650 int max_frames = 0;
651 oberon_object_t * prc = proc;
652 while(prc)
654 max_frames += 1;
655 prc = prc -> parent;
658 p -> max_frames = max_frames;
659 p -> reg_frame = GC_MALLOC(sizeof *p -> reg_frame);
660 memset(p -> reg_frame, 0, max_frames * sizeof *p -> reg_frame);
662 p -> level = max_frames - 1;
663 jvm_create_staticlinks_recursive(p, proc -> parent, p -> level);
666 static void
667 jvm_generate_push_static_links_recursive(gen_proc_t * p, oberon_object_t * parent)
669 if(parent == NULL)
671 return;
674 jvm_generate_push_static_links_recursive(p, parent -> parent);
675 int level = parent -> gen_proc -> level;
676 jvm_generate(p, 0, 1, "aload %i", p -> reg_frame[level]);
679 void
680 jvm_generate_push_static_links(gen_proc_t * p, oberon_object_t * proc)
682 jvm_generate_push_static_links_recursive(p, proc -> parent);