Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.12 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "assert.h"
  4.  
  5. long max_cons = 16;
  6. long cons_so_far = 0;
  7. long* fromspace;
  8. long* tospace;
  9. long* val_stack;
  10. long* registers;
  11. long* B;
  12. long* S;
  13. long* T;
  14. long* rbp;
  15. long* rsp;
  16. long maskCAR = ~0xF;
  17. long maskCONS = ~0x2;
  18. long NIL = 0x1;
  19. //function headers
  20. long convert_cINT_toLisp(long x);
  21. long my_CONS(long x, long y);
  22. void print_tree_inorder(long x);
  23. void print_tree(int x);
  24. void print_vs();
  25. void print_reg();
  26. void print_mem();
  27. long my_app(long x, long y);
  28. long APP();
  29. long CONS();
  30. long CAR(long p);
  31. long CDR(long p);
  32. void vs_CAR();
  33. void vs_CDR();
  34. void vs_rplacd();
  35. void vs_rplaca();
  36. void vs_eq();
  37. void vs_equal();
  38. long my_cons_copy(long x);
  39. void cons_copy();
  40. int my_cons_count(long x);
  41. void cons_count();
  42. long my_rev(long x);
  43. void rev();
  44. long my_make_lst(long n);
  45. void make_lst();
  46. long move(long x);
  47. int isPointer(long x);
  48. void flip();
  49. int is_tospace(long x);
  50. int is_fromspace(long x);
  51. void RPLACA(long p, long a);
  52. void RPLACD(long x, long b);
  53. int EQ(long r, long s);
  54. int EQUAL(long x, long y);
  55. int ATOM(long k);
  56.  
  57. /*
  58. DONUT FORGET: CONS RETURN A POINTER TO THE CDR!! NOT THE CAR!!!!!!!!!
  59. */
  60.  
  61. void vs_push(long x) {
  62. *rsp = x;
  63. rsp++;
  64. }
  65. void vs_push_integer(int x) { //takes a C int
  66. vs_push(x<<4);
  67. }
  68.  
  69. void vs_push_register(int x) {
  70. vs_push(registers[x]);
  71. }
  72.  
  73. void vs_push_nil() {
  74. vs_push(NIL);
  75. }
  76.  
  77. void vs_push_t() {
  78. vs_push(1<<4);
  79. }
  80.  
  81. long vs_pop(){
  82. long ret = *(rsp-1);
  83. rsp--;
  84. return ret;
  85. }
  86.  
  87. void vs_assign(int x) {
  88. registers[x] = vs_pop();
  89. }
  90.  
  91. void vs_CAR() {
  92. long ptr = vs_pop();
  93. if(isPointer(ptr))
  94. vs_push(CAR(ptr));
  95. }
  96.  
  97. void vs_CDR() {
  98. long ptr = vs_pop();
  99. if(isPointer(ptr))
  100. vs_push(CDR(ptr));
  101. }
  102.  
  103. void vs_rplacd() {
  104. long to_replace = vs_pop();
  105. long val = vs_pop();
  106. RPLACD(to_replace, val);
  107. }
  108.  
  109. void vs_rplaca() {
  110. long to_replace = vs_pop();
  111. long val = vs_pop();
  112. RPLACA(to_replace, val);
  113. }
  114.  
  115. void vs_eq() {
  116. long x = vs_pop();
  117. long y = vs_pop();
  118. if(EQ(x, y)) {
  119. vs_push(1<<4);
  120. }
  121. else {
  122. vs_push(NIL);
  123. }
  124. }
  125.  
  126. void vs_equal() {
  127. long x = vs_pop();
  128. long y = vs_pop();
  129. if(EQUAL(x, y))
  130. vs_push(1<<4);
  131. else
  132. vs_push(NIL);
  133. }
  134.  
  135.  
  136. void print_vs_top() {
  137. long *ret = rsp-1;
  138. if(isPointer(*ret))
  139. printf("PTR %ld\n", *ret);
  140. else if(ATOM(*ret)) {
  141. printf("INT %ld\n", *ret >> 4);
  142. }
  143. else if(*ret == NIL) {
  144. printf("NIL\n");
  145. }
  146. printf("neither\n");
  147. }
  148.  
  149. void print_tree(int x){
  150. print_tree_inorder(registers[x]);
  151. printf("\n");
  152. }
  153.  
  154. void print_tree_inorder(long x) {
  155. if(ATOM(x)) {
  156. if(x == NIL)
  157. printf("NIL ");
  158. else
  159. printf("%ld ", x >> 4);
  160. }
  161. else {
  162. //printf("\n");
  163. print_tree_inorder(CAR(x));
  164. print_tree_inorder(CDR(x));
  165. //printf("\n");
  166. }
  167. }
  168.  
  169.  
  170.  
  171. void print_mem() {
  172. long address = (long)tospace;
  173. printf("TO SPACE:\n");
  174. printf("+----------------------+ %ld\n", (long)address);
  175. for (int i = 0; i < max_cons*2; i++) {
  176. printf("| %ld\t\t\t|\n", (ATOM(tospace[i])) ? tospace[i] >> 4 : tospace[i]);
  177. printf("+----------------------+ %ld\n", (long)(address += 8));
  178. }
  179. address = (long)fromspace;
  180. printf("\nFROM SPACE:\n");
  181. printf("+----------------------+ %ld\n", (long)address);
  182. for (int i = 0; i < max_cons*2; i++) {
  183. printf("| %ld\t\t\t|\n", (ATOM(fromspace[i])) ? fromspace[i] >> 4 : fromspace[i]);
  184. printf("+----------------------+ %ld\n", (long)(address += 8));
  185. }
  186. }
  187.  
  188. void print_vs() {
  189. long address = (long)rbp;
  190. printf("VALUE STACK:\n");
  191. printf("+----------------------+ %ld\n", (long)(address));
  192. for (int i = 0; i < (rsp - rbp); i++) {
  193. printf("| %ld\t\t\t|\n", (ATOM(rbp[i])) ? rbp[i] >> 4 : rbp[i]);
  194. printf("+----------------------+ %ld\n", (long)(address += 8));
  195. }
  196. }
  197.  
  198. void print_reg() {
  199. long address = (long)registers;
  200. printf("REGISTERS:\n");
  201. printf("+----------------------+ %ld\n", (long)(address));
  202. for (int i = 0; i < 5; i++) {
  203. printf("| %ld\t\t\t|\n", (ATOM(registers[i])) ? registers[i] >> 4 : registers[i]);
  204. printf("+----------------------+ %ld\n", (long)(address += 8));
  205. }
  206. }
  207.  
  208. int isPointer(long x) {
  209. return (x & 0xF) == 0x8;
  210. }
  211. long convert_cINT_toLisp(long x) {
  212. return (x << 4);
  213. }
  214.  
  215.  
  216.  
  217. long CONS() {
  218. long car = vs_pop();
  219. long cdr = vs_pop();
  220. long ret = my_CONS(car, cdr);
  221. vs_push(ret);
  222. return ret;
  223. }
  224.  
  225. long my_CONS(long x, long y) {
  226. if((long)B == (long)T) { //START GARBAGE COLLECTION
  227. printf("\n\n\nGARBAGE COLLECTION!!!\n\n");
  228. flip(); //flip tospace and fromspace
  229. for(int i = 0; i < max_cons; i++) {
  230. //printf("register %d\n", i);
  231. registers[i] = move(registers[i]);
  232. }
  233. for(int i = 0; i < (rsp - rbp)/8; i++) {
  234. if(isPointer(val_stack[i])) {
  235. if(!is_tospace(CDR(val_stack[i])))
  236. val_stack[i] = move(val_stack[i]);
  237. else
  238. val_stack[i] = CDR(val_stack[i]);
  239. }
  240. }
  241. x = move(x);
  242. y = move(y);
  243. while(S < B) {
  244. //printf("here\n");
  245. S[0] = move(S[0]);
  246. S[1] = move(S[1]);
  247. S += 2;
  248. }
  249. }
  250. //just allocate
  251. if(B >= T) {
  252. //print_mem();
  253. printf("out of memory\n");
  254. exit(-1);
  255. }
  256. B[0] = x;
  257. B[1] = y;
  258. long p = (long)(B + 1); //the CDR of the new Cons
  259. B += 2;
  260. return p;
  261.  
  262. }
  263.  
  264. //assume for our purposes that these will always be in our world, x and y will be our type
  265. long CAR(long p) {
  266. long* pointer = (long*) (p & maskCAR);
  267. return *(pointer);
  268. }
  269.  
  270. long CDR(long p) {
  271. long* pointer = (long*) p;
  272. return *pointer;
  273. //return right
  274. }
  275.  
  276. long copy(long x) {
  277. //x is always going to be a pointer to a CDR
  278. //printf("in copy\n");
  279. if(B >= T) {
  280. printf("error in cons\n");
  281. exit(0);
  282. }
  283. B[0] = CAR(x);
  284. B[1] = CDR(x);
  285. long temp = (long)(B + 1);
  286. B += 2;
  287. return temp;
  288. }
  289.  
  290. long move(long x) {
  291. //printf("in move %ld\n", x);
  292. if(!is_fromspace(x)) //not in fromspace, would be a number that hasnt been allocated yet!
  293. return x;
  294. else {
  295. if(!is_tospace(CDR(x))) {
  296. RPLACD(x, copy(x));
  297. return CDR(x);
  298. }
  299. }
  300. return CDR(x);
  301. }
  302.  
  303. void flip() {
  304. long* temp = tospace;
  305. tospace = fromspace;
  306. fromspace = temp;
  307. B = tospace;
  308. S = tospace;
  309. T = tospace + max_cons*2;
  310. }
  311.  
  312. int is_tospace(long x) {
  313. if(!isPointer(x))
  314. return 0;
  315. long* bot = tospace;
  316. long* top = tospace + max_cons*2;
  317. if((long)bot < x && x <= (long)top)
  318. return 1;
  319. return 0;
  320. }
  321.  
  322. int is_fromspace(long x) {
  323. //check if its an ATOM
  324. if(!isPointer(x))
  325. return 0;
  326. // long bot = (long)fromspace;
  327. // long top = ((long)fromspace) + max_cons*2;
  328.  
  329. long* bot = fromspace;
  330. long* top = fromspace + max_cons*2;
  331. if((long)bot < x && x <= (long)top)
  332. return 1;
  333. return 0;
  334. }
  335.  
  336.  
  337.  
  338. void RPLACA(long p, long a){
  339. long* toreplace = (long*)(maskCAR & p);
  340. *toreplace = a;
  341. // Replace x of pair x, y, with a
  342. }
  343.  
  344. void RPLACD(long x, long b) {
  345. long* pointer = (long*) x;
  346. *pointer = b;
  347. // Replace x of pair x, y, with b
  348. }
  349.  
  350. int EQ(long r, long s) {
  351. return r == s;
  352. //do r and s reference the same object
  353. }
  354.  
  355. int EQUAL(long x, long y) {
  356. if(ATOM(x) && ATOM(y))
  357. return x == y;
  358. else if(ATOM(x) || ATOM(y))
  359. return 0;
  360.  
  361. if(EQUAL(CAR(x), CAR(y)) && EQUAL(CDR(x), CDR(y)))
  362. return 1;
  363. else
  364. return 0;
  365.  
  366. }
  367.  
  368.  
  369. int ATOM(long k) {
  370. return !(k & 0x8);
  371. // does k reference a non-pair
  372. }
  373.  
  374. long APP() {
  375. long x = vs_pop();
  376. long y = vs_pop();
  377. long ret = my_app(x, y);
  378. vs_push(ret);
  379. }
  380.  
  381. long my_app(long x, long y) {
  382. if(ATOM(x))
  383. return y;
  384. vs_push(my_app(CDR(x), y));
  385. vs_push(CAR(x));
  386. long ret = CONS();
  387. vs_pop();
  388. return ret;
  389. }
  390.  
  391. void rev() {
  392. long ptr = vs_pop();
  393. if(!isPointer(ptr)) {
  394. printf("error in rev\n");
  395. exit(-1);
  396. }
  397. else {
  398. //long x = vs_pop();
  399. vs_push(my_rev(ptr));
  400. }
  401. }
  402.  
  403. long my_rev(long x) {
  404. if(ATOM(x))
  405. return NIL;
  406. vs_push(NIL);
  407. vs_push(CAR(x));
  408. //print_vs();
  409. CONS();
  410. vs_push(my_rev(CDR(x)));
  411. APP();
  412. long ret = vs_pop();
  413. return ret;
  414. }
  415.  
  416. void cons_copy() {
  417. long ret = my_cons_copy(vs_pop());
  418. vs_push(ret);
  419. }
  420.  
  421. long my_cons_copy(long x) {
  422. if(ATOM(x))
  423. return x;
  424. vs_push(my_cons_copy(CDR(x)));
  425. vs_push(my_cons_copy(CAR(x)));
  426. CONS();
  427. return vs_pop();
  428. }
  429.  
  430. void cons_count() {
  431. int ret = my_cons_count(vs_pop());
  432. vs_push_integer(ret);
  433. }
  434.  
  435. int my_cons_count(long x) {
  436. if(ATOM(x))
  437. return 0;
  438. return 1 + my_cons_count(CAR(x)) + my_cons_count(CDR(x));
  439. }
  440.  
  441. void make_lst() {
  442. long ret = my_make_lst(vs_pop()>>4);
  443. vs_push(ret);
  444. }
  445.  
  446. long my_make_lst(long n) {
  447. if(n <= 0)
  448. return NIL;
  449. else {
  450. vs_push(my_make_lst(n-1));
  451. vs_push_nil();
  452. CONS();
  453. long ret = vs_pop();
  454. return ret;
  455. }
  456. }
  457.  
  458. int main(int argc, char* argv[], char** env) {
  459. max_cons = 2000000;
  460. fromspace = (long*)malloc((max_cons*2)*sizeof(long));
  461. tospace = (long*)malloc((max_cons*2)*sizeof(long));
  462. val_stack = (long*)malloc((max_cons*2)*sizeof(long));
  463. registers = (long*)malloc(max_cons*sizeof(long));
  464. rbp = val_stack;
  465. rsp = val_stack;
  466. B = tospace;
  467. S = tospace;
  468. T = tospace + max_cons*2;
  469. //printf("bfore\n");
  470. vs_push_integer(1000);
  471. //printf("%ld\n", vs_pop()>>4);
  472. make_lst();
  473. vs_assign(0);
  474.  
  475. vs_push_register(0);
  476. //printf("%ld\n", vs_pop());
  477. rev();
  478. rev();
  479. rev();
  480. rev();
  481. vs_assign(1);
  482. vs_push_register(0);
  483. vs_push_register(1);
  484. vs_equal();
  485. print_vs_top();
  486. // print_vs_top();
  487. // printf("what\n");
  488. // vs_push_integer(1);
  489. // print_vs_top();
  490.  
  491. // vs_push_integer(2);
  492. // print_vs_top();
  493.  
  494. // //print_vs();
  495.  
  496. // CONS();
  497. // print_vs_top();
  498.  
  499. // // print_mem();
  500. // // print_vs();
  501.  
  502. // vs_assign(0);
  503. // print_tree(0);
  504.  
  505. // vs_push_integer(3);
  506. // print_vs_top();
  507.  
  508. // vs_push_integer(4);
  509. // print_vs_top();
  510.  
  511. // //print_vs();
  512.  
  513. // CONS();
  514. // print_vs_top();
  515.  
  516. // vs_assign(1);
  517. // print_tree(1);
  518.  
  519. // vs_push_register(0);
  520. // vs_push_register(1);
  521. // APP();
  522. // vs_assign(2);
  523. // print_tree(2);
  524.  
  525. // vs_push_register(2);
  526. // print_vs_top();
  527. // // if(isPointer(vs_pop()))
  528. // // printf("IS POINTER\n");
  529. // rev();
  530. // print_vs_top();
  531. // vs_assign(3);
  532. // print_tree(3);
  533. // vs_push_register(3);
  534. // cons_copy();
  535. // vs_assign(4);
  536. // print_tree(4);
  537. // vs_push_register(4);
  538. // cons_count();
  539. // print_vs_top();
  540. // // vs_push_integer(1);
  541. // print_vs_top();
  542. // vs_push_integer(2);
  543. // print_vs_top();
  544. // CONS();
  545. // print_vs_top();
  546. // vs_assign(0);
  547. // print_tree(0);
  548. // vs_push_nil();
  549. // vs_push_register(0);
  550. // CONS();
  551. // vs_assign(1);
  552. // print_tree(1);
  553. // vs_push_register(0);
  554. // vs_push_register(1);
  555. // APP();
  556. // vs_assign(2);
  557. // print_tree(2);
  558. // print_mem();
  559. // vs_push_register(2);
  560. // vs_assign(3);
  561. // print_tree(3);
  562. // vs_push_register(2);
  563. // vs_push_register(3);
  564. // vs_equal();
  565. // print_vs_top();
  566. // vs_push_integer(3);
  567. // print_vs_top();
  568.  
  569. // vs_push_integer(4);
  570. // print_vs_top();
  571.  
  572. // print_vs();
  573.  
  574. // CONS();
  575. // print_vs_top();
  576.  
  577. // vs_assign(4);
  578. // print_tree(4);
  579.  
  580. // vs_push_register(3);
  581. // vs_push_register(4);
  582.  
  583. // vs_equal();
  584.  
  585. // print_vs_top();
  586. //print_mem();
  587. // long ptr = CONS(1 << 4, 2 << 4);
  588. // registers[0] = ptr;
  589. // registers[1] = CONS(ptr, 3 << 4);
  590. // registers[2] = CONS(5 << 4, 6 << 4);
  591. // registers[2] = 0;
  592. // print_mem();
  593. // registers[2] = CONS(7 << 4, 8 << 4);
  594. // print_mem();
  595. }
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606. /*
  607. From OH: basically need to wait for tests
  608. CONS should only pull from the value val_stack
  609. the first thing that happens is that you push an int (so make a push_int function that pushes things to the value stack)
  610. push int does the conversion from ints to to our object ints
  611. CONS pulls off of the value stack
  612. so for cons, we basically just assume that its always in OUR WORLD(aka padded with the tag)
  613.  
  614. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement